1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

allow ActiveRecord::Core#slice to use array arg

This commit is contained in:
Cohen Carlisle 2016-10-19 20:51:00 -04:00
parent 7056273845
commit 1d6b62026b
3 changed files with 15 additions and 1 deletions

View file

@ -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.

View file

@ -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

View file

@ -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"