Module: Tramway::Filtering

Included in:
ApplicationController
Defined in:
app/controllers/concerns/filtering.rb

Instance Method Summary collapse

Instance Method Details

#date_filter(type, filter) ⇒ Object



31
32
33
# File 'app/controllers/concerns/filtering.rb', line 31

def date_filter(type, filter)
  params[:list_filters][filter.to_sym]["#{type}_date".to_sym]
end

#filtering(records) ⇒ Object



35
36
37
38
39
40
41
42
# File 'app/controllers/concerns/filtering.rb', line 35

def filtering(records)
  if params[:filter].present?
    params[:filter] = JSON.parse params[:filter] if params[:filter].is_a? String
    records.ransack(params[:filter]).result(distinct: true)
  else
    records
  end
end

#list_filtering(records) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/controllers/concerns/filtering.rb', line 4

def list_filtering(records)
  params[:list_filters]&.each do |filter, _value|
    case decorator_class.list_filters[filter.to_sym][:type]
    when :select
      records = list_filtering_select records, filter
    when :dates
      records = list_filtering_dates records, filter
    end
  end

  records
end

#list_filtering_dates(records, filter) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'app/controllers/concerns/filtering.rb', line 21

def list_filtering_dates(records, filter)
  begin_date = date_filter :begin, filter
  end_date = date_filter :end, filter
  if begin_date.present? && end_date.present? && value.present?
    decorator_class.list_filters[filter.to_sym][:query].call(records, begin_date, end_date)
  else
    records
  end
end

#list_filtering_select(records, filter) ⇒ Object



17
18
19
# File 'app/controllers/concerns/filtering.rb', line 17

def list_filtering_select(records, filter)
  value.present? ? decorator_class.list_filters[filter.to_sym][:query].call(records, value) : records
end