New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow ActiveRecord::Core#slice to use array arg #26838

Merged
merged 1 commit into from Oct 31, 2016

Conversation

Projects
None yet
7 participants
@Cohen-Carlisle
Contributor

Cohen-Carlisle commented Oct 20, 2016

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 like ActiveRecord::QueryMethods#select.

Other Information

Imagine you have some code as follows:

class User < ApplicationRecord
  PUBLIC_FIELDS = %i(foo bar baz).freeze

  scope :public_fooed, -> { select(PUBLIC_FIELDS).where(foo: true) }

  def publicize
   slice(*PUBLIC_FIELDS)
  end
end

It annoys me that to filter attributes at the model class level (while doing a ::select), you don't need to splat PUBLIC_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.

@rails-bot

This comment has been minimized.

rails-bot commented Oct 20, 2016

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.

@rafaelfranca

Could you add a test case?

@Cohen-Carlisle

This comment has been minimized.

Contributor

Cohen-Carlisle commented Oct 22, 2016

@rafaelfranca sure thing, let me know if 9f9b999 will suffice

@rafaelfranca

Only a small comment. After you fix it could you add a CHANGELOG entry and squash your commits? Thanks.

activerecord/test/cases/base_test.rb Outdated
content: "accept arrays so I don't have to splat"
}.with_indifferent_access
topic = Topic.new(attrs)
assert_equal topic.slice(attrs.keys), attrs

This comment has been minimized.

@rafaelfranca

rafaelfranca Oct 22, 2016

Member

it is the other way around. assert_equal attrs, topic.slice(attrs.keys)

@Cohen-Carlisle Cohen-Carlisle force-pushed the Cohen-Carlisle:activerecord-slice-array-args branch Oct 22, 2016

@Cohen-Carlisle

This comment has been minimized.

Contributor

Cohen-Carlisle commented Oct 22, 2016

@rafaelfranca done and done

@Cohen-Carlisle

This comment has been minimized.

Contributor

Cohen-Carlisle commented Oct 26, 2016

Checking in on this PR.

activerecord/CHANGELOG.md Outdated
@@ -1,3 +1,7 @@
* Allow `slice` to take an array of methods (without need for splatting)

This comment has been minimized.

@prathamesh-sonpatki

prathamesh-sonpatki Oct 31, 2016

Member

Can you add a fullstop at the end.

@Cohen-Carlisle Cohen-Carlisle force-pushed the Cohen-Carlisle:activerecord-slice-array-args branch to 1d6b620 Oct 31, 2016

@Cohen-Carlisle

This comment has been minimized.

Contributor

Cohen-Carlisle commented Oct 31, 2016

@prathamesh-sonpatki Done and rebased/squashed.

@prathamesh-sonpatki

This comment has been minimized.

Member

prathamesh-sonpatki commented Oct 31, 2016

Looks good, let's wait for the committer now 😄

@guilleiguaran guilleiguaran merged commit 3c9eb70 into rails:master Oct 31, 2016

1 of 2 checks passed

continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
codeclimate Code Climate didn't find any new or fixed issues.
Details
@guilleiguaran

This comment has been minimized.

Member

guilleiguaran commented Oct 31, 2016

thank you very much for this, I've missed having this in some occasions

@Cohen-Carlisle Cohen-Carlisle deleted the Cohen-Carlisle:activerecord-slice-array-args branch Oct 31, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment