Module: Lato::Componentable

Extended by:
ActiveSupport::Concern
Included in:
ApplicationController
Defined in:
app/controllers/concerns/lato/componentable.rb

Instance Method Summary collapse

Instance Method Details

#lato_index_collection(collection, options = {}) ⇒ Object



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/concerns/lato/componentable.rb', line 5

def lato_index_collection(collection, options = {})
  # load options
  # NOTE: instance variables are for options used by "lato_index" component helper
  key = options[:key] || 'default'
  pagination = options[:pagination] || false
  @_lato_index ||= {}
  @_lato_index[key] = {
    columns: options[:columns] || collection.column_names || [],
    sortable_columns: options[:sortable_columns] || [],
    searchable_columns: options[:searchable_columns] || []
  }

  # manage sort by parameter
  unless params[:sort_by].blank?
    sort_by_splitted = params[:sort_by].split('|')
    sort_by_column = sort_by_splitted.first
    sort_by_order = sort_by_splitted.last

    if collection.respond_to?(:lato_index_order)
      collection = collection.lato_index_order(sort_by_column.to_sym, sort_by_order.to_sym)
    else
      collection = collection.order("#{sort_by_column} #{sort_by_order}")
    end
  end

  # manage search by parameter
  unless params[:search].blank?
    search = params[:search].to_s
    if collection.respond_to?(:lato_index_search)
      collection = collection.lato_index_search(search)
    else
      query = @_lato_index[key][:searchable_columns].map { |k| "lower(#{k}) LIKE :search" }
      collection = collection.where(query.join(' OR '), search: "%#{search.downcase.strip}%")
    end
  end

  # manage pagination
  if pagination || params[:page] || params[:per_page]
    page = params[:page]&.to_i || 1
    per_page = params[:per_page]&.to_i || 25
    per_page = 100 if per_page > 100
    collection = collection.page(page).per(per_page)
  end

  collection
end