Skip to content

A method for restricting an attribute to a set of choices.

License

Notifications You must be signed in to change notification settings

phene/attr_enumerator

 
 

Repository files navigation

attr_enumerator

A method for restricting an attribute to a set of choices.

An invocation of attr_enumerator will create:

  • A validation requiring the attribute value to be one of the provided choices

  • A class constant containing all valid 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)

Example

class Car < ActiveRecord::Base
  attr_enumerator :color, ['red', 'blue']
end

car = Car.new
car.color = 'red'

car.color_red?      # => true
car.color_blue?     # => false

Car::COLORS         # => ['red', 'blue']

car.save
Car.color_red       # => [<#Car>]

car.color = 'green'
car.valid?          # => false
car.errors          # => #<OrderedHash {:color=>["is invalid"]}>

With or without ActiveRecord

The AttrEnumerator module is automatically included in ActiveRecord::Base and is available for use in all ActiveRecord models.

To use attr_enumerator on a non-ActiveRecord model the class 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

An options hash may be passed as the last argument to attr_enumerator to configure its behavior. For example:

class Car < ActiveRecord::Base
  attr_enumerator :color, ['red', 'blue'], :constant => :PAINT_COLORS, :prefix => :colored
end

Car::PAINT_COLORS # => ['red', 'blue']

car = Car.new
car.color = 'red'
car.colored_red?  # => true

Options

  • constant (default: pluralized, uppercased attribute name)

    Set a custom constant name.

  • prefix (default: attribute name)

    Set a custom prefix for methods and scopes. Set to false to disable.

  • message (default: internationalized "is invalid")

    Set the validation 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)

    Set a method, proc or string to call to determine if the validation should occur. See ActiveModel::Validations for more details.

  • unless (default: unused)

    Set a method, proc or string to call to determine if the validation should not occur.

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.

  • Create a pull request

  • Let me know if you want to be included in CONTRIBUTORS.txt

About

A method for restricting an attribute to a set of choices.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%