Module: That

Defined in:
lib/that.rb,
lib/that/version.rb

Constant Summary collapse

DEFAULT_TO_THAT_KEYS =
[:name, :login, :email]
VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#that(query) ⇒ Object Also known as: dat



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
  # Make sure all models are loaded
  Rails.application.eager_load!

  # Find out what symbols are AR subclasses
  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 = []
  # Query everything!
  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