Skip to content

Commit

Permalink
allow ActiveRecord::Core#slice to use array arg
Browse files Browse the repository at this point in the history
  • Loading branch information
Cohen-Carlisle committed Oct 31, 2016
1 parent 7056273 commit 1d6b620
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions 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.

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/core.rb
Expand Up @@ -538,7 +538,7 @@ def pretty_print(pp)

# 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
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -1428,6 +1428,16 @@ def test_slice
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"
Expand Down

0 comments on commit 1d6b620

Please sign in to comment.