allow ActiveRecord::Core#slice to use array arg #26838
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @schneems (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
Could you add a test case? |
@rafaelfranca sure thing, let me know if |
Only a small comment. After you fix it could you add a CHANGELOG entry and squash your commits? Thanks. |
content: "accept arrays so I don't have to splat" | ||
}.with_indifferent_access | ||
topic = Topic.new(attrs) | ||
assert_equal topic.slice(attrs.keys), attrs |
rafaelfranca
Oct 22, 2016
Member
it is the other way around. assert_equal attrs, topic.slice(attrs.keys)
it is the other way around. assert_equal attrs, topic.slice(attrs.keys)
@rafaelfranca done and done |
Checking in on this PR. |
@@ -1,3 +1,7 @@ | |||
* Allow `slice` to take an array of methods (without need for splatting) |
prathamesh-sonpatki
Oct 31, 2016
Member
Can you add a fullstop at the end.
Can you add a fullstop at the end.
1d6b620
@prathamesh-sonpatki Done and rebased/squashed. |
Looks good, let's wait for the committer now |
thank you very much for this, I've missed having this in some occasions |
Summary
This allows
ActiveRecord::Core#slice
to take an array of attributes to retain in addition to a splat of arguments, making it behave like likeActiveRecord::QueryMethods#select
.Other Information
Imagine you have some code as follows:
It annoys me that to filter attributes at the model class level (while doing a
::select
), you don't need to splatPUBLIC_FIELDS
, but you do when doing it at the model object level (with#slice
).Since ActiveRecord::Core#slice will never have attributes (or methods) that are arrays, we can safely flatten its arguments to make it behave like
::select
with regard to array arguments.