From a66aeb84e215c8a6e365a7fd0197c5703a14f241 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Tue, 15 Jul 2014 08:43:18 -0700 Subject: Revert "Revert "Merge pull request #16059 from jenncoop/json-serialized-attr"" This reverts commit 6f3c64eeb1dc8288dae49f114aaf619adc7dcb7f. Conflicts: activerecord/CHANGELOG.md --- activerecord/test/cases/serialized_attribute_test.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases/serialized_attribute_test.rb') diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index 186a1a2ade..a8f667c732 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -3,10 +3,11 @@ require 'models/topic' require 'models/reply' require 'models/person' require 'models/traffic_light' +require 'models/post' require 'bcrypt' class SerializedAttributeTest < ActiveRecord::TestCase - fixtures :topics + fixtures :topics, :posts MyObject = Struct.new :attribute1, :attribute2 @@ -67,6 +68,19 @@ class SerializedAttributeTest < ActiveRecord::TestCase assert_equal(orig.content, clone.content) end + def test_serialized_json_attribute_returns_unserialized_value + Topic.serialize :content, JSON + my_post = posts(:welcome) + + t = Topic.new(content: my_post) + t.save! + t.reload + + assert_instance_of(Hash, t.content) + assert_equal(my_post.id, t.content["id"]) + assert_equal(my_post.title, t.content["title"]) + end + def test_serialized_attribute_declared_in_subclass hash = { 'important1' => 'value1', 'important2' => 'value2' } important_topic = ImportantTopic.create("important" => hash) -- cgit v1.2.3 From f4c8a4b71fcce46c3259240b50f30e134e14c4a2 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Mon, 14 Jul 2014 10:38:14 -0700 Subject: Merge pull request #16162 from chancancode/fix_json_coder Fixed JSON coder when loading NULL from DB --- activerecord/test/cases/serialized_attribute_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'activerecord/test/cases/serialized_attribute_test.rb') diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index a8f667c732..96d1e365f0 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -81,6 +81,24 @@ class SerializedAttributeTest < ActiveRecord::TestCase assert_equal(my_post.title, t.content["title"]) end + # This is to ensure that the JSON coder is behaving the same way as 4.0, but + # we can consider changing this in the future. + def test_json_db_null + Topic.serialize :content, JSON + + # Force a row to have a database NULL instead of a JSON "null" + id = Topic.connection.insert "INSERT INTO topics (content) VALUES(NULL)" + t = Topic.find(id) + + assert_nil t.content + + t.save! + + # On 4.0, re-saving a row with a database NULL will turn that into a JSON + # "null" + assert_equal 1, Topic.where('content = "null"').count + end + def test_serialized_attribute_declared_in_subclass hash = { 'important1' => 'value1', 'important2' => 'value2' } important_topic = ImportantTopic.create("important" => hash) -- cgit v1.2.3 From cd809fd8ce920b27f813b56907e9a603a2b91839 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Mon, 14 Jul 2014 11:11:59 -0700 Subject: Fixed SQL syntax for postgresql --- activerecord/test/cases/serialized_attribute_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/serialized_attribute_test.rb') diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index 96d1e365f0..d7e6d3465f 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -96,7 +96,7 @@ class SerializedAttributeTest < ActiveRecord::TestCase # On 4.0, re-saving a row with a database NULL will turn that into a JSON # "null" - assert_equal 1, Topic.where('content = "null"').count + assert_equal 1, Topic.where("content = 'null'").count end def test_serialized_attribute_declared_in_subclass -- cgit v1.2.3 From da6472c916be5645ea4d761e7cbf44d486ca07a8 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Tue, 15 Jul 2014 09:08:31 -0700 Subject: Document the change in `nil` handling for serialized attributes Also updated the test case to reflect that --- .../test/cases/serialized_attribute_test.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'activerecord/test/cases/serialized_attribute_test.rb') diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index d7e6d3465f..f8d87a3661 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -81,22 +81,25 @@ class SerializedAttributeTest < ActiveRecord::TestCase assert_equal(my_post.title, t.content["title"]) end - # This is to ensure that the JSON coder is behaving the same way as 4.0, but - # we can consider changing this in the future. - def test_json_db_null + def test_json_read_legacy_null Topic.serialize :content, JSON - # Force a row to have a database NULL instead of a JSON "null" - id = Topic.connection.insert "INSERT INTO topics (content) VALUES(NULL)" + # Force a row to have a JSON "null" instead of a database NULL (this is how + # null values are saved on 4.1 and before) + id = Topic.connection.insert "INSERT INTO topics (content) VALUES('null')" t = Topic.find(id) assert_nil t.content + end - t.save! + def test_json_read_db_null + Topic.serialize :content, JSON - # On 4.0, re-saving a row with a database NULL will turn that into a JSON - # "null" - assert_equal 1, Topic.where("content = 'null'").count + # Force a row to have a database NULL instead of a JSON "null" + id = Topic.connection.insert "INSERT INTO topics (content) VALUES(NULL)" + t = Topic.find(id) + + assert_nil t.content end def test_serialized_attribute_declared_in_subclass -- cgit v1.2.3