Module: EnumerateBy::ClassMethods

Defined in:
lib/enumerate_by.rb

Instance Method Summary collapse

Instance Method Details

#enumeration?Boolean

Does this class define an enumeration? Always true.

Returns:

  • (Boolean)


116
117
118
# File 'lib/enumerate_by.rb', line 116

def enumeration?
  true
end

#find_all_by_enumerator(enumerators) ⇒ Object

Finds records with the given enumerators.

For example,

Color.find_all_by_enumerator('red', 'green')  # => [#<Color id: 1, name: "red">, #<Color id: 1, name: "green">]
Color.find_all_by_enumerator('invalid')       # => []


153
154
155
# File 'lib/enumerate_by.rb', line 153

def find_all_by_enumerator(enumerators)
  all(:conditions => {enumerator_attribute => enumerators})
end

#find_all_by_enumerator!(enumerators) ⇒ Object

Finds records with the given enumerators. If no record is found for a particular enumerator, then an ActiveRecord::RecordNotFound exception is raised.

For Example,

Color.find_all_by_enumerator!('red', 'green')   # => [#<Color id: 1, name: "red">, #<Color id: 1, name: "green">]
Color.find_all_by_enumerator!('invalid')        # => ActiveRecord::RecordNotFound: Couldn't find Color with name(s) "invalid"

To avoid raising an exception on invalid enumerators, use find_all_by_enumerator.



167
168
169
170
171
# File 'lib/enumerate_by.rb', line 167

def find_all_by_enumerator!(enumerators)
  records = find_all_by_enumerator(enumerators)
  missing = [enumerators].flatten - records.map(&:enumerator)
  missing.empty? ? records : raise(ActiveRecord::RecordNotFound, "Couldn't find #{name} with #{enumerator_attribute}(s) #{missing.map(&:inspect).to_sentence}")
end

#find_by_enumerator(enumerator) ⇒ Object

Finds the record that is associated with the given enumerator. The attribute that defines the enumerator is based on what was specified when calling enumerate_by.

For example,

Color.find_by_enumerator('red')     # => #<Color id: 1, name: "red">
Color.find_by_enumerator('invalid') # => nil


128
129
130
# File 'lib/enumerate_by.rb', line 128

def find_by_enumerator(enumerator)
  first(:conditions => {enumerator_attribute => enumerator})
end

#find_by_enumerator!(enumerator) ⇒ Object Also known as: []

Finds the record that is associated with the given enumerator. If no record is found, then an ActiveRecord::RecordNotFound exception is raised.

For example,

Color['red']      # => #<Color id: 1, name: "red">
Color['invalid']  # => ActiveRecord::RecordNotFound: Couldn't find Color with name "red"

To avoid raising an exception on invalid enumerators, use find_by_enumerator.



142
143
144
# File 'lib/enumerate_by.rb', line 142

def find_by_enumerator!(enumerator)
  find_by_enumerator(enumerator) || raise(ActiveRecord::RecordNotFound, "Couldn't find #{name} with #{enumerator_attribute} #{enumerator.inspect}")
end

#uncachedObject

Temporarily disables the enumeration cache (as well as the query cache) within the context of the given block if the enumeration is configured to allow caching.



191
192
193
194
195
196
197
# File 'lib/enumerate_by.rb', line 191

def uncached
  old = perform_enumerator_caching
  self.perform_enumerator_caching = false
  super
ensure
  self.perform_enumerator_caching = old
end