Module: Enum::Helpers::EnumColumn

Includes:
EnumAttribute
Included in:
ActiveRecord::Base
Defined in:
lib/enum/helpers/enum_column.rb

Instance Method Summary collapse

Methods included from EnumAttribute

#attr_yinum

Methods included from EnumGenerator

#yinum

Instance Method Details

#yinum_column(attr, name_or_enum, options = {}, hash = nil) ⇒ Object Also known as: enum_column

Bind a column to an enum by:

Generating attribute reader and writer to convert to EnumValue.
Creating a validation for the attribute so it must have valid enum values (allowing nil).
If :scoped => true, generates scopes and questioning methods for every name in the enum.

If given a enum name (a symbol) and hash, also creates the enum.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/enum/helpers/enum_column.rb', line 11

def yinum_column(attr, name_or_enum, options = {}, hash = nil)
  # the first hash is either options or the hash if the options are missing
  hash, options = options, {} unless name_or_enum.is_a?(Enum) or hash
  # generating the enum attribute
  e = attr_yinum(attr, name_or_enum, options.merge(:qualifier => options[:scoped]), hash)
  # validation
  validates_inclusion_of attr, :in => e.values, :allow_nil => true
  if options[:scoped]
    # generating scopes and questioning methods
    e.by_name.each do |n, ev|
      scope n, -> { where(attr => ev.value) }
    end
  end

  e
end