Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed deprecation warnings surrounding 'named_scope' #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 25 additions & 4 deletions lib/preferences.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'preferences/preference_definition'
require 'app/models/preference'

# Adds support for defining preferences on ActiveRecord models.
#
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -222,8 +225,26 @@ def preference(name, *args)

definition
end

# Defines a preference that is accessible via the <tt>attributes</tt> method. This
# works by calling <tt>attr_accessible</tt> for the preference.
#
# Example:
#
# class User < ActiveRecord::Base
# accessible_preference :notifications
# end
#
# This will add <tt>attr_accessible :prefers_notifications</tt> 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.
Expand Down
6 changes: 5 additions & 1 deletion lib/preferences/preference_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion preferences.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.email = %q{[email protected]}
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}
Expand Down