aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md7
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb20
-rw-r--r--activesupport/lib/active_support/core_ext/struct.rb6
-rw-r--r--activesupport/lib/active_support/json/encoding.rb2
-rw-r--r--activesupport/lib/active_support/test_case.rb8
-rw-r--r--activesupport/lib/active_support/testing/mocha_module.rb22
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb4
-rw-r--r--activesupport/test/core_ext/struct_test.rb10
-rw-r--r--activesupport/test/json/encoding_test.rb18
9 files changed, 60 insertions, 37 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index dc84b44dec..2bbc41296f 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,5 +1,12 @@
## Rails 4.0.0 (unreleased) ##
+* Simplify mocha integration and remove monkey-patches, bumping mocha to 0.13.0. *James Mead*
+
+* `#as_json` isolates options when encoding a hash.
+ Fix #8182
+
+ *Yves Senn*
+
* Deprecate Hash#diff in favor of MiniTest's #diff. *Steve Klabnik*
* Kernel#capture can catch output from subprocesses *Dmitry Vorotilin*
diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb
index 0e7bb179bf..85b0e10be2 100644
--- a/activesupport/lib/active_support/core_ext/hash/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -94,17 +94,17 @@ class Hash
private
def typecast_xml_value(value)
- case value.class.to_s
- when 'Hash'
+ case value
+ when Hash
if value['type'] == 'array'
_, entries = Array.wrap(value.detect { |k,v| not v.is_a?(String) })
if entries.nil? || (c = value['__content__'] && c.blank?)
[]
else
- case entries.class.to_s # something weird with classes not matching here. maybe singleton methods breaking is_a?
- when 'Array'
+ case entries # something weird with classes not matching here. maybe singleton methods breaking is_a?
+ when Array
entries.collect { |v| typecast_xml_value(v) }
- when 'Hash'
+ when Hash
[typecast_xml_value(entries)]
else
raise "can't typecast #{entries.inspect}"
@@ -135,10 +135,10 @@ class Hash
# how multipart uploaded files from HTML appear
xml_value['file'].is_a?(StringIO) ? xml_value['file'] : xml_value
end
- when 'Array'
+ when Array
value.map! { |i| typecast_xml_value(i) }
value.length > 1 ? value : value.first
- when 'String'
+ when String
value
else
raise "can't typecast #{value.class.name} - #{value.inspect}"
@@ -146,10 +146,10 @@ class Hash
end
def unrename_keys(params)
- case params.class.to_s
- when 'Hash'
+ case params
+ when Hash
Hash[params.map { |k,v| [k.to_s.tr('-', '_'), unrename_keys(v)] } ]
- when 'Array'
+ when Array
params.map { |v| unrename_keys(v) }
else
params
diff --git a/activesupport/lib/active_support/core_ext/struct.rb b/activesupport/lib/active_support/core_ext/struct.rb
new file mode 100644
index 0000000000..c2c30044f2
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/struct.rb
@@ -0,0 +1,6 @@
+# Backport of Struct#to_h from Ruby 2.0
+class Struct # :nodoc:
+ def to_h
+ Hash[members.zip(values)]
+ end
+end unless Struct.instance_methods.include?(:to_h)
diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb
index f65c831e04..7a5c351ca8 100644
--- a/activesupport/lib/active_support/json/encoding.rb
+++ b/activesupport/lib/active_support/json/encoding.rb
@@ -65,7 +65,7 @@ module ActiveSupport
# they can detect circular references.
options.merge(:encoder => self)
else
- options
+ options.dup
end
end
diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb
index 8b06739b7f..c96ad17aba 100644
--- a/activesupport/lib/active_support/test_case.rb
+++ b/activesupport/lib/active_support/test_case.rb
@@ -5,16 +5,18 @@ require 'active_support/testing/setup_and_teardown'
require 'active_support/testing/assertions'
require 'active_support/testing/deprecation'
require 'active_support/testing/isolation'
-require 'active_support/testing/mocha_module'
require 'active_support/testing/constant_lookup'
require 'active_support/core_ext/kernel/reporting'
require 'active_support/deprecation'
+begin
+ silence_warnings { require 'mocha/setup' }
+rescue LoadError
+end
+
module ActiveSupport
class TestCase < ::MiniTest::Spec
- include ActiveSupport::Testing::MochaModule
-
# Use AS::TestCase for the base class when describing a model
register_spec_type(self) do |desc|
Class === desc && desc < ActiveRecord::Base
diff --git a/activesupport/lib/active_support/testing/mocha_module.rb b/activesupport/lib/active_support/testing/mocha_module.rb
deleted file mode 100644
index 833dc867f0..0000000000
--- a/activesupport/lib/active_support/testing/mocha_module.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-module ActiveSupport
- module Testing
- module MochaModule
- begin
- require 'mocha/api'
- include Mocha::API
-
- def before_setup
- mocha_setup
- super
- end
-
- def after_teardown
- super
- mocha_verify
- mocha_teardown
- end
- rescue LoadError
- end
- end
- end
-end
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index 6746b58cd3..c378dcd01d 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -656,7 +656,9 @@ class HashExtTest < ActiveSupport::TestCase
end
def test_diff
- assert_equal({ :a => 2 }, { :a => 2, :b => 5 }.diff({ :a => 1, :b => 5 }))
+ assert_deprecated do
+ assert_equal({ :a => 2 }, { :a => 2, :b => 5 }.diff({ :a => 1, :b => 5 }))
+ end
end
def test_slice
diff --git a/activesupport/test/core_ext/struct_test.rb b/activesupport/test/core_ext/struct_test.rb
new file mode 100644
index 0000000000..0dff7b32d2
--- /dev/null
+++ b/activesupport/test/core_ext/struct_test.rb
@@ -0,0 +1,10 @@
+require 'abstract_unit'
+require 'active_support/core_ext/struct'
+
+class StructExt < ActiveSupport::TestCase
+ def test_to_h
+ x = Struct.new(:foo, :bar)
+ z = x.new(1, 2)
+ assert_equal({ foo: 1, bar: 2 }, z.to_h)
+ end
+end
diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb
index 7ed71f9abc..5bb2a45c87 100644
--- a/activesupport/test/json/encoding_test.rb
+++ b/activesupport/test/json/encoding_test.rb
@@ -22,6 +22,15 @@ class TestJSONEncoding < ActiveSupport::TestCase
end
end
+ class CustomWithOptions
+ attr_accessor :foo, :bar
+
+ def as_json(options={})
+ options[:only] = %w(foo bar)
+ super(options)
+ end
+ end
+
TrueTests = [[ true, %(true) ]]
FalseTests = [[ false, %(false) ]]
NilTests = [[ nil, %(null) ]]
@@ -248,6 +257,15 @@ class TestJSONEncoding < ActiveSupport::TestCase
assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
end
+ def test_to_json_should_not_keep_options_around
+ f = CustomWithOptions.new
+ f.foo = "hello"
+ f.bar = "world"
+
+ hash = {"foo" => f, "other_hash" => {"foo" => "other_foo", "test" => "other_test"}}
+ assert_equal(%({"foo":{"foo":"hello","bar":"world"},"other_hash":{"foo":"other_foo","test":"other_test"}}), hash.to_json)
+ end
+
def test_struct_encoding
Struct.new('UserNameAndEmail', :name, :email)
Struct.new('UserNameAndDate', :name, :date)