Module: LatoCore::Interface::Cells

Included in:
LatoCore::Interface
Defined in:
lib/lato_core/interfaces/cells.rb

Overview

This module contains a list of functions used by controllers to interact with widgets cells.

Instance Method Summary collapse

Instance Method Details

#core__cells_initializeObject

This function must be executed before every action and set metadata used by cells.



9
10
11
# File 'lib/lato_core/interfaces/cells.rb', line 9

def core__cells_initialize
  @core__cells_authenticity_token = form_authenticity_token
end

#core__widgets_index(records, search: nil, pagination: 50) ⇒ Object

This function manage the widget index from front end.



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
51
52
53
54
55
56
# File 'lib/lato_core/interfaces/cells.rb', line 14

def core__widgets_index(records, search: nil, pagination: 50)
  response = {
    records: records,
    total: records.length,
    per_page: pagination,
    search: '',
    search_key: search,
    sort: '',
    sort_dir: 'ASC',
    pagination: 1,
  }

  # manage search
  if search && params[:widget_index] && params[:widget_index][:search] && !params[:widget_index][:search].blank?
    search_array = search.is_a?(Array) ? search : [search]
    query1 = ''
    query2 = []
    search_array.each do |s|
      query1 += "#{s} like ? OR "
      query2.push("%#{params[:widget_index][:search]}%")
    end
    query1 = query1[0...-4]
    query = [query1] + query2
    response[:records] = response[:records].where(query)
    response[:total] = response[:records].length
    response[:search] = params[:widget_index][:search]
  end
  # manage sort
  if params[:widget_index] && !params[:widget_index][:sort].blank? && !params[:widget_index][:sort_dir].blank?
    response[:sort] = params[:widget_index][:sort]
    response[:sort_dir] = params[:widget_index][:sort_dir]
    response[:records] = response[:records].order("#{params[:widget_index][:sort]} #{params[:widget_index][:sort_dir]}")
  end
  # manage pagination
  if pagination
    if params[:widget_index] && params[:widget_index][:pagination]
      response[:pagination] = params[:widget_index][:pagination].to_i
    end
    response[:records] = core__paginate_array(response[:records], pagination, response[:pagination])
  end
  # return response
  response
end