Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit 4f9c7655f84343ff9ec432bfdab699ac7706a6b7 1 parent 949894e
@gravis gravis authored rds committed
View
2  lib/preferences.rb
@@ -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
View
25 test/functional/preferences_test.rb
@@ -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
View
23 test/unit/preference_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.