gitlab-org--gitlab-foss/app/models/concerns/sortable.rb

36 lines
1.1 KiB
Ruby
Raw Normal View History

# == Sortable concern
#
# Set default scope for ordering objects
#
module Sortable
extend ActiveSupport::Concern
included do
# By default all models should be ordered
# by created_at field starting from newest
default_scope { order(created_at: :desc, id: :desc) }
2015-02-05 22:15:05 -05:00
scope :order_created_desc, -> { reorder(created_at: :desc, id: :desc) }
scope :order_created_asc, -> { reorder(created_at: :asc, id: :asc) }
scope :order_updated_desc, -> { reorder(updated_at: :desc, id: :desc) }
scope :order_updated_asc, -> { reorder(updated_at: :asc, id: :asc) }
2015-02-07 02:57:28 -05:00
scope :order_name_asc, -> { reorder(name: :asc) }
scope :order_name_desc, -> { reorder(name: :desc) }
end
module ClassMethods
2015-02-05 19:49:41 -05:00
def order_by(method)
case method.to_s
2015-02-05 22:15:05 -05:00
when 'name_asc' then order_name_asc
2015-02-05 23:21:21 -05:00
when 'name_desc' then order_name_desc
2015-02-05 22:15:05 -05:00
when 'updated_asc' then order_updated_asc
when 'updated_desc' then order_updated_desc
when 'created_asc' then order_created_asc
when 'created_desc' then order_created_desc
else
2015-02-05 19:49:41 -05:00
all
end
end
end
end