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.
- #obj_to_value(obj, column, row = nil) ⇒ Object
- #order(collection) ⇒ Object
- #order_column(collection, direction, column, index) ⇒ Object
- #ordered ⇒ Object
- #paginate(collection) ⇒ Object
- #search(collection) ⇒ Object
- #search_column(collection, original, 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
#obj_to_value(obj, column, row = nil) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'app/models/effective/datatable_value_tool.rb', line 170 def obj_to_value(obj, column, row = nil) return obj if column[:compute] # This matches format.rb. Probably should be refactored. if column[:format] datatable.dsl_tool.instance_exec(obj, row, &column[:format]) elsif column[:partial] obj.to_s elsif obj.respond_to?(column[:name]) obj.send(column[:name]) elsif column[:as] == :time && obj.respond_to?(:strftime) (@_column_as_time ||= Time.zone.now.beginning_of_day) + ((1.hour * obj.hour) + (1.minute * obj.min)) # For search/order by time else obj end end |
#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 54 55 56 57 58 59 |
# 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 = obj_to_value(x[index], column) y = obj_to_value(y[index], column) x <=> y || x.to_s <=> y.to_s || 0 end else collection.sort! do |x, y| x = obj_to_value(x[index], column) y = obj_to_value(y[index], column) y <=> x || y.to_s <=> x.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
159 160 161 162 163 164 |
# File 'app/models/effective/datatable_value_tool.rb', line 159 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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/effective/datatable_value_tool.rb', line 61 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, original, column, index) ⇒ Object
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'app/models/effective/datatable_value_tool.rb', line 77 def search_column(collection, original, column, index) Rails.logger.info "VALUE TOOL: search_column #{column.to_s} #{original} #{index}" if EffectiveDatatables.debug fuzzy = column[:search][:fuzzy] term = Effective::Attribute.new(column[:as]).parse(original, name: column[:name]) term_downcased = term.to_s.downcase # term == 'nil' rescue false is a Rails 4.1 fix, where you can't compare a TimeWithZone to 'nil' if (term == 'nil' rescue false) return collection.select! { |row| obj_to_value(row[index], column, row) == nil } || collection end # See effective_resources gem search() method # relation.rb collection.select! do |row| obj = row[index] value = obj_to_value(row[index], column, row) case column[:as] when :boolean if fuzzy term ? (obj == true) : (obj != true) else obj == term end when :datetime, :date end_at = ( case (original.to_s.scan(/(\d+)/).flatten).length when 1 ; term.end_of_year # Year when 2 ; term.end_of_month # Year-Month when 3 ; term.end_of_day # Year-Month-Day when 4 ; term.end_of_hour # Year-Month-Day Hour when 5 ; term.end_of_minute # Year-Month-Day Hour-Minute when 6 ; term + 1.second # Year-Month-Day Hour-Minute-Second else term end ) value >= term && value <= end_at when :time (value.hour == term.hour) && (term.min == 0 ? true : (value.min == term.min)) when :decimal, :currency if fuzzy && (term.round(0) == term) && original.to_s.include?('.') == false if term < 0 value <= term && value > (term - 1.0) else value >= term && value < (term + 1.0) end else value == term end when :duration if fuzzy && (term % 60 == 0) && original.to_s.include?('m') == false if term < 0 value <= term && value > (term - 60) else value >= term && value < (term + 60) end else value == term end when *datatable.association_macros, :resource Array(obj).any? do |resource| Array(term).any? do |term| matched = false if term.kind_of?(Integer) && resource.respond_to?(:id) && resource.respond_to?(:to_param) matched = (resource.id == term || resource.to_param == term) end matched ||= (fuzzy ? resource.to_s.downcase.include?(term.to_s.downcase) : resource.to_s == term) end end else # :string, :text, :email if fuzzy value.to_s.downcase.include?(term_downcased) else value == term || (value.to_s == term.to_s) end 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
166 167 168 |
# File 'app/models/effective/datatable_value_tool.rb', line 166 def size(collection) collection.size end |