Module: Wallaby::SortingHelper

Included in:
ResourcesHelper
Defined in:
lib/helpers/wallaby/sorting_helper.rb

Instance Method Summary collapse

Instance Method Details

#next_sort_param(field_name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/helpers/wallaby/sorting_helper.rb', line 21

def next_sort_param(field_name)
  field_name  = field_name.to_s
  orders      = [ 'asc', 'desc', nil ]
  clean_params.tap do |hash|
    sortings = sort_hash.dup
    next_index = (orders.index(sortings[field_name]) + 1) % orders.length
    if orders[next_index].nil?
      sortings.delete field_name
    else
      sortings[field_name] = orders[next_index]
    end
    if sortings.present?
      hash[:sort] = sortings.to_a.map{ |v| v.join ' ' }.join ','
    else
      hash.delete :sort
    end
  end
end

#sort_class(field_name) ⇒ Object



40
41
42
# File 'lib/helpers/wallaby/sorting_helper.rb', line 40

def sort_class(field_name)
  sort_hash[ field_name.to_s ]
end

#sort_hashObject



14
15
16
17
18
19
# File 'lib/helpers/wallaby/sorting_helper.rb', line 14

def sort_hash
  @sort_hash ||= begin
    array = params[:sort].to_s.split(%r(\s*,\s*)).map{ |v| v.split %r(\s+) }
    Wallaby::Utils.to_hash array
  end
end


2
3
4
5
6
7
8
9
10
11
12
# File 'lib/helpers/wallaby/sorting_helper.rb', line 2

def sort_link(field_name, model_decorator = current_model_decorator)
   = model_decorator. field_name

  if [:is_origin] && ![:is_association] || [:sort_field_name]
    sort_field_name = [:sort_field_name] || field_name
    extra_params = next_sort_param(sort_field_name).permit!.to_h
    index_link(model_decorator.model_class, extra_params: extra_params) { [:label] }
  else
    [:label]
  end
end