Sha256: a10d42ed3f73358325658e3dd38a441ae1a6d40cbc623b2f83d914b88a05237e
Contents?: true
Size: 1.52 KB
Versions: 1
Compression:
Stored size: 1.52 KB
Contents
# ActiveRecord::EnumeratedModel ActiveRecord::EnumeratedModel is an ActiveRecord extensions that allows you to create enumerated constants out of a model containing static data. Sometimes you have a model where values will not change. Consider the following: ```ruby class Role ADMIN = 1 EDITOR = 2 VIEWER = 3 end ``` We'll often do something like this in order to easily check values in our application. ```ruby unless @current_user.has?(Role::ADMIN) # some admin stuff end ``` Keeping this out of ActiveRecord prevents us from doing any kind of joining in the database and limits what `Role` can do. Wouldn't be cool if you could `Role::ADMIN` returned an ActiveRecord row? Role::ADMIN == #<Role name: "Admin"> This is what ActiveRecord::EnumeratedModel provides. Since we're dealing with constants here, ActiveRecord::EnumeratedModel also makes your model readonly. ## Installation Add this line to your application's Gemfile: gem 'activerecord-enumerated_model' And then execute: $ bundle Or install it yourself as: $ gem install activerecord-enumerated_model ## Usage Simply include `ActiveRecord::EnumeratedModel` in your model: ```ruby class StaticThing < ActiveRecord::Base include ActiveRecord::EnumeratedModel end ``` ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Added some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
activerecord-enumerated_model-0.1.2 | README.md |