A customizable method for restricting an attribute to a set of choices. By default, attr_enumerator
will create:
-
A validation requiring the attribute value to be one of the choices
-
A class constant containing all choices
-
Instance methods for each choice, returning
true
if the attribute is that value andfalse
otherwise -
Scopes for each choice that retrieve records where the attribute is that value (only applicable when used with
ActiveRecord
)
AttrEnumerator
is automatically included in ActiveRecord::Base
and is available for use in ActiveRecord
models.
class Car < ActiveRecord::Base attr_enumerator :color, ["red", "blue"] end
To use attr_enumerator
on a class that does not descend from ActiveRecord::Base
it must include ActiveModel::Validations
and provide an accessor to the attribute.
class Car include ActiveModel::Validations include AttrEnumerator attr_accessor :color attr_enumerator :color, ["red", "blue"] end
Options may be passed to attr_enumerator
to configure its behavior. For instance, use :create_constant => false
to prevent a class constant from being created.
class Car < ActiveRecord::Base attr_enumerator :color, ["red", "blue"], :create_constant => false end
Options that may be passed directly to attr_enumerator
may also be configured globally. Options passed directly to attr_enumerator
will override global options.
AttrEnumerator::Options.configure do |config| config.create_constant = false end
-
create_constant
(default:true
)When
true
, a class constant containing an array of choices is created using the pluralized, uppercased attribute name. Set to a string or symbol to change the constant name orfalse
to not create the constant. -
create_methods
(default:true
)When
true
, create a method for each choice that returnstrue
if the attribute is that value andfalse
otherwise. Use theprefix
andsuffix
options to customize the method names. Set tofalse
to not create methods. -
create_scopes
(default:true
, ignored when not usingActiveRecord
)When
true
, create a scope for each choice that retrieves records where the attribute is that value. Use theprefix
andsuffix
options to customize the scope names. Set tofalse
to not create scopes. -
prefix
(default:true
)When
true
, prefixes created methods and scopes with the attribute name. Set to a string or symbol to change the prefix orfalse
to disable. -
suffix
(default:false
)See
prefix
. -
message
(default:"is invalid"
)Specifies the error message when the attribute is not a valid value. Use
%{value}
to refer to the value of the attribute, e.g."%{value} is not a valid choice"
. -
allow_nil
(default:false
)Allow the attribute to be
nil
. -
allow_blank
(default:false
)Allow the attribute to be blank.
-
if
(default: unused)Specifies a method, proc or string to call to determine if the validation should occur. See
ActiveModel::Validations
for more details. -
unless
Opposite of
if
.
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
-
Fork the project
-
Start a feature/bugfix branch
-
Commit and push until you are happy with your contribution
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.