Class: Effective::DatatableValueTool

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

Overview

The collection is an Array of Arrays

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datatable) ⇒ DatatableValueTool

Returns a new instance of DatatableValueTool.



9
10
11
12
13
14
15
16
17
# File 'app/models/effective/datatable_value_tool.rb', line 9

def initialize(datatable)
  @datatable = datatable

  if datatable.array_collection?
    @columns = datatable.columns
  else
    @columns = datatable.columns.reject { |_, col| col[:sql_column].present? || (col[:search_method].present? && col[:sql_column] != false) }
  end
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



7
8
9
# File 'app/models/effective/datatable_value_tool.rb', line 7

def columns
  @columns
end

#datatableObject (readonly)

Returns the value of attribute datatable.



6
7
8
# File 'app/models/effective/datatable_value_tool.rb', line 6

def datatable
  @datatable
end

Instance Method Details

#obj_to_value(obj, column, row = nil) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/models/effective/datatable_value_tool.rb', line 175

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[:as] == :belongs_to_polymorphic
    obj.send(column[:name]).to_s
  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



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/effective/datatable_value_tool.rb', line 27

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/effective/datatable_value_tool.rb', line 41

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

#orderedObject



23
24
25
# File 'app/models/effective/datatable_value_tool.rb', line 23

def ordered
  @ordered ||= columns[datatable.order_name]
end

#paginate(collection) ⇒ Object



164
165
166
167
168
169
# File 'app/models/effective/datatable_value_tool.rb', line 164

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



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

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



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
158
159
160
161
162
# File 'app/models/effective/datatable_value_tool.rb', line 79

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][:operation] == :matches)

  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)
    next if value.nil? || value == ''

    obj_equals = obj.respond_to?(column[:name]) && obj.send(column[:name]).to_s.downcase == term_downcased

    obj_equals || 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

#searchedObject



19
20
21
# File 'app/models/effective/datatable_value_tool.rb', line 19

def searched
  @searched ||= datatable.search.select { |name, _| columns.key?(name) }
end

#size(collection) ⇒ Object



171
172
173
# File 'app/models/effective/datatable_value_tool.rb', line 171

def size(collection)
  collection.size
end