Change Debate.sort_by method for individual scopes
This commit is contained in:
@@ -6,7 +6,7 @@ class DebatesController < ApplicationController
|
|||||||
respond_to :html, :js
|
respond_to :html, :js
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@debates = Debate.search(params).page(params[:page]).for_render.sort_by(@order)
|
@debates = Debate.search(params).page(params[:page]).for_render.send("sort_by_#{@order}")
|
||||||
set_debate_votes(@debates)
|
set_debate_votes(@debates)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ class Debate < ActiveRecord::Base
|
|||||||
scope :archived, -> { where("archived_at IS NOT NULL AND hidden_at IS NULL") }
|
scope :archived, -> { where("archived_at IS NOT NULL AND hidden_at IS NULL") }
|
||||||
scope :flagged_as_inappropiate, -> { where("inappropiate_flags_count > 0") }
|
scope :flagged_as_inappropiate, -> { where("inappropiate_flags_count > 0") }
|
||||||
scope :for_render, -> { includes(:tags) }
|
scope :for_render, -> { includes(:tags) }
|
||||||
|
scope :sort_by_total_votes, -> { reorder(cached_votes_total: :desc) }
|
||||||
|
scope :sort_by_likes , -> { reorder(cached_votes_up: :desc) }
|
||||||
|
scope :sort_by_created_at, -> { reorder(created_at: :desc) }
|
||||||
|
|
||||||
|
|
||||||
# Ahoy setup
|
# Ahoy setup
|
||||||
visitable # Ahoy will automatically assign visit_id on create
|
visitable # Ahoy will automatically assign visit_id on create
|
||||||
@@ -40,17 +44,6 @@ class Debate < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.sort_by(order)
|
|
||||||
case order
|
|
||||||
when 'total_votes'
|
|
||||||
reorder(cached_votes_total: :desc)
|
|
||||||
when 'created_at'
|
|
||||||
reorder(created_at: :desc)
|
|
||||||
when 'likes'
|
|
||||||
reorder(cached_votes_up: :desc)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def likes
|
def likes
|
||||||
cached_votes_up
|
cached_votes_up
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user