Skip to content

Commit

Permalink
Fix #preferences lookup not typecasting values [pluginaweek#2 state:r…
Browse files Browse the repository at this point in the history
…esolved]
  • Loading branch information
obrie committed Apr 17, 2010
1 parent 359df0a commit eeb1b86
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.rdoc
@@ -1,5 +1,7 @@
== master

* Fix #preferences lookup not typecasting values

== 0.4.1 / 2010-03-07

* Add support for per-group default preferences
Expand Down
5 changes: 4 additions & 1 deletion lib/preferences.rb
Expand Up @@ -317,7 +317,10 @@ def preferences(group = nil)
end
end

preferences.dup
preferences.inject({}) do |typed_preferences, (name, value)|
typed_preferences[name] = value.nil? ? value : preference_definitions[name].type_cast(value)
typed_preferences
end
end

# Queries whether or not a value is present for the given preference.
Expand Down
10 changes: 10 additions & 0 deletions test/functional/preferences_test.rb
Expand Up @@ -968,6 +968,11 @@ def test_should_use_unsaved_changes_over_stored_preferences
assert_equal e = {'notifications' => false, 'language' => 'English'}, @user.preferences
end

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

def test_should_cache_results
assert_queries(1) { @user.preferences }
assert_queries(0) { @user.preferences }
Expand Down Expand Up @@ -1019,6 +1024,11 @@ def test_should_merge_stored_preferences_with_unsaved_changes
assert_equal e = {'notifications' => false, 'language' => 'Spanish'}, @user.preferences(:chat)
end

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

def test_should_cache_results
assert_queries(1) { @user.preferences(:chat) }
assert_queries(0) { @user.preferences(:chat) }
Expand Down

0 comments on commit eeb1b86

Please sign in to comment.