Module: Enja::ClassMethods

Defined in:
lib/enja.rb

Instance Method Summary collapse

Instance Method Details

#define_role_field(attribute_name = :role, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/enja.rb', line 12

def define_role_field(attribute_name = :role, options = {})
  enumerize_options = enja_default_options.merge!(options)

  extend ::Enumerize
  enumerize(attribute_name.to_sym, enumerize_options)
  enumerize_options[:in].each do |role|
    scope(role, -> { where(attribute_name => role) })
    define_method(:"#{role}!") { update!(attribute_name => role ) }
  end

  validates(attribute_name, presence: true)
end