Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Revert "Merge pull request #16059 from jenncoop/json-serialized-attr"

This reverts commit a030977.

This needs more work before it would work correctly on master.
  • Loading branch information...
commit 6f3c64eeb1dc8288dae49f114aaf619adc7dcb7f 1 parent a030977
@chancancode chancancode authored
View
41 activerecord/CHANGELOG.md
@@ -1,44 +1,3 @@
-* Restore 4.0 behavior for using serialize attributes with `JSON` as coder.
-
- With 4.1.x, `serialize` started returning a string when `JSON` was passed as
- the second attribute. It will now return a hash as per previous versions.
-
- Example:
-
- class Post < ActiveRecord::Base
- serialize :comment, JSON
- end
-
- class Comment
- include ActiveModel::Model
- attr_accessor :category, :text
- end
-
- post = Post.create!
- post.comment = Comment.new(category: "Animals", text: "This is a comment about squirrels.")
- post.save!
-
- # 4.0
- post.comment # => {"category"=>"Animals", "text"=>"This is a comment about squirrels."}
-
- # 4.1 before
- post.comment # => "#<Comment:0x007f80ab48ff98>"
-
- # 4.1 after
- post.comment # => {"category"=>"Animals", "text"=>"This is a comment about squirrels."}
-
- When using `JSON` as the coder in `serialize`, Active Record will use the
- new `ActiveRecord::Coders::JSON` coder which delegates its `dump/load` to
- `ActiveSupport::JSON.encode/decode`. This ensures special objects are dumped
- correctly using the `#as_json` hook.
-
- To keep the previous behaviour, supply a custom coder instead
- ([example](https://gist.github.com/jenncoop/8c4142bbe59da77daa63)).
-
- Fixes #15594.
-
- *Jenn Cooper*
-
* Add a `:required` option to singular associations, providing a nicer
API for presence validations on associations.
View
1  activerecord/lib/active_record.rb
@@ -97,7 +97,6 @@ module ActiveRecord
module Coders
autoload :YAMLColumn, 'active_record/coders/yaml_column'
- autoload :JSON, 'active_record/coders/json'
end
module AttributeMethods
View
7 activerecord/lib/active_record/attribute_methods/serialization.rb
@@ -37,12 +37,7 @@ module ClassMethods
# serialize :preferences, Hash
# end
def serialize(attr_name, class_name_or_coder = Object)
- # When ::JSON is used, force it to go through the Active Support JSON encoder
- # to ensure special objects (e.g. Active Record models) are dumped correctly
- # using the #as_json hook.
- coder = if class_name_or_coder == ::JSON
- Coders::JSON
- elsif [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) }
+ coder = if [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) }
class_name_or_coder
else
Coders::YAMLColumn.new(class_name_or_coder)
View
13 activerecord/lib/active_record/coders/json.rb
@@ -1,13 +0,0 @@
-module ActiveRecord
- module Coders # :nodoc:
- class JSON # :nodoc:
- def self.dump(obj)
- ActiveSupport::JSON.encode(obj)
- end
-
- def self.load(json)
- ActiveSupport::JSON.decode(json)
- end
- end
- end
-end
View
16 activerecord/test/cases/serialized_attribute_test.rb
@@ -3,11 +3,10 @@
require 'models/reply'
require 'models/person'
require 'models/traffic_light'
-require 'models/post'
require 'bcrypt'
class SerializedAttributeTest < ActiveRecord::TestCase
- fixtures :topics, :posts
+ fixtures :topics
MyObject = Struct.new :attribute1, :attribute2
@@ -68,19 +67,6 @@ def test_serialized_attribute_calling_dup_method
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)
Please sign in to comment.
Something went wrong with that request. Please try again.