56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'app/models/effective/active_record_datatable_tool.rb', line 56
def search_column_with_defaults(collection, table_column, term)
column = table_column[:column]
sql_op = table_column[:filter][:sql_operation] || :where
case table_column[:type]
when :string, :text
if (table_column[:filter][:type] == :select && table_column[:filter][:fuzzy] != true) || sql_op != :where
if ['null', 'nil', nil].include?(term)
collection.public_send(sql_op, "#{column} = :term OR #{column} IS NULL", term: term)
else
collection.public_send(sql_op, "#{column} = :term", term: term)
end
else
collection.public_send(sql_op, "#{column} #{ilike} :term", term: "%#{term}%")
end
when :belongs_to_polymorphic
(type, id) = term.split('_')
if type.present? && id.present?
collection.public_send(sql_op, "#{column} = :id AND #{column.sub('_id', '_type')} = :type", id: id, type: type)
else
collection
end
when :has_many
reflection = collection.klass.reflect_on_association(table_column[:name].to_sym)
raise "unable to find #{collection.klass.name} :has_many :#{table_column[:name]} association" unless reflection
obj = reflection.build_association({})
klass = obj.class
inverse = reflection.inverse_of || klass.reflect_on_association(collection.table_name) || obj.class.reflect_on_association(collection.table_name.singularize)
raise "unable to find #{klass.name} has_many :#{collection.table_name} or belongs_to :#{collection.table_name.singularize} associations" unless inverse
ids = if [:select, :grouped_select].include?(table_column[:filter][:type])
inverse_ids = term.split(',').map { |term| (term = term.to_i) == 0 ? nil : term }.compact
return collection unless inverse_ids.present?
klass.where(id: inverse_ids).joins(inverse.name).pluck(inverse.foreign_key)
else
klass_columns = if (table_column[:column] == klass.table_name) klass.columns.map { |col| col.name if col.text? }.compact else
[table_column[:column].gsub("#{klass.table_name}.", '')] end
conditions = klass_columns.map { |col_name| "#{klass.table_name}.#{col_name} #{ilike} :term" }
klass.where(conditions.join(' OR '), term: "%#{term}%", num: term.to_i).joins(inverse.name).pluck(inverse.foreign_key)
end
collection.public_send(sql_op, id: ids)
when :has_and_belongs_to_many
reflection = collection.klass.reflect_on_association(table_column[:name].to_sym)
raise "unable to find #{collection.klass.name} :has_and_belongs_to_many :#{table_column[:name]} association" unless reflection
obj = reflection.build_association({})
klass = obj.class
inverse = reflection.inverse_of || klass.reflect_on_association(collection.table_name) || obj.class.reflect_on_association(collection.table_name.singularize)
raise "unable to find #{klass.name} has_and_belongs_to_many :#{collection.table_name} or belongs_to :#{collection.table_name.singularize} associations" unless inverse
ids = if [:select, :grouped_select].include?(table_column[:filter][:type])
inverse_ids = term.split(',').map { |term| (term = term.to_i) == 0 ? nil : term }.compact
return collection unless inverse_ids.present?
klass.where(id: inverse_ids).flat_map { |klass| (klass.send(inverse.name).pluck(:id) rescue []) }
else
klass_columns = if (table_column[:column] == klass.table_name) klass.columns.map { |col| col.name if col.text? }.compact else
[table_column[:column].gsub("#{klass.table_name}.", '')] end
conditions = klass_columns.map { |col_name| "#{klass.table_name}.#{col_name} #{ilike} :term" }
klass.where(conditions.join(' OR '), term: "%#{term}%", num: term.to_i).flat_map { |klass| (klass.send(inverse.name).pluck(:id) rescue []) }
end
collection.public_send(sql_op, id: ids)
when :obfuscated_id
if (deobfuscated_id = collection.deobfuscate(term)) == term collection.public_send(sql_op, "#{column} = :term", term: 0)
else
collection.public_send(sql_op, "#{column} = :term", term: deobfuscated_id)
end
when :effective_address
ids = Effective::Address
.where('addressable_type = ?', collection_class.name)
.where("address1 #{ilike} :term OR address2 #{ilike} :term OR city #{ilike} :term OR postal_code #{ilike} :term OR state_code = :code OR country_code = :code", term: "%#{term}%", code: term)
.pluck(:addressable_id)
collection.public_send(sql_op, id: ids)
when :effective_roles
collection.with_role(term)
when :datetime, :date
begin
digits = term.scan(/(\d+)/).flatten.map(&:to_i)
start_at = Time.zone.local(*digits)
case digits.length
when 1 end_at = start_at.end_of_year
when 2 end_at = start_at.end_of_month
when 3 end_at = start_at.end_of_day
when 4 end_at = start_at.end_of_hour
when 5 end_at = start_at.end_of_minute
when 6
end_at = start_at + 1.second
else
end_at = start_at
end
collection.public_send(sql_op, "#{column} >= :start_at AND #{column} <= :end_at", start_at: start_at, end_at: end_at)
rescue => e
collection
end
when :boolean
collection.public_send(sql_op, "#{column} = :term", term: [1, 'true', 'yes'].include?(term.to_s.downcase))
when :integer
collection.public_send(sql_op, "#{column} = :term", term: term.gsub(/\D/, '').to_i)
when :year
collection.public_send(sql_op, "EXTRACT(YEAR FROM #{column}) = :term", term: term.to_i)
when :price
price_in_cents = (term.gsub(/[^0-9|\.]/, '').to_f * 100.0).to_i
collection.public_send(sql_op, "#{column} = :term", term: price_in_cents)
when :currency, :decimal
collection.public_send(sql_op, "#{column} = :term", term: term.gsub(/[^0-9|\.]/, '').to_f)
else
collection.public_send(sql_op, "#{column} = :term", term: term)
end
end
|