Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 4f9c7655f84343ff9ec432bfdab699ac7706a6b7 1 parent 949894e
Philippe Lafoucrière gravis authored rds committed
2  lib/preferences.rb
@@ -417,7 +417,7 @@ def write_preference(name, value, group = nil)
417 417 end
418 418
419 419 value = convert_number_column_value(value) if preference_definitions[name].number?
420   - preferences_group(group)[name] = value
  420 + preferences_group(group)[name] = preference_definitions[name].type_cast(value)
421 421
422 422 value
423 423 end
25 test/functional/preferences_test.rb
@@ -129,6 +129,26 @@ def test_should_include_new_definitions_in_preference_definitions
129 129 end
130 130 end
131 131
  132 +class PreferencesTypeCasted < ModelPreferenceTest
  133 + def setup
  134 + @definition = User.preference :rate, :float, :default => 1.0
  135 + @user = new_user
  136 + end
  137 +
  138 + def test_should_have_float_type
  139 + assert_equal :float, @definition.type
  140 + end
  141 +
  142 + def test_should_only_have_default_preferences
  143 + assert_equal e = {'rate' => 1.0}, @user.preferences
  144 + end
  145 +
  146 + def test_should_type_cast_changed_values
  147 + @user.write_preference(:rate, "1.1")
  148 + assert_equal e = {'rate' => 1.1}, @user.preferences
  149 + end
  150 +end
  151 +
132 152 class PreferencesByDefaultTest < ModelPreferenceTest
133 153 def setup
134 154 @definition = User.preference :notifications
@@ -178,7 +198,7 @@ def setup
178 198 @user = new_user
179 199 end
180 200
181   - def test_should_have_boolean_type
  201 + def test_should_have_integer_type
182 202 assert_equal :integer, @definition.type
183 203 end
184 204
@@ -236,6 +256,7 @@ def test_should_not_have_any_stored_preferences
236 256 class PreferencesReaderTest < ModelPreferenceTest
237 257 def setup
238 258 User.preference :notifications, :default => true
  259 + User.preference :rate, :float, :default => 1.0
239 260 @user = create_user
240 261 end
241 262
@@ -261,6 +282,8 @@ def test_should_use_stored_value_if_stored
261 282 def test_should_type_cast_based_on_preference_definition
262 283 @user.write_preference(:notifications, 'false')
263 284 assert_equal false, @user.preferred(:notifications)
  285 + @user.write_preference(:rate, "1.2")
  286 + assert_equal 1.2, @user.preferred(:rate)
264 287 end
265 288
266 289 def test_should_cache_stored_values
23 test/unit/preference_test.rb
@@ -212,6 +212,29 @@ def teardown
212 212 end
213 213 end
214 214
  215 +class PreferenceWithFloatTypeTest < ActiveSupport::TestCase
  216 + def setup
  217 + User.preference :rate, :float, :default => 10.0
  218 + end
  219 +
  220 + def test_should_type_cast_nil_values
  221 + preference = new_preference(:name => 'rate', :value => nil)
  222 + assert_nil preference.value
  223 + end
  224 +
  225 + def test_should_type_cast_numeric_values
  226 + preference = new_preference(:name => 'rate', :value => 1.0)
  227 + assert_equal 1.0, preference.value
  228 +
  229 + preference.value = "1.1"
  230 + assert_equal 1.1, preference.value
  231 + end
  232 +
  233 + def teardown
  234 + User.preference_definitions.delete('rate')
  235 + end
  236 +end
  237 +
215 238 class PreferenceWithSTIOwnerTest < ActiveSupport::TestCase
216 239 def setup
217 240 @manager = create_manager

0 comments on commit 4f9c765

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