1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix #to_json for BasicObject Enumerables

This commit is contained in:
Sammy Larbi 2013-04-20 13:06:22 -05:00
parent b4c96490ee
commit d75eeadf74
4 changed files with 40 additions and 12 deletions

View file

@ -1,3 +1,8 @@
* Ensure classes which `include Enumerable` get `#to_json` in addition to
`#as_json`.
*Sammy Larbi*
* Change the signature of `fetch_multi` to return a hash rather than an
array. This makes it consistent with the output of `read_multi`.

View file

@ -26,7 +26,7 @@ require 'active_support/core_ext/module/aliasing'
# bypassed completely. This means that as_json won't be invoked and the JSON gem will simply
# ignore any options it does not natively understand. This also means that ::JSON.{generate,dump}
# should give exactly the same results with or without active support.
[Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].each do |klass|
[Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass, Enumerable].each do |klass|
klass.class_eval do
def to_json_with_active_support_encoder(options = nil)
if options.is_a?(::JSON::State)

View file

@ -2,4 +2,4 @@ ActiveSupport::Deprecation.warn 'You have required `active_support/core_ext/obje
'This file will be removed in Rails 4.2. You should require `active_support/core_ext/object/json` ' \
'instead.'
require 'active_support/core_ext/object/json'
require 'active_support/core_ext/object/json'

View file

@ -327,12 +327,39 @@ class TestJSONEncoding < ActiveSupport::TestCase
assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
end
def test_enumerable_should_pass_encoding_options_to_children_in_as_json
people = [
{ :name => 'John', :address => { :city => 'London', :country => 'UK' }},
{ :name => 'Jean', :address => { :city => 'Paris' , :country => 'France' }}
People = Class.new(BasicObject) do
include Enumerable
def initialize()
@people = [
{ :name => 'John', :address => { :city => 'London', :country => 'UK' }},
{ :name => 'Jean', :address => { :city => 'Paris' , :country => 'France' }}
]
end
def each(*, &blk)
@people.each do |p|
yield p if blk
p
end.each
end
end
def test_enumerable_should_generate_json_with_as_json
json = People.new.as_json :only => [:address, :city]
expected = [
{ 'address' => { 'city' => 'London' }},
{ 'address' => { 'city' => 'Paris' }}
]
json = people.each.as_json :only => [:address, :city]
assert_equal(expected, json)
end
def test_enumerable_should_generate_json_with_to_json
json = People.new.to_json :only => [:address, :city]
assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
end
def test_enumerable_should_pass_encoding_options_to_children_in_as_json
json = People.new.each.as_json :only => [:address, :city]
expected = [
{ 'address' => { 'city' => 'London' }},
{ 'address' => { 'city' => 'Paris' }}
@ -342,11 +369,7 @@ class TestJSONEncoding < ActiveSupport::TestCase
end
def test_enumerable_should_pass_encoding_options_to_children_in_to_json
people = [
{ :name => 'John', :address => { :city => 'London', :country => 'UK' }},
{ :name => 'Jean', :address => { :city => 'Paris' , :country => 'France' }}
]
json = people.each.to_json :only => [:address, :city]
json = People.new.each.to_json :only => [:address, :city]
assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
end