Class: Effective::ArrayDatatableTool

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective/array_datatable_tool.rb

Overview

The collection is an Array of Arrays

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datatable, table_columns) ⇒ ArrayDatatableTool

Returns a new instance of ArrayDatatableTool.



8
9
10
11
# File 'app/models/effective/array_datatable_tool.rb', line 8

def initialize(datatable, table_columns)
  @datatable = datatable
  @table_columns = table_columns
end

Instance Attribute Details

#table_columnsObject

Returns the value of attribute table_columns.



4
5
6
# File 'app/models/effective/array_datatable_tool.rb', line 4

def table_columns
  @table_columns
end

Instance Method Details

#order(collection) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/effective/array_datatable_tool.rb', line 21

def order(collection)
  if order_column.present?
    index = display_index(order_column)

    if order_direction == 'ASC'
      collection.sort! { |x, y| (x[index] && y[index]) ? (x[index] <=> y[index]) : 0 }
    else
      collection.sort! { |x, y| (x[index] && y[index]) ? (y[index] <=> x[index]) : 0 }
    end
  end

  collection
end

#order_columnObject



17
18
19
# File 'app/models/effective/array_datatable_tool.rb', line 17

def order_column
  @order_column ||= table_columns[order_name]
end

#paginate(collection) ⇒ Object



59
60
61
# File 'app/models/effective/array_datatable_tool.rb', line 59

def paginate(collection)
  Kaminari.paginate_array(collection).page(page).per(per_page)
end

#search(collection) ⇒ Object



35
36
37
38
39
40
41
42
# File 'app/models/effective/array_datatable_tool.rb', line 35

def search(collection)
  search_terms.each do |name, search_term|
    column_search = search_column(collection, table_columns[name], search_term)
    raise 'search_column must return an Array object' unless column_search.kind_of?(Array)
    collection = column_search
  end
  collection
end

#search_column_with_defaults(collection, table_column, search_term) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/effective/array_datatable_tool.rb', line 44

def search_column_with_defaults(collection, table_column, search_term)
  search_term = search_term.downcase
  index = display_index(table_column)

  collection.select! do |row|
    value = row[index].to_s.downcase

    if table_column[:filter][:type] == :select && table_column[:filter][:fuzzy] != true
      value == search_term
    else
      value.include?(search_term)
    end
  end || collection
end

#search_termsObject



13
14
15
# File 'app/models/effective/array_datatable_tool.rb', line 13

def search_terms
  @search_terms ||= @datatable.search_terms.select { |name, search_term| table_columns.key?(name) }
end