Skip to content
Browse files

Add {preference}_changed?, {preference}_was, {preference}_changed, {p…

…reference}_will_change!, and reset_{preference}!
  • Loading branch information...
1 parent 1ed8cd2 commit 3a343070e3e54b510af401051bb42337c4bd817c @obrie obrie committed Mar 7, 2010
Showing with 232 additions and 3 deletions.
  1. +1 −0 CHANGELOG.rdoc
  2. +57 −3 lib/preferences.rb
  3. +174 −0 test/functional/preferences_test.rb
View
1 CHANGELOG.rdoc
@@ -1,5 +1,6 @@
== master
+* Add {preference}_changed?, {preference}_was, {preference}_changed, {preference}_will_change!, and reset_{preference}!
* Add #preferences_changed?, #preferences_changed, and #preference_changes
* Fix preferences that are reverted externally still getting stored
* Fix preference definition types not being used to typecast values
View
60 lib/preferences.rb
@@ -109,9 +109,21 @@ module MacroMethods
# * <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.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.write_preference(:notifications, value)</tt>
+ # * <tt>prefers_notifications_changed?</tt> - Whether the preference has unsaved changes
+ # * <tt>prefers_notifications_was</tt> - The last saved value for the preference
+ # * <tt>prefers_notifications_change</tt> - A list of [original_value, new_value] if the preference has changed
+ # * <tt>prefers_notifications_will_change!</tt> - Forces the preference to get updated
+ # * <tt>reset_prefers_notifications!</tt> - Reverts any unsaved changes to the preference
+ #
+ # ...and the equivalent +preferred+ methods:
+ # * <tt>preferred_notifications?</tt>
+ # * <tt>preferred_notifications</tt>
+ # * <tt>preferred_notifications=(value)</tt>
+ # * <tt>preferred_notifications_changed?</tt>
+ # * <tt>preferred_notifications_was</tt>
+ # * <tt>preferred_notifications_change</tt>
+ # * <tt>preferred_notifications_will_change!</tt>
+ # * <tt>reset_preferred_notifications!</tt>
#
# Notice that there are two tenses used depending on the context of the
# preference. Conventionally, <tt>prefers_notifications?</tt> is better
@@ -183,6 +195,32 @@ def preference(name, *args)
end
alias_method "prefers_#{name}=", "preferred_#{name}="
+ # Changes
+ define_method("preferred_#{name}_changed?") do |*group|
+ preference_changed?(name, group.first)
+ end
+ alias_method "prefers_#{name}_changed?", "preferred_#{name}_changed?"
+
+ define_method("preferred_#{name}_was") do |*group|
+ preference_was(name, group.first)
+ end
+ alias_method "prefers_#{name}_was", "preferred_#{name}_was"
+
+ define_method("preferred_#{name}_change") do |*group|
+ preference_change(name, group.first)
+ end
+ alias_method "prefers_#{name}_change", "preferred_#{name}_change"
+
+ define_method("preferred_#{name}_will_change!") do |*group|
+ preference_will_change!(name, group.first)
+ end
+ alias_method "prefers_#{name}_will_change!", "preferred_#{name}_will_change!"
+
+ define_method("reset_preferred_#{name}!") do |*group|
+ reset_preference!(name, group.first)
+ end
+ alias_method "reset_prefers_#{name}!", "reset_preferred_#{name}!"
+
definition
end
end
@@ -498,6 +536,22 @@ def preference_change(name, group)
[preferences_changed_group(group)[name], preferred(name, group)] if preference_changed?(name, group)
end
+ # Gets the last saved value for the given preference
+ def preference_was(name, group)
+ preference_changed?(name, group) ? preferences_changed_group(group)[name] : preferred(name, group)
+ end
+
+ # Forces the given preference to be saved regardless of whether the value
+ # is actually diferent
+ def preference_will_change!(name, group)
+ preferences_changed_group(group)[name] = clone_preference_value(name, group)
+ end
+
+ # Reverts any unsaved changes to the given preference
+ def reset_preference!(name, group)
+ write_preference(name, preferences_changed_group(group)[name], group) if preference_changed?(name, group)
+ end
+
# Determines whether the old value is different from the new value for the
# given preference. This will use the typecasted value to determine
# equality.
View
174 test/functional/preferences_test.rb
@@ -93,6 +93,46 @@ def test_should_create_prefers_writer
assert @user.respond_to?(:prefers_notifications=)
end
+ def test_should_create_preferred_changed_query
+ assert @user.respond_to?(:preferred_notifications_changed?)
+ end
+
+ def test_should_create_prefers_changed_query
+ assert @user.respond_to?(:prefers_notifications_changed?)
+ end
+
+ def test_should_create_preferred_was
+ assert @user.respond_to?(:preferred_notifications_was)
+ end
+
+ def test_should_create_prefers_was
+ assert @user.respond_to?(:prefers_notifications_was)
+ end
+
+ def test_should_create_preferred_change
+ assert @user.respond_to?(:preferred_notifications_change)
+ end
+
+ def test_should_create_prefers_change
+ assert @user.respond_to?(:prefers_notifications_change)
+ end
+
+ def test_should_create_preferred_will_change
+ assert @user.respond_to?(:preferred_notifications_will_change!)
+ end
+
+ def test_should_create_prefers_will_change
+ assert @user.respond_to?(:prefers_notifications_will_change!)
+ end
+
+ def test_should_create_preferred_reset
+ assert @user.respond_to?(:reset_preferred_notifications!)
+ end
+
+ def test_should_create_prefers_reset
+ assert @user.respond_to?(:reset_prefers_notifications!)
+ end
+
def test_should_include_new_definitions_in_preference_definitions
assert_equal e = {'notifications' => @definition}, User.preference_definitions
end
@@ -670,10 +710,18 @@ def test_should_query_preferences_changed
assert_equal true, @user.preferences_changed?
end
+ def test_should_query_preference_changed
+ assert_equal true, @user.prefers_notifications_changed?
+ end
+
def test_should_not_query_preferences_changed_for_group
assert_equal false, @user.preferences_changed?(:chat)
end
+ def test_should_not_query_preference_changed_for_group
+ assert_equal false, @user.prefers_notifications_changed?(:chat)
+ end
+
def test_should_have_preferences_changed
assert_equal ['notifications'], @user.preferences_changed
end
@@ -699,10 +747,26 @@ def test_should_not_build_same_preference_changes_result
assert_not_same @user.preference_changes, @user.preference_changes
end
+ def test_should_have_preference_change
+ assert_equal [true, false], @user.prefers_notifications_change
+ end
+
+ def test_should_have_preference_was
+ assert_equal true, @user.prefers_notifications_was
+ end
+
def test_should_not_have_preference_changes_for_group
assert_equal e = {}, @user.preference_changes(:chat)
end
+ def test_should_not_have_preference_change_for_group
+ assert_nil @user.prefers_notifications_change(:chat)
+ end
+
+ def test_should_have_preference_was_for_group
+ assert_equal true, @user.prefers_notifications_was(:chat)
+ end
+
def test_should_use_latest_value_for_preference_changes
@user.write_preference(:notifications, nil)
assert_equal e = {'notifications' => [true, nil]}, @user.preference_changes
@@ -736,10 +800,18 @@ def test_should_not_query_preferences_changed
assert_equal false, @user.preferences_changed?
end
+ def test_not_should_query_preference_changed
+ assert_equal false, @user.prefers_notifications_changed?
+ end
+
def test_should_query_preferences_changed_for_group
assert_equal true, @user.preferences_changed?(:chat)
end
+ def test_should_query_preference_changed_for_group
+ assert_equal true, @user.prefers_notifications_changed?(:chat)
+ end
+
def test_should_have_preferences_changed
assert_equal [], @user.preferences_changed
end
@@ -752,9 +824,25 @@ def test_should_have_preference_changes
assert_equal e = {}, @user.preference_changes
end
+ def test_should_not_have_preference_change
+ assert_nil @user.prefers_notifications_change
+ end
+
+ def test_should_have_preference_was
+ assert_equal true, @user.prefers_notifications_was
+ end
+
def test_should_not_have_preference_changes_for_group
assert_equal e = {'notifications' => [true, false]}, @user.preference_changes(:chat)
end
+
+ def test_should_have_preference_change_for_group
+ assert_equal [true, false], @user.prefers_notifications_change(:chat)
+ end
+
+ def test_should_have_preference_was_for_group
+ assert_equal true, @user.prefers_notifications_was(:chat)
+ end
end
class PreferencesAfterRevertPreferenceChangeTest < ModelPreferenceTest
@@ -779,6 +867,92 @@ def test_should_not_have_preference_changes
end
end
+class PreferencesAfterForcingChangeTest < ModelPreferenceTest
+ def setup
+ User.preference :notifications, :boolean, :default => true
+
+ @user = create_user
+ @user.prefers_notifications_will_change!
+ @user.save
+ end
+
+ def test_should_store_preference
+ assert_equal 1, @user.stored_preferences.count
+
+ preference = @user.stored_preferences.first
+ assert_equal nil, preference.group_type
+ assert_equal nil, preference.group_id
+ assert_equal true, preference.value
+ end
+end
+
+class PreferencesAfterForcingChangeForGroupTest < ModelPreferenceTest
+ def setup
+ User.preference :notifications, :boolean, :default => true
+ User.preference :language, :string, :default => 'English'
+
+ @user = create_user
+ @user.prefers_notifications_will_change!(:chat)
+ @user.save
+ end
+
+ def test_should_store_preference
+ assert_equal 1, @user.stored_preferences.count
+
+ preference = @user.stored_preferences.first
+ assert_equal 'chat', preference.group_type
+ assert_equal nil, preference.group_id
+ assert_equal true, preference.value
+ end
+
+ def test_should_use_cloned_value_for_tracking_old_value
+ old_value = @user.preferred(:language)
+ @user.preferred_language_will_change!
+
+ tracked_old_value = @user.preferred_language_was
+ assert_equal old_value, tracked_old_value
+ assert_not_same old_value, tracked_old_value
+ end
+end
+
+class PreferencesAfterResettingPreferenceTest < ModelPreferenceTest
+ def setup
+ User.preference :notifications, :boolean, :default => true
+
+ @user = create_user
+ @user.write_preference(:notifications, false)
+ @user.write_preference(:notifications, false, :chat)
+ @user.reset_prefers_notifications!
+ end
+
+ def test_should_revert_to_original_value
+ assert_equal true, @user.preferred(:notifications)
+ end
+
+ def test_should_not_reset_groups
+ assert_equal true, @user.preferred(:notifications, :chat)
+ end
+end
+
+class PreferencesAfterResettingPreferenceTest < ModelPreferenceTest
+ def setup
+ User.preference :notifications, :boolean, :default => true
+
+ @user = create_user
+ @user.write_preference(:notifications, false)
+ @user.write_preference(:notifications, false, :chat)
+ @user.reset_prefers_notifications!(:chat)
+ end
+
+ def test_should_revert_to_original_value
+ assert_equal true, @user.preferred(:notifications, :chat)
+ end
+
+ def test_should_not_reset_default_group
+ assert_equal false, @user.preferred(:notifications)
+ end
+end
+
class PreferencesLookupTest < ModelPreferenceTest
def setup
User.preference :notifications, :boolean, :default => true

0 comments on commit 3a34307

Please sign in to comment.
Something went wrong with that request. Please try again.