attr_enumerator

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 and false 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

Configuration

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

Options

  • 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 or false to not create the constant.

  • create_methods (default: true)

    When true, create a method for each choice that returns true if the attribute is that value and false otherwise. Use the prefix and suffix options to customize the method names. Set to false to not create methods.

  • create_scopes (default: true, ignored when not using ActiveRecord)

    When true, create a scope for each choice that retrieves records where the attribute is that value. Use the prefix and suffix options to customize the scope names. Set to false 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 or false 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.

Contributing to attr_enumerator

  • 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.