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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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! do |x, y|
        if (x[index] && y[index])
          x[index] <=> y[index]
        elsif x[index]
          -1
        elsif y[index]
          1
        else
          0
        end
      end
    else
      collection.sort! do |x, y|
        if (x[index] && y[index])
          y[index] <=> x[index]
        elsif x[index]
          1
        elsif y[index]
          -1
        else
          0
        end
      end
    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



79
80
81
# File 'app/models/effective/array_datatable_tool.rb', line 79

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

#search(collection) ⇒ Object



55
56
57
58
59
60
61
62
# File 'app/models/effective/array_datatable_tool.rb', line 55

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/effective/array_datatable_tool.rb', line 64

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