Module: Preferences
- Defined in:
- lib/preferences.rb,
lib/preferences/engine.rb,
lib/preferences/version.rb,
lib/preferences/preference_definition.rb
Overview
Adds support for defining preferences on ActiveRecord models.
Saving preferences
Preferences are not automatically saved when they are set. You must save the record that the preferences were set on.
For example,
class User < ActiveRecord::Base
preference :notifications
end
u = User.new(:login => 'admin', :prefers_notifications => false)
u.save!
u = User.find_by_login('admin')
u.attributes = {:prefers_notifications => true}
u.save!
Validations
Since the generated accessors for a preference allow the preference to be treated just like regular ActiveRecord attributes, they can also be validated against in the same way. For example,
class User < ActiveRecord::Base
preference :color, :string
validates_presence_of :preferred_color
validates_inclusion_of :preferred_color, :in => %w(red green blue)
end
u = User.new
u.valid? # => false
u.errors.on(:preferred_color) # => "can't be blank"
u.preferred_color = 'white'
u.valid? # => false
u.errors.on(:preferred_color) # => "is not included in the list"
u.preferred_color = 'red'
u.valid? # => true
Defined Under Namespace
Modules: ClassMethods, InstanceMethods, MacroMethods Classes: Engine, PreferenceDefinition
Constant Summary collapse
- VERSION =
"1.6.0"