Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Raise exception if unknown preference is accessed

  • Loading branch information...
commit 4b308d84b7d591f10e04baf5c05fb118a7d42ce7 1 parent fa57073
@obrie obrie authored
View
1  CHANGELOG.rdoc
@@ -1,5 +1,6 @@
== master
+* Raise exception if unknown preference is accessed
* Rename #set_preference to #write_preference
* Add caching of preference lookups
* Fix preferences being stored even if they didn't change
View
9 lib/preferences.rb
@@ -337,6 +337,7 @@ def preferences(*args)
def preferred?(name, group = nil)
name = name.to_s
group = group.is_a?(Symbol) ? group.to_s : group
+ assert_valid_preference(name)
value = preferred(name, group)
preference_definitions[name].query(value)
@@ -362,6 +363,7 @@ def preferred?(name, group = nil)
def preferred(name, group = nil)
name = name.to_s
group = group.is_a?(Symbol) ? group.to_s : group
+ assert_valid_preference(name)
if preferences_group(group).include?(name)
# Value for this group/name has been written, but not saved yet:
@@ -401,6 +403,7 @@ def preferred(name, group = nil)
def write_preference(name, value, group = nil)
name = name.to_s
group = group.is_a?(Symbol) ? group.to_s : group
+ assert_valid_preference(name)
unless changed_preferences_group(group).include?(name)
old = clone_preference_value(name, group)
@@ -424,6 +427,12 @@ def reload(*args)
end
private
+ # Asserts that the given name is a valid preference in this model. If it
+ # is not, then an ArgumentError exception is raised.
+ def assert_valid_preference(name)
+ raise(ArgumentError, "Unknown preference: #{name}") unless preference_definitions.include?(name)
+ end
+
# Gets the set of preferences identified by the given group
def preferences_group(group)
@preferences ||= {}
View
15 test/functional/preferences_test.rb
@@ -204,6 +204,11 @@ def setup
@user = create_user
end
+ def test_should_raise_exception_if_invalid_preference_read
+ exception = assert_raise(ArgumentError) { @user.preferred(:invalid) }
+ assert_equal 'Unknown preference: invalid', exception.message
+ end
+
def test_use_default_value_if_not_stored
assert_equal true, @user.preferred(:notifications)
end
@@ -321,6 +326,11 @@ def setup
@user = create_user
end
+ def test_should_raise_exception_if_invalid_preference_queried
+ exception = assert_raise(ArgumentError) { @user.preferred?(:invalid) }
+ assert_equal 'Unknown preference: invalid', exception.message
+ end
+
def test_should_be_true_if_present
@user.preferred_language = 'English'
assert_equal true, @user.preferred?(:language)
@@ -423,6 +433,11 @@ def setup
@user = create_user(:login => 'admin')
end
+ def test_should_raise_exception_if_invalid_preference_written
+ exception = assert_raise(ArgumentError) { @user.write_preference(:invalid, true) }
+ assert_equal 'Unknown preference: invalid', exception.message
+ end
+
def test_should_have_same_value_if_not_changed
@user.write_preference(:notifications, true)
assert_equal true, @user.preferred(:notifications)
Please sign in to comment.
Something went wrong with that request. Please try again.