Module: EnumerateBy::Extensions::BaseConditions

Defined in:
lib/enumerate_by/extensions/base_conditions.rb

Overview

Adds support for using enumerators in dynamic finders and conditions. For example suppose the following models are defined:

class Color < ActiveRecord::Base
  enumerate_by :name

  bootstrap(
    {:id => 1, :name => 'red'},
    {:id => 2, :name => 'blue'},
    {:id => 3, :name => 'green'}
  )
end

class Car < ActiveRecord::Base
  belongs_to :color
end

Normally, looking up all cars associated with a particular color requires either a join or knowing the id of the color upfront:

Car.find(:all, :joins => :color, :conditions => {:colors => {:name => 'red}})
Car.find_by_color_id(1)

Instead of doing this manually, the color can be referenced directly via its enumerator like so:

# With dynamic finders
Car.find_by_color('red')

# With conditions
Car.all(:conditions => {:color => 'red'})

# With updates
Car.update_all(:color => 'red')

In the above examples, color is essentially treated like a normal attribute on the class, instead triggering the associated Color record to be looked up and replacing the condition with a color_id condition.

Note that this does not add an additional join on the colors table since the lookup of the color’s id should be relatively fast when it’s cached in-memory.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

:nodoc:



46
47
48
49
50
51
52
53
# File 'lib/enumerate_by/extensions/base_conditions.rb', line 46

def self.extended(base) #:nodoc:
  class << base
    alias_method_chain :construct_attributes_from_arguments, :enumerations
    alias_method_chain :sanitize_sql_hash_for_conditions, :enumerations
    alias_method_chain :sanitize_sql_hash_for_assignment, :enumerations
    alias_method_chain :all_attributes_exists?, :enumerations
  end
end

Instance Method Details

#all_attributes_exists_with_enumerations?(attribute_names) ⇒ Boolean

Make sure dynamic finders don’t fail since it won’t find the association name in its columns

Returns:

  • (Boolean)


82
83
84
85
86
87
# File 'lib/enumerate_by/extensions/base_conditions.rb', line 82

def all_attributes_exists_with_enumerations?(attribute_names)
  exists = all_attributes_exists_without_enumerations?(attribute_names)
  exists ||= attribute_names.all? do |name|
    column_methods_hash.include?(name.to_sym) || reflect_on_enumeration(name)
  end
end

#construct_attributes_from_arguments_with_enumerations(attribute_names, arguments) ⇒ Object

Add support for dynamic finders



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/enumerate_by/extensions/base_conditions.rb', line 56

def construct_attributes_from_arguments_with_enumerations(attribute_names, arguments)
  attributes = construct_attributes_from_arguments_without_enumerations(attribute_names, arguments)
  attribute_names.each_with_index do |name, idx|
    if options = enumerator_options_for(name, arguments[idx])
      attributes.delete(name)
      attributes.merge!(options)
    end
  end
  
  attributes
end

#sanitize_sql_hash_for_assignment_with_enumerations(*args) ⇒ Object

Sanitizes a hash of attribute/value pairs into SQL conditions for a SET clause.



75
76
77
78
# File 'lib/enumerate_by/extensions/base_conditions.rb', line 75

def sanitize_sql_hash_for_assignment_with_enumerations(*args)
  replace_enumerations_in_hash(args.first, false)
  sanitize_sql_hash_for_assignment_without_enumerations(*args)
end

#sanitize_sql_hash_for_conditions_with_enumerations(*args) ⇒ Object

Sanitizes a hash of attribute/value pairs into SQL conditions for a WHERE clause.



69
70
71
72
# File 'lib/enumerate_by/extensions/base_conditions.rb', line 69

def sanitize_sql_hash_for_conditions_with_enumerations(*args)
  replace_enumerations_in_hash(args.first)
  sanitize_sql_hash_for_conditions_without_enumerations(*args)
end