Skip to content

Commit

Permalink
Rename #set_preference to #write_preference
Browse files Browse the repository at this point in the history
  • Loading branch information
obrie committed Mar 7, 2010
1 parent d2fce18 commit fa57073
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
@@ -1,5 +1,6 @@
== master

* Rename #set_preference to #write_preference
* Add caching of preference lookups
* Fix preferences being stored even if they didn't change
* Release gems via rake-gemcutter instead of rubyforge
Expand Down
6 changes: 3 additions & 3 deletions README.rdoc
Expand Up @@ -112,8 +112,8 @@ Reader method:
user.preferred(:language) # => "English"

Write method:
user.set_preference(:hot_salsa, false) # => false
user.set_preference(:language, "English") # => "English"
user.write_preference(:hot_salsa, false) # => false
user.write_preference(:language, "English") # => "English"

=== Accessing all preferences

Expand Down Expand Up @@ -146,7 +146,7 @@ through an example:
car = Car.find(:first)

user.preferred_color = 'red', car
# user.set_preference(:color, 'red', car) # The generic way
# user.write_preference(:color, 'red', car) # The generic way

This will create a color preference of "red" for the given car. In this way,
you can have "color" preferences for different records.
Expand Down
16 changes: 8 additions & 8 deletions lib/preferences.rb
Expand Up @@ -108,10 +108,10 @@ module MacroMethods
# ...generates the following methods:
# * <tt>prefers_notifications?</tt> - Whether a value has been specified, i.e. <tt>record.prefers?(:notifications)</tt>
# * <tt>prefers_notifications</tt> - The actual value stored, i.e. <tt>record.prefers(:notifications)</tt>
# * <tt>prefers_notifications=(value)</tt> - Sets a new value, i.e. <tt>record.set_preference(:notifications, value)</tt>
# * <tt>prefers_notifications=(value)</tt> - Sets a new value, i.e. <tt>record.write_preference(:notifications, value)</tt>
# * <tt>preferred_notifications?</tt> - Whether a value has been specified, i.e. <tt>record.preferred?(:notifications)</tt>
# * <tt>preferred_notifications</tt> - The actual value stored, i.e. <tt>record.preferred(:notifications)</tt>
# * <tt>preferred_notifications=(value)</tt> - Sets a new value, i.e. <tt>record.set_preference(:notifications, value)</tt>
# * <tt>preferred_notifications=(value)</tt> - Sets a new value, i.e. <tt>record.write_preference(:notifications, value)</tt>
#
# Notice that there are two tenses used depending on the context of the
# preference. Conventionally, <tt>prefers_notifications?</tt> is better
Expand Down Expand Up @@ -179,7 +179,7 @@ def preference(name, *args)

# Writer
define_method("preferred_#{name}=") do |*args|
set_preference(*args.flatten.unshift(name))
write_preference(*args.flatten.unshift(name))
end
alias_method "prefers_#{name}=", "preferred_#{name}="

Expand Down Expand Up @@ -331,7 +331,7 @@ def preferences(*args)
# user.preferred?(:color, 'cars') # => true
# user.preferred?(:color, Car.first) # => true
#
# user.set_preference(:color, nil)
# user.write_preference(:color, nil)
# user.preferred(:color) # => nil
# user.preferred?(:color) # => false
def preferred?(name, group = nil)
Expand All @@ -357,7 +357,7 @@ def preferred?(name, group = nil)
# user.preferred(:color, 'cars') # => "red"
# user.preferred(:color, Car.first) # => "red"
#
# user.set_preference(:color, 'blue')
# user.write_preference(:color, 'blue')
# user.preferred(:color) # => "blue"
def preferred(name, group = nil)
name = name.to_s
Expand Down Expand Up @@ -393,12 +393,12 @@ def preferred(name, group = nil)
# == Examples
#
# user = User.find(:first)
# user.set_preference(:color, 'red') # => "red"
# user.write_preference(:color, 'red') # => "red"
# user.save!
#
# user.set_preference(:color, 'blue', Car.first) # => "blue"
# user.write_preference(:color, 'blue', Car.first) # => "blue"
# user.save!
def set_preference(name, value, group = nil)
def write_preference(name, value, group = nil)
name = name.to_s
group = group.is_a?(Symbol) ? group.to_s : group

