Add order on topic index.

This commit is contained in:
taitus
2017-08-07 18:56:04 +02:00
parent 1dc3cd4bb3
commit a1d37fa6c4
3 changed files with 15 additions and 21 deletions

View File

@@ -1,6 +1,8 @@
class CommunitiesController < ApplicationController class CommunitiesController < ApplicationController
before_action :set_community, :load_topics, only: :show before_action :set_order, :set_community, :load_topics, only: :show
has_orders %w{newest most_commented oldest}, only: :show
skip_authorization_check skip_authorization_check
@@ -9,11 +11,15 @@ class CommunitiesController < ApplicationController
private private
def set_order
@order = params[:order].present? ? params[:order] : "newest"
end
def set_community def set_community
@community = Community.find(params[:id]) @community = Community.find(params[:id])
end end
def load_topics def load_topics
@topics = @community.topics.page(params[:page]) @topics = @community.topics.send("sort_by_#{@order}").page(params[:page])
end end
end end

View File

@@ -9,6 +9,10 @@ class Topic < ActiveRecord::Base
after_create :associate_comment after_create :associate_comment
scope :sort_by_newest, -> { order(created_at: :desc) }
scope :sort_by_oldest, -> { order(created_at: :asc) }
scope :sort_by_most_commented, -> { reorder(comments_count: :desc) }
private private
def associate_comment def associate_comment

View File

@@ -1,22 +1,6 @@
<div class="row column">
<div class="order"> <div class="order">
<div class="wide-order-selector small-12 medium-8">
<form>
<div class="small-12 medium-8 float-left">
<label for="order-selector-participation">
Ordenar por: Ordenar por:
</label> <%= render 'shared/wide_order_selector', i18n_namespace: "topics" %>
</div>
<div class="small-12 medium-4 float-left">
<select class="js-location-changer js-order-selector select-order">
<option>Value 1</option>
<option>Value 2</option>
</select>
</div>
</form>
</div>
</div>
</div> </div>
<% if topics.any? %> <% if topics.any? %>