Prefered allows you to add preferences / settings to any model that extends ActiveRecord::Base in a Rails 3 project
You can let bundler to install Prefered by adding this line to your application’s Gemfile:
gem 'prefered'
And then execute:
bundle install
Or install it yourself as:
gem install prefered
Next you will need to run the generator which will create the db migration for you:
rails g prefered:preferences_migration
Then migrate your db:
rake db:migrate
Now your all installed you can add preferences to your active record model, for instance for a User model:
class User < ActiveRecord::Base has_preferences do preference :colour, :default => 'green' group :email preference :html, :default => false end end end
This would add the following to your user model:
user.preferences.colour #=> 'green' user.preferences.email.html #=> false
To change preferences you simply use a setter for each preference:
user.preferences.colour= 'yellow' user.preferences.email.html= false
And to persist them just save the user model:
user.save
Thanks to anyone who gives this a try, peace & love, Rob :)
Copyright © 2010 Robert Oles
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.