Expand Down
62 changes: 31 additions & 31 deletions test/functional/preferences_test.rb
Expand Up @@ -424,29 +424,29 @@ def setup
end

def test_should_have_same_value_if_not_changed
@user.set_preference(:notifications, true)
@user.write_preference(:notifications, true)
assert_equal true, @user.preferred(:notifications)
end

def test_should_use_new_value_if_changed
@user.set_preference(:notifications, false)
@user.write_preference(:notifications, false)
assert_equal false, @user.preferred(:notifications)
end

def test_should_not_save_record_after_changing_preference
@user.login = 'test'
@user.set_preference(:notifications, false)
@user.write_preference(:notifications, false)

assert_equal 'admin', User.find(@user.id).login
end

def test_should_not_create_stored_preferences_immediately
@user.set_preference(:notifications, false)
@user.write_preference(:notifications, false)
assert @user.stored_preferences.empty?
end

def test_should_not_create_stored_preference_if_value_not_changed
@user.set_preference(:notifications, true)
@user.write_preference(:notifications, true)
@user.save!

assert_equal 0, @user.stored_preferences.count
Expand All @@ -455,28 +455,28 @@ def test_should_not_create_stored_preference_if_value_not_changed
def test_should_not_create_stored_integer_preference_if_typecast_not_changed
User.preference :age, :integer

@user.set_preference(:age, '')
@user.write_preference(:age, '')
@user.save!

assert_equal 0, @user.stored_preferences.count
end

def test_should_create_stored_preference_if_value_changed
@user.set_preference(:notifications, false)
@user.write_preference(:notifications, false)
@user.save!

assert_equal 1, @user.stored_preferences.count
end

def test_should_reset_unsaved_preferences_after_reload
@user.set_preference(:notifications, false)
@user.write_preference(:notifications, false)
@user.reload

assert_equal true, @user.preferred(:notifications)
end

def test_should_not_save_reset_preferences_after_reload
@user.set_preference(:notifications, false)
@user.write_preference(:notifications, false)
@user.reload
@user.save!

Expand All @@ -486,7 +486,7 @@ def test_should_not_save_reset_preferences_after_reload
def test_should_overwrite_existing_stored_preference_if_value_changed
preference = create_preference(:owner => @user, :name => 'notifications', :value => true)

@user.set_preference(:notifications, false)
@user.write_preference(:notifications, false)
@user.save!

preference.reload
Expand All @@ -496,7 +496,7 @@ def test_should_overwrite_existing_stored_preference_if_value_changed
def test_should_not_remove_preference_if_set_to_default
create_preference(:owner => @user, :name => 'notifications', :value => false)

@user.set_preference(:notifications, true)
@user.write_preference(:notifications, true)
@user.save!
@user.reload

Expand All @@ -508,7 +508,7 @@ def test_should_not_remove_preference_if_set_to_default
def test_should_not_remove_preference_if_set_to_nil
create_preference(:owner => @user, :name => 'notifications', :value => false)

@user.set_preference(:notifications, nil)
@user.write_preference(:notifications, nil)
@user.save!
@user.reload

Expand All @@ -526,31 +526,31 @@ def setup
end

def test_should_have_same_value_if_not_changed
@user.set_preference(:notifications, true, :chat)
@user.write_preference(:notifications, true, :chat)
assert_equal true, @user.preferred(:notifications, :chat)
end

def test_should_use_new_value_if_changed
@user.set_preference(:notifications, false, :chat)
@user.write_preference(:notifications, false, :chat)
assert_equal false, @user.preferred(:notifications, :chat)
end

def test_should_not_create_stored_preference_if_value_not_changed
@user.set_preference(:notifications, true, :chat)
@user.write_preference(:notifications, true, :chat)
@user.save!

assert_equal 0, @user.stored_preferences.count
end

def test_should_create_stored_preference_if_value_changed
@user.set_preference(:notifications, false, :chat)
@user.write_preference(:notifications, false, :chat)
@user.save!

assert_equal 1, @user.stored_preferences.count
end

def test_should_set_group_attributes_on_stored_preferences
@user.set_preference(:notifications, false, :chat)
@user.write_preference(:notifications, false, :chat)
@user.save!

