Move all order logic to models

- Have a default order for each model
-- The overall default is ID DESC
- Allow for custom orderings
-- When comma-separated IDs are used
This commit is contained in:
BrokenEagle
2018-01-28 20:46:18 -08:00
parent f83480cc8a
commit d829ab3a00
49 changed files with 134 additions and 116 deletions

View File

@@ -59,9 +59,12 @@ class WikiPage < ApplicationRecord
end
end
def default_order
order(updated_at: :desc)
end
def search(params = {})
q = super
params = {} if params.blank?
if params[:title].present?
q = q.where("title LIKE ? ESCAPE E'\\\\'", params[:title].mb_chars.downcase.tr(" ", "_").to_escaped_for_sql_like)
@@ -95,14 +98,12 @@ class WikiPage < ApplicationRecord
params[:order] ||= params.delete(:sort)
case params[:order]
when "time"
q = q.order("updated_at desc")
when "title"
q = q.order("title")
when "post_count"
q = q.includes(:tag).order("tags.post_count desc nulls last").references(:tags)
else
q = q.order("updated_at desc")
q = q.apply_default_order(params)
end
q