Permalink
Browse files

preferences now return a hash with type-casted values. should close #2

  • Loading branch information...
gravis authored and rds committed Apr 11, 2010
1 parent 949894e commit 4f9c7655f84343ff9ec432bfdab699ac7706a6b7
Showing with 48 additions and 2 deletions.
  1. +1 −1 lib/preferences.rb
  2. +24 −1 test/functional/preferences_test.rb
  3. +23 −0 test/unit/preference_test.rb
View
@@ -417,7 +417,7 @@ def write_preference(name, value, group = nil)
end
value = convert_number_column_value(value) if preference_definitions[name].number?
- preferences_group(group)[name] = value
+ preferences_group(group)[name] = preference_definitions[name].type_cast(value)
value
end
@@ -129,6 +129,26 @@ def test_should_include_new_definitions_in_preference_definitions
end
end
+class PreferencesTypeCasted < ModelPreferenceTest
+ def setup
+ @definition = User.preference :rate, :float, :default => 1.0
+ @user = new_user
+ end
+
+ def test_should_have_float_type
+ assert_equal :float, @definition.type
+ end
+
+ def test_should_only_have_default_preferences
+ assert_equal e = {'rate' => 1.0}, @user.preferences
+ end
+
+ def test_should_type_cast_changed_values
+ @user.write_preference(:rate, "1.1")
+ assert_equal e = {'rate' => 1.1}, @user.preferences
+ end
+end
+
class PreferencesByDefaultTest < ModelPreferenceTest
def setup
@definition = User.preference :notifications
@@ -178,7 +198,7 @@ def setup
@user = new_user
end
- def test_should_have_boolean_type
+ def test_should_have_integer_type
assert_equal :integer, @definition.type
end
@@ -236,6 +256,7 @@ def test_should_not_have_any_stored_preferences
class PreferencesReaderTest < ModelPreferenceTest
def setup
User.preference :notifications, :default => true
+ User.preference :rate, :float, :default => 1.0
@user = create_user
end
@@ -261,6 +282,8 @@ def test_should_use_stored_value_if_stored
def test_should_type_cast_based_on_preference_definition
@user.write_preference(:notifications, 'false')
assert_equal false, @user.preferred(:notifications)
+ @user.write_preference(:rate, "1.2")
+ assert_equal 1.2, @user.preferred(:rate)
end
def test_should_cache_stored_values
@@ -212,6 +212,29 @@ def teardown
end
end
+class PreferenceWithFloatTypeTest < ActiveSupport::TestCase
+ def setup
+ User.preference :rate, :float, :default => 10.0
+ end
+
+ def test_should_type_cast_nil_values
+ preference = new_preference(:name => 'rate', :value => nil)
+ assert_nil preference.value
+ end
+
+ def test_should_type_cast_numeric_values
+ preference = new_preference(:name => 'rate', :value => 1.0)
+ assert_equal 1.0, preference.value
+
+ preference.value = "1.1"
+ assert_equal 1.1, preference.value
+ end
+
+ def teardown
+ User.preference_definitions.delete('rate')
+ end
+end
+
class PreferenceWithSTIOwnerTest < ActiveSupport::TestCase
def setup
@manager = create_manager

0 comments on commit 4f9c765

Please sign in to comment.