preference = @user.stored_preferences.first
Expand All @@ -561,7 +561,7 @@ def test_should_set_group_attributes_on_stored_preferences
def test_should_overwrite_existing_stored_preference_if_value_changed
preference = create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => true)

@user.set_preference(:notifications, false, :chat)
@user.write_preference(:notifications, false, :chat)
@user.save!

preference.reload
Expand All @@ -579,31 +579,31 @@ def setup
end

def test_should_have_same_value_if_not_changed
@user.set_preference(:notifications, true, @car)
@user.write_preference(:notifications, true, @car)
assert_equal true, @user.preferred(:notifications, @car)
end

def test_should_use_new_value_if_changed
@user.set_preference(:notifications, false, @car)
@user.write_preference(:notifications, false, @car)
assert_equal false, @user.preferred(:notifications, @car)
end

def test_should_not_create_stored_preference_if_value_not_changed
@user.set_preference(:notifications, true, @car)
@user.write_preference(:notifications, true, @car)
@user.save!

assert_equal 0, @user.stored_preferences.count
end

def test_should_create_stored_preference_if_value_changed
@user.set_preference(:notifications, false, @car)
@user.write_preference(:notifications, false, @car)
@user.save!

assert_equal 1, @user.stored_preferences.count
end

def test_should_set_group_attributes_on_stored_preferences
@user.set_preference(:notifications, false, @car)
@user.write_preference(:notifications, false, @car)
@user.save!

preference = @user.stored_preferences.first
Expand All @@ -625,7 +625,7 @@ def test_should_only_have_defaults_if_nothing_customized
end

def test_should_merge_defaults_with_unsaved_changes
@user.set_preference(:notifications, false)
@user.write_preference(:notifications, false)
assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences
end

Expand All @@ -636,7 +636,7 @@ def test_should_merge_defaults_with_saved_changes

def test_should_merge_stored_preferences_with_unsaved_changes
create_preference(:owner => @user, :name => 'notifications', :value => false)
@user.set_preference(:language, 'Latin')
@user.write_preference(:language, 'Latin')
assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences
end

Expand Down Expand Up @@ -676,7 +676,7 @@ def test_should_only_have_defaults_if_nothing_customized
end

def test_should_merge_defaults_with_unsaved_changes
@user.set_preference(:notifications, false, :chat)
@user.write_preference(:notifications, false, :chat)
assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences(:chat)
end

Expand All @@ -687,7 +687,7 @@ def test_should_merge_defaults_with_saved_changes

def test_should_merge_stored_preferences_with_unsaved_changes
create_preference(:owner => @user, :group_type => 'chat', :name => 'notifications', :value => false)
@user.set_preference(:language, 'Latin', :chat)
@user.write_preference(:language, 'Latin', :chat)
assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences(:chat)
end

Expand Down Expand Up @@ -732,7 +732,7 @@ def test_should_only_have_defaults_if_nothing_customized
end

def test_should_merge_defaults_with_unsaved_changes
@user.set_preference(:notifications, false, @car)
@user.write_preference(:notifications, false, @car)
assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences(@car)
end

Expand All @@ -743,7 +743,7 @@ def test_should_merge_defaults_with_saved_changes

def test_should_merge_stored_preferences_with_unsaved_changes
create_preference(:owner => @user, :group_type => 'Car', :group_id => @car.id, :name => 'notifications', :value => false)
@user.set_preference(:language, 'Latin', @car)
@user.write_preference(:language, 'Latin', @car)
assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences(@car)
end
end
Expand All @@ -763,7 +763,7 @@ def test_should_only_have_defaults_if_nothing_customized
end

def test_should_merge_defaults_with_unsaved_changes
@user.set_preference(:notifications, false)
@user.write_preference(:notifications, false)
assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences(nil)
end

Expand All @@ -774,7 +774,7 @@ def test_should_merge_defaults_with_saved_changes

def test_should_merge_stored_preferences_with_unsaved_changes
create_preference(:owner => @user, :name => 'notifications', :value => false)
@user.set_preference(:language, 'Latin')
@user.write_preference(:language, 'Latin')
assert_equal e = {'notifications' => false, 'language' => 'Latin'}, @user.preferences(nil)
end
end
Expand Down

0 comments on commit fa57073

Please sign in to comment.