Class: Effective::DatatableValueTool
- Inherits:
-
Object
- Object
- Effective::DatatableValueTool
- Defined in:
- app/models/effective/datatable_value_tool.rb
Overview
The collection is an Array of Arrays
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#datatable ⇒ Object
readonly
Returns the value of attribute datatable.
Instance Method Summary collapse
-
#initialize(datatable) ⇒ DatatableValueTool
constructor
A new instance of DatatableValueTool.
- #order(collection) ⇒ Object
- #order_column(collection, direction, column, index) ⇒ Object
- #ordered ⇒ Object
- #paginate(collection) ⇒ Object
- #search(collection) ⇒ Object
- #search_column(collection, value, column, index) ⇒ Object
- #searched ⇒ Object
- #size(collection) ⇒ Object
Constructor Details
#initialize(datatable) ⇒ DatatableValueTool
Returns a new instance of DatatableValueTool.
7 8 9 10 11 12 13 14 15 |
# File 'app/models/effective/datatable_value_tool.rb', line 7 def initialize(datatable) @datatable = datatable if datatable.array_collection? @columns = datatable.columns else @columns = datatable.columns.select { |_, col| col[:sql_column].blank? } end end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
5 6 7 |
# File 'app/models/effective/datatable_value_tool.rb', line 5 def columns @columns end |
#datatable ⇒ Object (readonly)
Returns the value of attribute datatable.
4 5 6 |
# File 'app/models/effective/datatable_value_tool.rb', line 4 def datatable @datatable end |
Instance Method Details
#order(collection) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/models/effective/datatable_value_tool.rb', line 25 def order(collection) return collection unless ordered.present? collection = if ordered[:sort_method] datatable.dsl_tool.instance_exec(collection, datatable.order_direction, ordered, ordered[:index], &ordered[:sort_method]) else order_column(collection, datatable.order_direction, ordered, ordered[:index]) end raise 'sort method must return an Array' unless collection.kind_of?(Array) collection end |
#order_column(collection, direction, column, index) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/models/effective/datatable_value_tool.rb', line 39 def order_column(collection, direction, column, index) Rails.logger.info "VALUE TOOL: order_column :#{column.to_s} :#{direction} #{index}" if EffectiveDatatables.debug if direction == :asc collection.sort! do |x, y| x[index] <=> y[index] || x[index].to_s <=> y[index].to_s || 0 end else collection.sort! do |x, y| y[index] <=> x[index] || y[index].to_s <=> x[index].to_s || 0 end end collection end |
#ordered ⇒ Object
21 22 23 |
# File 'app/models/effective/datatable_value_tool.rb', line 21 def ordered @ordered ||= columns[datatable.order_name] end |
#paginate(collection) ⇒ Object
127 128 129 130 131 132 |
# File 'app/models/effective/datatable_value_tool.rb', line 127 def paginate(collection) page = [datatable.page.to_i - 1, 0].max per_page = datatable.per_page.to_i collection[(page * per_page)...((page * per_page) + per_page)] end |
#search(collection) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/models/effective/datatable_value_tool.rb', line 55 def search(collection) searched.each do |name, value| column = columns[name] collection = if column[:search_method] datatable.dsl_tool.instance_exec(collection, value, column, column[:index], &column[:search_method]) else search_column(collection, value, column, column[:index]) end raise 'search method must return an Array object' unless collection.kind_of?(Array) end collection end |
#search_column(collection, value, column, index) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/models/effective/datatable_value_tool.rb', line 71 def search_column(collection, value, column, index) Rails.logger.info "VALUE TOOL: search_column #{column.to_s} #{value} #{index}" if EffectiveDatatables.debug fuzzy = column[:search][:fuzzy] term = Effective::Attribute.new(column[:as]).parse(value, name: column[:name]) term_downcased = term.downcase if fuzzy && term.kind_of?(String) if term == 'nil' return (collection.select! { |row| row[index].nil? } || collection) end # See effective_resources gem search() method # relation.rb collection.select! do |row| case column[:as] when :duration if fuzzy && (term % 60 == 0) && value.to_s.include?('m') == false if term < 0 row[index] <= term && row[index] > (term - 60) else row[index] >= term && row[index] < (term + 60) end else row[index] == term end when :decimal, :currency if fuzzy && (term.round(0) == term) && value.to_s.include?('.') == false if term < 0 row[index] <= term && row[index] > (term - 1.0) else row[index] >= term && row[index] < (term + 1.0) end else row[index] == term end when :resource Array(row[index]).any? do |resource| if term.kind_of?(Integer) && resource.respond_to?(:id) resource.id == term || resource.to_param == term elsif term.kind_of?(Array) && resource.respond_to?(:id) term.any? { |term| resource.id == term || resource.to_param == term || resource.to_param == value } else fuzzy ? resource.to_s.downcase == term_downcased : resource.to_s == term end end when :string, :text, :email if fuzzy row[index].to_s.downcase.include?(term_downcased) else row[index] == term end else row[index] == term end end || collection end |
#searched ⇒ Object
17 18 19 |
# File 'app/models/effective/datatable_value_tool.rb', line 17 def searched @searched ||= datatable.search.select { |name, _| columns.key?(name) } end |
#size(collection) ⇒ Object
134 135 136 |
# File 'app/models/effective/datatable_value_tool.rb', line 134 def size(collection) collection.size end |