3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/that.rb', line 3
def that(query)
query = query.to_s
Rails.application.eager_load!
models = Module.constants.collect do |constant_name|
constant = eval(constant_name.to_s)
if not constant.nil? and constant.is_a? Class and constant.superclass == ActiveRecord::Base
constant
end
end
models.compact!
results = []
models.each do |model|
keys = model.respond_to?(:to_that) ? model.to_that : DEFAULT_TO_THAT_KEYS
keys.each do |key|
results += model.where(key => query) if model.column_names.include?(key.to_s)
end
end
results.length == 1 ? results.first : results
end
|