Class: Effective::ArrayDatatableTool
- Inherits:
-
Object
- Object
- Effective::ArrayDatatableTool
- Defined in:
- app/models/effective/array_datatable_tool.rb
Overview
The collection is an Array of Arrays
Instance Attribute Summary collapse
-
#table_columns ⇒ Object
Returns the value of attribute table_columns.
Instance Method Summary collapse
-
#initialize(datatable, table_columns) ⇒ ArrayDatatableTool
constructor
A new instance of ArrayDatatableTool.
- #order(collection) ⇒ Object
- #order_by_column ⇒ Object
- #order_column_with_defaults(collection, table_column, direction, index) ⇒ Object
- #paginate(collection) ⇒ Object
- #search(collection) ⇒ Object
- #search_column_with_defaults(collection, table_column, search_term, index) ⇒ Object
- #search_terms ⇒ Object
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_columns ⇒ Object
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 |
# File 'app/models/effective/array_datatable_tool.rb', line 21 def order(collection) return collection unless order_by_column.present? column_order = order_column(collection, order_by_column, @datatable.order_direction, display_index(order_by_column)) raise 'order_column must return an Array' unless column_order.kind_of?(Array) column_order end |
#order_by_column ⇒ Object
17 18 19 |
# File 'app/models/effective/array_datatable_tool.rb', line 17 def order_by_column @order_by_column ||= table_columns[@datatable.order_name] end |
#order_column_with_defaults(collection, table_column, direction, index) ⇒ Object
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 57 |
# File 'app/models/effective/array_datatable_tool.rb', line 29 def order_column_with_defaults(collection, table_column, direction, index) if direction == :asc collection.sort! do |x, y| if (x[index] && y[index]) convert_to_column_type(table_column, x[index]) <=> convert_to_column_type(table_column, 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]) convert_to_column_type(table_column, y[index]) <=> convert_to_column_type(table_column, x[index]) elsif x[index] 1 elsif y[index] -1 else 0 end end end collection end |
#paginate(collection) ⇒ Object
80 81 82 |
# File 'app/models/effective/array_datatable_tool.rb', line 80 def paginate(collection) Kaminari.paginate_array(collection).page(page).per(per_page) end |
#search(collection) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'app/models/effective/array_datatable_tool.rb', line 59 def search(collection) search_terms.each do |name, search_term| column_search = search_column(collection, table_columns[name], search_term, display_index(table_columns[name])) 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, index) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/models/effective/array_datatable_tool.rb', line 68 def search_column_with_defaults(collection, table_column, search_term, index) search_term = search_term.downcase if table_column[:filter][:fuzzy] collection.select! do |row| if table_column[:filter][:fuzzy] convert_to_column_type(table_column, row[index]).to_s.downcase.include?(search_term) else row[index] == search_term end end || collection end |
#search_terms ⇒ Object
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 |