Module: Enumerations::ClassMethods

Defined in:
lib/enumerations.rb

Instance Method Summary collapse

Instance Method Details

#enumeration(name, options = {}) ⇒ Object

Create an enumeration for the symbol name. Options include foreign_key attribute and class_name

Example:

 class User < ActiveRecord::Base
   enumeration :role
 end

user.role => #<Enumerations::Value: @base=Role, @symbol=:admin...>

user.role = Role.staff


34
35
36
37
38
# File 'lib/enumerations.rb', line 34

def enumeration(name, options = {})
  reflection = Reflection.new(name, options)

  add_enumeration(reflection)
end

#fetch_foreign_key_values(reflection, *symbols) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/enumerations.rb', line 55

def fetch_foreign_key_values(reflection, *symbols)
  symbols.flatten.map do |symbol|
    enumeration_value = reflection.enumerator_class.find(symbol)

    enumeration_value &&
      enumeration_value.send(reflection.enumerator_class.primary_key || :symbol)
  end
end

#reflect_on_all_enumerationsObject

Output all the enumerations that this model has defined Returns an array of Reflection objects for all the enumerations in the class.

Example:

User.reflect_on_all_enumerations => # [
   #<Enumerations::Reflection: @name=:role...>,
   #<Enumerations::Reflection: @name=:status...>
]


51
52
53
# File 'lib/enumerations.rb', line 51

def reflect_on_all_enumerations
  _enumerations
end