Merge pull request #16148 from rails/fix_json_autoload

Fix json autoload
This commit is contained in:
Godfrey Chan 2014-07-29 01:35:49 -07:00
commit 540bc3d9b1
3 changed files with 52 additions and 3 deletions

@ -1,3 +1,10 @@
* Fixed a compatibility issue with the `Oj` gem when cherry-picking the file
`active_support/core_ext/object/json` without requiring `active_support/json`.
Fixes #16131.
*Godfrey Chan*
* Make `Hash#with_indifferent_access` copy the default proc too.
*arthurnn*, *Xanders*

@ -162,7 +162,7 @@ def as_json(options = nil) #:nodoc:
class Time
def as_json(options = nil) #:nodoc:
if ActiveSupport.use_standard_json_time_format
if ActiveSupport::JSON::Encoding.use_standard_json_time_format
xmlschema(ActiveSupport::JSON::Encoding.time_precision)
else
%(#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
@ -172,7 +172,7 @@ def as_json(options = nil) #:nodoc:
class Date
def as_json(options = nil) #:nodoc:
if ActiveSupport.use_standard_json_time_format
if ActiveSupport::JSON::Encoding.use_standard_json_time_format
strftime("%Y-%m-%d")
else
strftime("%Y/%m/%d")
@ -182,7 +182,7 @@ def as_json(options = nil) #:nodoc:
class DateTime
def as_json(options = nil) #:nodoc:
if ActiveSupport.use_standard_json_time_format
if ActiveSupport::JSON::Encoding.use_standard_json_time_format
xmlschema(ActiveSupport::JSON::Encoding.time_precision)
else
strftime('%Y/%m/%d %H:%M:%S %z')

@ -0,0 +1,42 @@
require 'abstract_unit'
# These test cases were added to test that cherry-picking the json extensions
# works correctly, primarily for dependencies problems reported in #16131. They
# need to be executed in isolation to reproduce the scenario correctly, because
# other test cases might have already loaded additional dependencies.
class JsonCherryPickTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
def test_time_as_json
require_or_skip 'active_support/core_ext/object/json'
expected = Time.new(2004, 7, 25)
actual = Time.parse(expected.as_json)
assert_equal expected, actual
end
def test_date_as_json
require_or_skip 'active_support/core_ext/object/json'
expected = Date.new(2004, 7, 25)
actual = Date.parse(expected.as_json)
assert_equal expected, actual
end
def test_datetime_as_json
require_or_skip 'active_support/core_ext/object/json'
expected = DateTime.new(2004, 7, 25)
actual = DateTime.parse(expected.as_json)
assert_equal expected, actual
end
private
def require_or_skip(file)
require(file) || skip("'#{file}' was already loaded")
end
end