Permalink
Browse files

Fix default values behaviour.

  • Loading branch information...
1 parent 6798a6d commit 8d8040e4325b7ab1654c07752c56e04e16f38cdf @kirill-lashuk kirill-lashuk committed May 10, 2011
Showing with 14 additions and 2 deletions.
  1. +1 −1 lib/serialized_attributes/serialized_attributes.rb
  2. +13 −1 test/simple_test.rb
@@ -41,7 +41,7 @@ def attribute(name, type, opts = {})
serialized_attributes_definition[name] = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, opts[:default], type.to_s, nil)
define_method("#{name.to_s}=".to_sym) { |value| @attributes[name] = value }
- define_method(name) { self.class.serialized_attributes_definition[name].type_cast(@attributes[name]) }
+ define_method(name) { self.class.serialized_attributes_definition[name].type_cast(@attributes.fetch(name, opts[:default])) }
attr_accessible name if opts[:attr_accessible]
end
View
@@ -137,4 +137,16 @@ def test_serizalied_attribute_names_are_returned_by_the_class
def test_serizalied_attribute_names_are_returned_by_an_instance
assert Sprocket.new.serialized_attribute_names.sort == ['in_motion', 'size'].sort
end
-end
+
+ # => test that default value is proprely used in just created model
+ def test_default_value_in_just_create_model
+ assert_equal 'new default value', ModelSecond.new.custom_field_renamed
+ end
+
+ # => test that default value is proprely used in saved model
+ def test_default_value_in_save_model
+ model = ModelSecond.create
+ model.reload
+ assert_equal 'new default value', model.custom_field_renamed
+ end
+end

0 comments on commit 8d8040e

Please sign in to comment.