Permalink
Browse files

Rename preference_values hash to preferences

Rename preferences association to stored_preferences
  • Loading branch information...
1 parent b73fe27 commit 257880e058985ce803c4014fa20d2eeb156cc7d2 @obrie obrie committed Jun 21, 2008
Showing with 65 additions and 58 deletions.
  1. +6 −0 CHANGELOG
  2. +6 −6 README
  3. +1 −1 Rakefile
  4. +17 −16 lib/preferences.rb
  5. +33 −33 test/functional/preferences_test.rb
  6. +2 −2 test/unit/preference_test.rb
View
@@ -1,5 +1,11 @@
*SVN*
+*0.1.1* (June 20th, 2008)
+
+* Rename preference_values hash to preferences
+
+* Rename preferences association to stored_preferences
+
*0.1.0* (June 19th, 2008)
* Avoid string evaluation for dynamic methods
View
12 README
@@ -105,15 +105,15 @@ Write method:
=== Accessing all preferences
To get the collection of all custom, stored preferences for a particular record,
-you can access the +preferences+ has_many association which is automatically
+you can access the +stored_preferences+ has_many association which is automatically
generated:
- user.preferences
+ user.stored_preferences
In addition to this, you can get a hash of all stored preferences *and* default
-preferences, by accessing the +preference_values+ helper:
+preferences, by accessing the +preferences+ helper:
- user.preference_values # => {"language"=>"English", "color"=>nil}
+ user.preferences # => {"language"=>"English", "color"=>nil}
This hash will contain the value for every preference that has been defined for
the model, whether that's the default value or one that has been previously
@@ -151,8 +151,8 @@ preferences by name. For example,
user.preferred_color('automobiles') # => "red"
user.preferred_color('clothing') # => "tan"
- user.preference_values # => {"color"=>nil, "automobiles"=>{"color"=>"red"}, "clothing=>{"color=>"tan"}}
- user.preference_values('automobiles') # => {"color"=>"red"}
+ user.preferences # => {"color"=>nil, "automobiles"=>{"color"=>"red"}, "clothing=>{"color=>"tan"}}
+ user.preferences('automobiles') # => {"color"=>"red"}
=== Saving preferences
View
@@ -4,7 +4,7 @@ require 'rake/gempackagetask'
require 'rake/contrib/sshpublisher'
PKG_NAME = 'preferences'
-PKG_VERSION = '0.1.0'
+PKG_VERSION = '0.1.1'
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
RUBY_FORGE_PROJECT = 'pluginaweek'
View
@@ -53,7 +53,7 @@ module MacroMethods
#
# After the first preference is defined, the following associations are
# created for the model:
- # * +preferences+ - A collection of all the preferences specified for a record
+ # * +stored_preferences+ - A collection of all the custom preferences specified for a record
#
# == Generated shortcut methods
#
@@ -97,11 +97,12 @@ def preference(attribute, *args)
class_inheritable_hash :preference_definitions
self.preference_definitions = {}
- class_inheritable_hash :default_preference_values
- self.default_preference_values = {}
+ class_inheritable_hash :default_preferences
+ self.default_preferences = {}
- has_many :preferences,
- :as => :owner
+ has_many :stored_preferences,
+ :as => :owner,
+ :class_name => 'Preference'
after_save :update_preferences
@@ -112,7 +113,7 @@ def preference(attribute, *args)
attribute = attribute.to_s
definition = PreferenceDefinition.new(attribute, *args)
self.preference_definitions[attribute] = definition
- self.default_preference_values[attribute] = definition.default_value
+ self.default_preferences[attribute] = definition.default_value
# Create short-hand helper methods, making sure that the attribute
# is method-safe in terms of what characters are allowed
@@ -147,22 +148,22 @@ module InstanceMethods
#
# A user with no stored values:
# user = User.find(:first)
- # user.preference_values
+ # user.preferences
# => {"language"=>"English", "color"=>nil}
#
# A user with stored values for a particular group:
# user.preferred_color = 'red', 'cars'
- # user.preference_values
+ # user.preferences
# => {"language"=>"English", "color"=>nil, "cars"=>{"language=>"English", "color"=>"red"}}
#
# Getting preference values for the owning record:
- # user.preference_values(nil)
+ # user.preferences(nil)
# => {"language"=>"English", "color"=>nil}
#
# Getting preference values for a particular group:
- # user.preference_values('cars')
+ # user.preferences('cars')
# => {"language"=>"English", "color"=>"red"}
- def preference_values(*args)
+ def preferences(*args)
if args.any?
group = args.first
group_id, group_type = Preference.split_group(group)
@@ -172,12 +173,12 @@ def preference_values(*args)
end
# Find all of the stored preferences
- stored_preferences = preferences.find(:all, :conditions => conditions)
+ stored_preferences = self.stored_preferences.find(:all, :conditions => conditions)
# Hashify attribute -> value or group -> attribute -> value
- stored_preferences.inject(self.class.default_preference_values.dup) do |preferences, preference|
+ stored_preferences.inject(self.class.default_preferences.dup) do |preferences, preference|
if group = preference.group
- preference_group = preferences[group] ||= self.class.default_preference_values.dup
+ preference_group = preferences[group] ||= self.class.default_preferences.dup
else
preference_group = preferences
end
@@ -224,7 +225,7 @@ def preferred(attribute, group = nil)
value = @preference_values[attribute][group]
else
group_id, group_type = Preference.split_group(group)
- preference = preferences.find(:first, :conditions => {:attribute => attribute, :group_id => group_id, :group_type => group_type})
+ preference = stored_preferences.find(:first, :conditions => {:attribute => attribute, :group_id => group_id, :group_type => group_type})
value = preference ? preference.value : preference_definitions[attribute].default_value
end
@@ -264,7 +265,7 @@ def update_preferences
attributes = {:attribute => attribute, :group_id => group_id, :group_type => group_type}
# Find an existing preference or build a new one
- preference = preferences.find(:first, :conditions => attributes) || preferences.build(attributes)
+ preference = stored_preferences.find(:first, :conditions => attributes) || stored_preferences.build(attributes)
preference.value = value
preference.save!
end
@@ -32,8 +32,8 @@ def test_should_create_preference_definitions
assert User.respond_to?(:preference_definitions)
end
- def test_should_create_default_preferences_values
- assert User.respond_to?(:default_preference_values)
+ def test_should_create_default_preferences
+ assert User.respond_to?(:default_preferences)
end
def test_should_include_new_definitions_in_preference_definitions
@@ -42,7 +42,7 @@ def test_should_include_new_definitions_in_preference_definitions
def teardown
User.preference_definitions.delete('notifications')
- User.default_preference_values.delete('notifications')
+ User.default_preferences.delete('notifications')
end
end
@@ -71,7 +71,7 @@ def test_should_have_a_preferred_language
assert_equal 'English', @user.preferred_language
end
- def test_should_have_only_default_preference_values
+ def test_should_have_only_default_preferences
expected = {
'hot_salsa' => nil,
'dark_chocolate' => true,
@@ -80,7 +80,7 @@ def test_should_have_only_default_preference_values
'language' => 'English'
}
- assert_equal expected, @user.preference_values
+ assert_equal expected, @user.preferences
end
end
@@ -138,7 +138,7 @@ def test_should_be_able_to_use_generic_set_preference_method
def test_should_still_be_new_record_after_changing_preference
@user.preferred_color = 'blue'
assert @user.new_record?
- assert @user.preferences.empty?
+ assert @user.stored_preferences.empty?
end
end
@@ -147,12 +147,12 @@ def setup
@user = create_user
end
- def test_should_not_have_any_preferences
- assert @user.preferences.empty?
+ def test_should_not_have_any_stored_preferences
+ assert @user.stored_preferences.empty?
end
end
-class UserWithoutPreferencesTest < Test::Unit::TestCase
+class UserWithoutStoredPreferencesTest < Test::Unit::TestCase
def setup
@user = create_user
end
@@ -182,22 +182,22 @@ def test_should_not_save_record_after_changing_preference
user = User.find(@user.id)
assert_equal 'English', user.preferred_language
- assert user.preferences.empty?
+ assert user.stored_preferences.empty?
end
end
-class UserWithPreferencesTest < Test::Unit::TestCase
+class UserWithStoredPreferencesTest < Test::Unit::TestCase
def setup
@user = create_user
@user.preferred_language = 'Latin'
@user.save!
end
- def test_should_have_preferences
- assert_equal 1, @user.preferences.size
+ def test_should_have_stored_preferences
+ assert_equal 1, @user.stored_preferences.size
end
- def test_should_include_custom_and_default_preferences_in_preference_values
+ def test_should_include_custom_and_default_preferences_in_preferences
expected = {
'hot_salsa' => nil,
'dark_chocolate' => true,
@@ -206,21 +206,21 @@ def test_should_include_custom_and_default_preferences_in_preference_values
'language' => 'Latin'
}
- assert_equal expected, @user.preference_values
+ assert_equal expected, @user.preferences
end
def test_should_not_remove_preference_if_set_to_default
@user.preferred_language = 'English'
@user.save!
@user.reload
- assert_equal 1, @user.preferences.size
+ assert_equal 1, @user.stored_preferences.size
end
def test_should_not_remove_preference_if_set_to_nil
@user.preferred_language = nil
@user.save!
@user.reload
- assert_equal 1, @user.preferences.size
+ assert_equal 1, @user.stored_preferences.size
end
def test_should_not_save_preference_if_model_is_not_saved
@@ -236,7 +236,7 @@ def test_should_modify_existing_preferences_when_saved
@user.reload
assert_equal 'Spanish', @user.preferred_language
- assert_equal 1, @user.preferences.size
+ assert_equal 1, @user.stored_preferences.size
end
def test_should_add_new_preferences_when_saved
@@ -245,22 +245,22 @@ def test_should_add_new_preferences_when_saved
@user.reload
assert_equal 'blue', @user.preferred_color
- assert_equal 2, @user.preferences.size
+ assert_equal 2, @user.stored_preferences.size
end
end
-class UserWithPreferencesForBasicGroupsTest < Test::Unit::TestCase
+class UserWithStoredPreferencesForBasicGroupsTest < Test::Unit::TestCase
def setup
@user = create_user
@user.preferred_color = 'red', 'cars'
@user.save!
end
- def test_should_have_preferences
- assert_equal 1, @user.preferences.size
+ def test_should_have_stored_preferences
+ assert_equal 1, @user.stored_preferences.size
end
- def test_should_have_preference_values_for_group
+ def test_should_have_preferences_for_group
expected = {
'hot_salsa' => nil,
'dark_chocolate' => true,
@@ -276,7 +276,7 @@ def test_should_have_preference_values_for_group
}
}
- assert_equal expected, @user.preference_values
+ assert_equal expected, @user.preferences
end
def test_should_not_have_preference_without_group
@@ -293,7 +293,7 @@ def test_should_modify_existing_preferences_when_saved
@user.reload
assert_equal 'blue', @user.preferred_color('cars')
- assert_equal 1, @user.preferences.size
+ assert_equal 1, @user.stored_preferences.size
end
def test_should_be_able_to_differentiate_between_groups
@@ -303,11 +303,11 @@ def test_should_be_able_to_differentiate_between_groups
@user.reload
assert_equal 'red', @user.preferred_color('cars')
assert_equal 'blue', @user.preferred_color('boats')
- assert_equal 2, @user.preferences.size
+ assert_equal 2, @user.stored_preferences.size
end
end
-class UserWithPreferencesForActiveRecordGroupsTest < Test::Unit::TestCase
+class UserWithStoredPreferencesForActiveRecordGroupsTest < Test::Unit::TestCase
def setup
@car = create_car
@@ -316,11 +316,11 @@ def setup
@user.save!
end
- def test_should_have_preferences
- assert_equal 1, @user.preferences.size
+ def test_should_have_stored_preferences
+ assert_equal 1, @user.stored_preferences.size
end
- def test_should_have_preference_values_for_group
+ def test_should_have_preferences_for_group
expected = {
'hot_salsa' => nil,
'dark_chocolate' => true,
@@ -336,7 +336,7 @@ def test_should_have_preference_values_for_group
}
}
- assert_equal expected, @user.preference_values
+ assert_equal expected, @user.preferences
end
def test_should_not_have_preference_without_group
@@ -353,7 +353,7 @@ def test_should_modify_existing_preferences_when_saved
@user.reload
assert_equal 'blue', @user.preferred_color(@car)
- assert_equal 1, @user.preferences.size
+ assert_equal 1, @user.stored_preferences.size
end
def test_should_be_able_to_differentiate_between_groups
@@ -365,6 +365,6 @@ def test_should_be_able_to_differentiate_between_groups
@user.reload
assert_equal 'red', @user.preferred_color(@car)
assert_equal 'blue', @user.preferred_color(@different_car)
- assert_equal 2, @user.preferences.size
+ assert_equal 2, @user.stored_preferences.size
end
end
@@ -133,7 +133,7 @@ def test_should_not_have_a_group_association
def teardown
User.preference_definitions.delete('notifications')
- User.default_preference_values.delete('notifications')
+ User.default_preferences.delete('notifications')
end
end
@@ -186,6 +186,6 @@ def test_should_type_cast_boolean_values
def teardown
User.preference_definitions.delete('notifications')
- User.default_preference_values.delete('notifications')
+ User.default_preferences.delete('notifications')
end
end

0 comments on commit 257880e

Please sign in to comment.