Module: Enum::Helpers::EnumColumn
Instance Method Summary collapse
-
#yinum_column(attr, name_or_enum, options = {}, hash = nil) ⇒ Object
(also: #enum_column)
Bind a column to an enum by: Generating attribute reader and writer to convert to EnumValue.
Methods included from EnumAttribute
Methods included from EnumGenerator
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:
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 (attr, name_or_enum, = {}, hash = nil) # the first hash is either options or the hash if the options are missing hash, = , {} unless name_or_enum.is_a?(Enum) or hash # generating the enum attribute e = (attr, name_or_enum, .merge(:qualifier => [:scoped]), hash) # validation validates_inclusion_of attr, :in => e.values, :allow_nil => true if [:scoped] # generating scopes and questioning methods e.by_name.each do |n, ev| scope n, -> { where(attr => ev.value) } end end e end |