diff options
author | Godfrey Chan <godfreykfc@gmail.com> | 2013-11-05 19:22:03 -0800 |
---|---|---|
committer | Godfrey Chan <godfreykfc@gmail.com> | 2013-11-05 19:22:03 -0800 |
commit | 134c1156dd5713da41c62ff798fe3979723e64cc (patch) | |
tree | 6e7f9540dafbec02d4ca6c2a7e75c637d86e1f1c /activesupport/lib | |
parent | 44406d1e77061ce22effaae4698918c1f9f6271a (diff) | |
download | rails-134c1156dd5713da41c62ff798fe3979723e64cc.tar.gz rails-134c1156dd5713da41c62ff798fe3979723e64cc.tar.bz2 rails-134c1156dd5713da41c62ff798fe3979723e64cc.zip |
Fixed Object#as_json and Struct#as_json with options
These methods now takes the same options as Hash#as_json, for example:
struct = Struct.new(:foo, :bar).new
struct.foo = "hello"
struct.bar = "world"
json = struct.as_json(only: [:foo]) # => {foo: "hello"}
This is extracted from PR #11728 from @sergiocampama, see also the
discussion in #11460.
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/json.rb | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/json.rb b/activesupport/lib/active_support/core_ext/object/json.rb index 554da1a2aa..c50cd57c2c 100644 --- a/activesupport/lib/active_support/core_ext/object/json.rb +++ b/activesupport/lib/active_support/core_ext/object/json.rb @@ -20,16 +20,16 @@ end class Object def as_json(options = nil) #:nodoc: if respond_to?(:to_hash) - to_hash + to_hash.as_json(options) else - instance_values + instance_values.as_json(options) end end end class Struct #:nodoc: def as_json(options = nil) - Hash[members.zip(values)] + Hash[members.zip(values)].as_json(options) end end |