Module: Headmin::Sortable

Defined in:
app/controllers/concerns/headmin/sortable.rb

Instance Method Summary collapse

Instance Method Details

#sort(collection) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'app/controllers/concerns/headmin/sortable.rb', line 3

def sort(collection)
  if sort_params.to_h.any?
    sort_by_params(collection)
  elsif collection.attribute_names.include?(:created_at.to_s)
    collection.order(created_at: :desc)
  else
    collection
  end
end

#sort_by_params(collection) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/concerns/headmin/sortable.rb', line 13

def sort_by_params(collection)
  sort_params.each do |key, value|
    next unless %w[asc desc].include?(value)
    attribute = key.gsub("sort_", "")

    # Sort on model columns first, then check for translation columns
    if collection.attribute_names.include?(attribute.to_s)
      column = collection.arel_table[attribute]
    elsif collection.respond_to?(:translation_class) && collection.translation_class.attribute_names.include?(attribute.to_s)
      column = collection.translation_class.arel_table[attribute]
    else
      next
    end

    order_scope = "order_by_#{attribute}".to_sym
    sort_scope = "sort_by_#{attribute}".to_sym
    collection = if collection.respond_to?(order_scope)
      collection.send(order_scope, value)
    elsif collection.respond_to?(sort_scope)
      collection.send(sort_scope, value)
    else
      collection.order(column.send(value))
    end
  end
  collection
end

#sort_paramsObject



40
41
42
# File 'app/controllers/concerns/headmin/sortable.rb', line 40

def sort_params
  params.permit!.select { |p| p.to_s.include?("sort_") }
end