diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-06-22 16:25:40 -0600 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-06-26 06:45:57 -0300 |
commit | 14b1208dd313f643f72aa2d92874198342e9fe14 (patch) | |
tree | 82af9d0a1dbcaafb4061c50ffa6d1a595d339e95 /activerecord/test | |
parent | 2571c3f415ac0c6eacedbc199e31b1c77e7bacc4 (diff) | |
download | rails-14b1208dd313f643f72aa2d92874198342e9fe14.tar.gz rails-14b1208dd313f643f72aa2d92874198342e9fe14.tar.bz2 rails-14b1208dd313f643f72aa2d92874198342e9fe14.zip |
Encapsulate the creation of `Attribute` objects
This will make it less painful to add additional properties, which
should persist across writes, such as `name`.
Conflicts:
activerecord/lib/active_record/attribute_set.rb
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/attribute_set_test.rb | 36 | ||||
-rw-r--r-- | activerecord/test/cases/attribute_test.rb | 26 |
2 files changed, 61 insertions, 1 deletions
diff --git a/activerecord/test/cases/attribute_set_test.rb b/activerecord/test/cases/attribute_set_test.rb index df35a7b384..35caa0ddab 100644 --- a/activerecord/test/cases/attribute_set_test.rb +++ b/activerecord/test/cases/attribute_set_test.rb @@ -35,7 +35,7 @@ module ActiveRecord attributes[:bar].value duped = attributes.dup - duped[:foo] = Attribute.from_database(2, Type::Integer.new) + duped.write_from_database(:foo, 2) duped[:bar].value << 'bar' assert_equal 1, attributes[:foo].value @@ -120,6 +120,40 @@ module ActiveRecord assert_nil attributes.fetch_value(:bar) end + class MyType + def type_cast_from_user(value) + return if value.nil? + value + " from user" + end + + def type_cast_from_database(value) + return if value.nil? + value + " from database" + end + end + + test "write_from_database sets the attribute with database typecasting" do + builder = AttributeSet::Builder.new(foo: MyType.new) + attributes = builder.build_from_database + + assert_nil attributes.fetch_value(:foo) + + attributes.write_from_database(:foo, "value") + + assert_equal "value from database", attributes.fetch_value(:foo) + end + + test "write_from_user sets the attribute with user typecasting" do + builder = AttributeSet::Builder.new(foo: MyType.new) + attributes = builder.build_from_database + + assert_nil attributes.fetch_value(:foo) + + attributes.write_from_user(:foo, "value") + + assert_equal "value from user", attributes.fetch_value(:foo) + end + def attributes_with_uninitialized_key builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Float.new) builder.build_from_database(foo: '1.1') diff --git a/activerecord/test/cases/attribute_test.rb b/activerecord/test/cases/attribute_test.rb index 57dd2e9a5e..0adf9545b8 100644 --- a/activerecord/test/cases/attribute_test.rb +++ b/activerecord/test/cases/attribute_test.rb @@ -99,5 +99,31 @@ module ActiveRecord attribute = Attribute.from_database('a value', @type) attribute.dup end + + class MyType + def type_cast_from_user(value) + value + " from user" + end + + def type_cast_from_database(value) + value + " from database" + end + end + + test "with_value_from_user returns a new attribute with the value from the user" do + old = Attribute.from_database("old", MyType.new) + new = old.with_value_from_user("new") + + assert_equal "old from database", old.value + assert_equal "new from user", new.value + end + + test "with_value_from_database returns a new attribute with the value from the database" do + old = Attribute.from_user("old", MyType.new) + new = old.with_value_from_database("new") + + assert_equal "old from user", old.value + assert_equal "new from database", new.value + end end end |