diff --git a/README.rdoc b/README.rdoc
index 8555197..9651c45 100644
--- a/README.rdoc
+++ b/README.rdoc
@@ -115,6 +115,37 @@ Write method:
user.write_preference(:hot_salsa, false) # => false
user.write_preference(:language, "English") # => "English"
+=== Accessible preferences
+
+If you want a preference to be accessible via the +attributes+ method on the
+model, use the +accessible_preference+ method:
+
+ class User < ActiveRecord::Base
+ accessible_preference :hot_salsa
+ accessible_preference :two_percent_milk
+ end
+
+Now, you can easily update all the preferences at once from your controllers:
+
+In the view:
+
+ - form_for @user do |f|
+ = f.check_box :prefers_hot_salsa
+ = f.check_box :prefers_two_percent_milk
+
+In the controller:
+
+ UsersController < ApplicationController
+ def update
+ @user = User.find(params[:id])
+ @user.attributes = params[:user]
+
+ flash.notice = 'Saved preferences' if @user.save
+
+ render 'edit'
+ end
+ end
+
=== Accessing all preferences
To get the collection of all custom, stored preferences for a particular record,
diff --git a/lib/preferences.rb b/lib/preferences.rb
index 23ae075..4c93d3b 100644
--- a/lib/preferences.rb
+++ b/lib/preferences.rb
@@ -1,4 +1,5 @@
require 'preferences/preference_definition'
+require 'app/models/preference'
# Adds support for defining preferences on ActiveRecord models.
#
@@ -160,9 +161,9 @@ def preference(name, *args)
after_save :update_preferences
# Named scopes
- named_scope :with_preferences, lambda {|preferences| build_preference_scope(preferences)}
- named_scope :without_preferences, lambda {|preferences| build_preference_scope(preferences, true)}
-
+ scope :with_preferences, lambda {|preferences| build_preference_scope(preferences)}
+ scope :without_preferences, lambda {|preferences| build_preference_scope(preferences, true)}
+
extend Preferences::ClassMethods
include Preferences::InstanceMethods
end
@@ -171,6 +172,8 @@ def preference(name, *args)
name = name.to_s
definition = PreferenceDefinition.new(name, *args)
self.preference_definitions[name] = definition
+
+ attr_accessible :"prefers_#{name}" if definition.accessible?
# Create short-hand accessor methods, making sure that the name
# is method-safe in terms of what characters are allowed
@@ -222,8 +225,26 @@ def preference(name, *args)
definition
end
+
+ # Defines a preference that is accessible via the attributes method. This
+ # works by calling attr_accessible for the preference.
+ #
+ # Example:
+ #
+ # class User < ActiveRecord::Base
+ # accessible_preference :notifications
+ # end
+ #
+ # This will add attr_accessible :prefers_notifications to the
+ # User model.
+ #
+ def accessible_preference(name, *args)
+ options = args.extract_options!.dup
+ options.merge!({ :accessible => true })
+ preference name, *(args << options)
+ end
end
-
+
module ClassMethods #:nodoc:
# Generates the scope for looking under records with a specific set of
# preferences associated with them.
diff --git a/lib/preferences/preference_definition.rb b/lib/preferences/preference_definition.rb
index da800d4..24da7d1 100644
--- a/lib/preferences/preference_definition.rb
+++ b/lib/preferences/preference_definition.rb
@@ -2,10 +2,14 @@ module Preferences
# Represents the definition of a preference for a particular model
class PreferenceDefinition
# The data type for the content stored in this preference type
- attr_reader :type
+ attr_reader :type, :accessible
+ alias :accessible? :accessible
def initialize(name, *args) #:nodoc:
options = args.extract_options!
+
+ @accessible = !!options.delete(:accessible)
+
options.assert_valid_keys(:default, :group_defaults)
@type = args.first ? args.first.to_sym : :boolean
diff --git a/preferences.gemspec b/preferences.gemspec
index 4e20fad..baba3f2 100644
--- a/preferences.gemspec
+++ b/preferences.gemspec
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.email = %q{aaron@pluginaweek.org}
s.files = ["app/models", "app/models/preference.rb", "generators/preferences", "generators/preferences/USAGE", "generators/preferences/preferences_generator.rb", "generators/preferences/templates", "generators/preferences/templates/001_create_preferences.rb", "lib/preferences", "lib/preferences/preference_definition.rb", "lib/preferences.rb", "test/unit", "test/unit/preference_test.rb", "test/unit/preference_definition_test.rb", "test/app_root", "test/app_root/db", "test/app_root/db/migrate", "test/app_root/db/migrate/003_create_employees.rb", "test/app_root/db/migrate/004_migrate_preferences_to_version_1.rb", "test/app_root/db/migrate/002_create_cars.rb", "test/app_root/db/migrate/001_create_users.rb", "test/app_root/app", "test/app_root/app/models", "test/app_root/app/models/car.rb", "test/app_root/app/models/employee.rb", "test/app_root/app/models/user.rb", "test/app_root/app/models/manager.rb", "test/test_helper.rb", "test/factory.rb", "test/functional", "test/functional/preferences_test.rb", "CHANGELOG.rdoc", "init.rb", "LICENSE", "Rakefile", "README.rdoc"]
s.homepage = %q{http://www.pluginaweek.org}
- s.require_paths = ["lib"]
+ s.require_paths = ["lib", "."]
s.rubyforge_project = %q{pluginaweek}
s.rubygems_version = %q{1.3.5}
s.summary = %q{Adds support for easily creating custom preferences for ActiveRecord models}