Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sortable.rb « concerns « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0ad2654867d767ca1e18955ab773d81ce59a23f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# == 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) }

    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) }
    scope :order_name_asc, -> { reorder(name: :asc) }
    scope :order_name_desc, -> { reorder(name: :desc) }
  end

  module ClassMethods
    def order_by(method)
      case method.to_s
      when 'name_asc' then order_name_asc
      when 'name_desc' then order_name_desc
      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
        all
      end
    end
  end
end