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

Merged
merged 1 commit into from Oct 31, 2016

Projects

None yet

7 participants

@Cohen-Carlisle
Contributor

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.

@schneems schneems was assigned by rails-bot Oct 20, 2016
@rails-bot

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
Contributor

@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
+ 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
rafaelfranca Oct 22, 2016 Member

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

@Cohen-Carlisle
Contributor

@rafaelfranca done and done

@Cohen-Carlisle
Contributor

Checking in on this PR.

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

Can you add a fullstop at the end.

Cohen Carlisle allow ActiveRecord::Core#slice to use array arg
1d6b620
@Cohen-Carlisle
Contributor

@prathamesh-sonpatki Done and rebased/squashed.

@prathamesh-sonpatki

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
Member
guilleiguaran commented Oct 31, 2016 edited

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