From 0a2b7fe56743d76fab95280ae39c2cd5fef8b7c2 Mon Sep 17 00:00:00 2001 From: hoverlover Date: Mon, 21 Feb 2011 15:40:01 -0600 Subject: [PATCH 1/5] Changed deprecated 'named_scope' method calls to 'scope'. --- lib/preferences.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/preferences.rb b/lib/preferences.rb index 23ae075..325f015 100644 --- a/lib/preferences.rb +++ b/lib/preferences.rb @@ -160,8 +160,8 @@ 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 From ea05dd351fc7c33036fc7ed1e270a1e9c6d4445a Mon Sep 17 00:00:00 2001 From: hoverlover Date: Mon, 21 Feb 2011 21:57:10 -0600 Subject: [PATCH 2/5] Now requiring preference.rb. --- lib/preferences.rb | 1 + preferences.gemspec | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/preferences.rb b/lib/preferences.rb index 325f015..fdfc24b 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. # 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} From 8668613c024d98476666256304591dced64a3fab Mon Sep 17 00:00:00 2001 From: hoverlover Date: Tue, 22 Feb 2011 15:42:28 -0600 Subject: [PATCH 3/5] Added ability to define accessible preferences. --- README.rdoc | 31 ++++++++++++++++++++++++ lib/preferences.rb | 24 ++++++++++++++++-- lib/preferences/preference_definition.rb | 6 ++++- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/README.rdoc b/README.rdoc index 8555197..a3dc3c7 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` or `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 fdfc24b..4c93d3b 100644 --- a/lib/preferences.rb +++ b/lib/preferences.rb @@ -163,7 +163,7 @@ def preference(name, *args) # Named scopes 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 @@ -172,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 @@ -223,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 From 691c1b1d1d8e524bad3483bec683a043c8fa55fa Mon Sep 17 00:00:00 2001 From: hoverlover Date: Tue, 22 Feb 2011 15:46:51 -0600 Subject: [PATCH 4/5] Fixed formatting of inline code in README. --- README.rdoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rdoc b/README.rdoc index a3dc3c7..4f9d25b 100644 --- a/README.rdoc +++ b/README.rdoc @@ -117,8 +117,8 @@ Write method: === Accessible preferences -If you want a preference to be accessible via the `attributes` or `attributes=` method on the -model, use the `accessible_preference` method: +If you want a preference to be accessible via the +attributes+ or +attributes=+ method on the +model, use the +accessible_preference+ method: class User < ActiveRecord::Base accessible_preference :hot_salsa From 58ae6d1776f7bdcaa7a89f0baed1769459f8e004 Mon Sep 17 00:00:00 2001 From: hoverlover Date: Tue, 22 Feb 2011 15:49:01 -0600 Subject: [PATCH 5/5] Fixed last bit of README formatting. --- README.rdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 4f9d25b..9651c45 100644 --- a/README.rdoc +++ b/README.rdoc @@ -117,7 +117,7 @@ Write method: === Accessible preferences -If you want a preference to be accessible via the +attributes+ or +attributes=+ method on the +If you want a preference to be accessible via the +attributes+ method on the model, use the +accessible_preference+ method: class User < ActiveRecord::Base