diff options
author | Cohen Carlisle <cohen.carlisle@gmail.com> | 2016-10-19 20:51:00 -0400 |
---|---|---|
committer | Cohen Carlisle <cohen.carlisle@gmail.com> | 2016-10-31 11:14:45 -0400 |
commit | 1d6b62026b3fbb17215b3a33c9606b5d9bd415dc (patch) | |
tree | 002aa675b65c76bdedc042ef6a3146e18620eed0 | |
parent | 7056273845d5f8307f80b98e0ae7a39e6e95ba0b (diff) | |
download | rails-1d6b62026b3fbb17215b3a33c9606b5d9bd415dc.tar.gz rails-1d6b62026b3fbb17215b3a33c9606b5d9bd415dc.tar.bz2 rails-1d6b62026b3fbb17215b3a33c9606b5d9bd415dc.zip |
allow ActiveRecord::Core#slice to use array arg
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/core.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 10 |
3 files changed, 15 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index c2f26bce70..95cdc6bc3f 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Allow `slice` to take an array of methods (without need for splatting). + + *Cohen Carlisle* + * Improved partial writes with HABTM and has many through associations to fire database query only if relation has been changed. diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index d24bd4efa8..0a9c06ba59 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -538,7 +538,7 @@ module ActiveRecord # Returns a hash of the given methods with their names as keys and returned values as values. def slice(*methods) - Hash[methods.map! { |method| [method, public_send(method)] }].with_indifferent_access + Hash[methods.flatten.map! { |method| [method, public_send(method)] }].with_indifferent_access end private diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 4dc6059d96..4e9d78de5d 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1428,6 +1428,16 @@ class BasicsTest < ActiveRecord::TestCase assert_nil hash["firm_name"] end + def test_slice_accepts_array_argument + attrs = { + title: "slice", + author_name: "@Cohen-Carlisle", + content: "accept arrays so I don't have to splat" + }.with_indifferent_access + topic = Topic.new(attrs) + assert_equal attrs, topic.slice(attrs.keys) + end + def test_default_values_are_deeply_dupped company = Company.new company.description << "foo" |