diff options
author | John Firebaugh <john_firebaugh@us.ibm.com> | 2011-02-21 16:17:30 -0800 |
---|---|---|
committer | John Firebaugh <john_firebaugh@us.ibm.com> | 2011-07-17 11:34:06 -0700 |
commit | da4e1faf6f77732844a0fec4cbc9967be3156523 (patch) | |
tree | 9019dba1412fe457bae33e11a98df87460ad0ce7 | |
parent | 2f00854d22a833e6a4afe7b12fa5209bc01c8f04 (diff) | |
download | rails-da4e1faf6f77732844a0fec4cbc9967be3156523.tar.gz rails-da4e1faf6f77732844a0fec4cbc9967be3156523.tar.bz2 rails-da4e1faf6f77732844a0fec4cbc9967be3156523.zip |
serializable_hash(:only => [], :methods => [:foo]) should work
-rw-r--r-- | activemodel/lib/active_model/serialization.rb | 11 | ||||
-rw-r--r-- | activemodel/test/cases/serialization_test.rb | 10 |
2 files changed, 14 insertions, 7 deletions
diff --git a/activemodel/lib/active_model/serialization.rb b/activemodel/lib/active_model/serialization.rb index 4a174cb62a..78516608fd 100644 --- a/activemodel/lib/active_model/serialization.rb +++ b/activemodel/lib/active_model/serialization.rb @@ -69,14 +69,11 @@ module ActiveModel def serializable_hash(options = nil) options ||= {} - only = Array.wrap(options[:only]).map(&:to_s) - except = Array.wrap(options[:except]).map(&:to_s) - attribute_names = attributes.keys.sort - if only.any? - attribute_names &= only - elsif except.any? - attribute_names -= except + if only = options[:only] + attribute_names &= Array.wrap(only).map(&:to_s) + elsif except = options[:except] + attribute_names -= Array.wrap(except).map(&:to_s) end method_names = Array.wrap(options[:methods]).map { |n| n if respond_to?(n.to_s) }.compact diff --git a/activemodel/test/cases/serialization_test.rb b/activemodel/test/cases/serialization_test.rb index 8cc1ccb1e7..05b906b601 100644 --- a/activemodel/test/cases/serialization_test.rb +++ b/activemodel/test/cases/serialization_test.rb @@ -42,4 +42,14 @@ class SerializationTest < ActiveModel::TestCase assert_equal expected , @user.serializable_hash(:methods => [:foo]) end + def test_method_serializable_hash_should_work_with_only_and_methods + expected = {:foo=>"i_am_foo"} + assert_equal expected , @user.serializable_hash(:only => [], :methods => [:foo]) + end + + def test_method_serializable_hash_should_work_with_except_and_methods + expected = {"gender"=>"male", :foo=>"i_am_foo"} + assert_equal expected , @user.serializable_hash(:except => [:name, :email], :methods => [:foo]) + end + end |