Skip to content
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

activerecord: CollectionProxy#build can take Hash-like object #598

Merged
merged 3 commits into from
Jun 21, 2024

Conversation

tk0miya
Copy link
Contributor

@tk0miya tk0miya commented Jun 11, 2024

The current definition of #build, #craete and #create! methods of
ActiveRecord::Associations::CollectionProxy#build take a Hash object.

But they can also take Hash-like object. Usually, Rails apps pass an
instance of ActionController::Parameters. The technique is well known
as "Mass Assignment".

Internally, they expects the object should have #each_pair method.
Therefore, this changes the signature of these methods to take a
Hash-like object instead of Hash.

refs: https://github.com/rails/rails/blob/v7.0.0/activemodel/lib/active_model/attribute_assignment.rb#L29

Copy link

@tk0miya Thanks for your contribution!

Please follow the instructions below for each change.
See also: https://github.com/ruby/gem_rbs_collection/blob/main/docs/CONTRIBUTING.md

Available commands

You can use the following commands by commenting on this PR.

  • /merge: Merge this PR if CI passes

activerecord

You changed RBS files for an existing gem.
You need to get approval from the reviewers of this gem.

@hibariya, @ksss, @Little-Rubyist, please review this pull request.
If this change is acceptable, please make a review comment including APPROVE from here.
Screen Shot 2024-03-19 at 14 13 36

After that, the PR author or the reviewers can merge this PR.
Just comment /merge to merge this PR.

@tk0miya tk0miya force-pushed the activerecord_support_parameters branch from 9915d72 to d0100ff Compare June 11, 2024 16:39
def build: (?::Hash[untyped, untyped] attributes) -> untyped | ...
def create: (?::Hash[untyped, untyped] attributes) -> untyped | ...
def create!: (?::Hash[untyped, untyped] attributes) -> untyped | ...
# Collection proxies in Active Record are middlemen between an
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first, I moved this definition from activerecord-generated.rbs to here, and merge the above definition into it.
c68dd60

#
# person.pets.size # => 5 # size of the collection
# person.pets.count # => 0 # count from database
def build: (?_EachPair attributes) ?{ () -> untyped } -> untyped
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second, I modified the definition of #build, #create and #create! to take _EachPair.
d0100ff

gems/activerecord/6.0/activerecord.rbs Show resolved Hide resolved
@ksss
Copy link
Collaborator

ksss commented Jun 12, 2024

$ bundle exec rbs --repo gems -r activemodel diff --detail --format diff --type-name 'ActiveRecord::Associations::CollectionProxy' --before gems/activerecord/6.0 --after 598
- [::ActiveRecord::Associations::CollectionProxy public] def build: (?::Hash[untyped, untyped] attributes) -> untyped | (?::Hash[untyped, untyped] attributes) { () -> untyped } -> untyped
+ [::ActiveRecord::Associations::CollectionProxy public] def build: (?::ActiveRecord::Associations::CollectionProxy::_EachPair attributes) ?{ () -> untyped } -> untyped

- [::ActiveRecord::Associations::CollectionProxy public] def create: (?::Hash[untyped, untyped] attributes) -> untyped | (?::Hash[untyped, untyped] attributes) { () -> untyped } -> untyped
+ [::ActiveRecord::Associations::CollectionProxy public] def create: (?::ActiveRecord::Associations::CollectionProxy::_EachPair attributes) ?{ () -> untyped } -> untyped

- [::ActiveRecord::Associations::CollectionProxy public] def create!: (?::Hash[untyped, untyped] attributes) -> untyped | (?::Hash[untyped, untyped] attributes) { () -> untyped } -> untyped
+ [::ActiveRecord::Associations::CollectionProxy public] def create!: (?::ActiveRecord::Associations::CollectionProxy::_EachPair attributes) ?{ () -> untyped } -> untyped

gems/activerecord/6.0/activerecord.rbs Show resolved Hide resolved
# instantiation of the actual post records.
class CollectionProxy < Relation
interface _EachPair
def each_pair: () { ([untyped, untyped]) -> untyped } -> self
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what is the best way for such an interface. But no reason to ignore them. Okay, I'll add them soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I merged #603 into this PR to support ActionController::Parameters.

@tk0miya tk0miya force-pushed the activerecord_support_parameters branch from be96959 to 4253a73 Compare June 13, 2024 14:36
The definitions for `ActiveRecord::Associations::CollectionProxy` are
placed both `activerecord.rbs` and `activerecord-generated.rbs`.  Before
change them, this merges them into the single definition.
The current definition of `#build`, `#craete` and `#create!` methods of
`ActiveRecord::Associations::CollectionProxy#build` take a Hash object.

But they can also take Hash-like object.  Usually, Rails apps pass an
instance of `ActionController::Parameters`.  The technique is well known
as "Mass Assignment".

Internally, they expects the object should have `#each_pair` method.
Therefore, this changes the signature of these methods to take a
Hash-like object instead of Hash.

refs: https://github.com/rails/rails/blob/v7.0.0/activemodel/lib/active_model/attribute_assignment.rb#L29
@tk0miya tk0miya force-pushed the activerecord_support_parameters branch from 4253a73 to b0992ee Compare June 16, 2024 05:41
Copy link
Collaborator

@ksss ksss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APPROVE

Copy link

Thanks for your review, @ksss!

@tk0miya, @ksss This PR is ready to be merged.
Just comment /merge to merge this PR.

@tk0miya
Copy link
Contributor Author

tk0miya commented Jun 21, 2024

/merge

@github-actions github-actions bot merged commit fe99f97 into ruby:main Jun 21, 2024
5 checks passed
@tk0miya tk0miya deleted the activerecord_support_parameters branch June 21, 2024 02:48
rhiroe added a commit to rhiroe/gem_rbs_collection that referenced this pull request Jul 28, 2024
resolved ruby#619

Add _test/metadata.yaml and _test/steep_expectations.yml to activerecord 6.1 as in 6.0.

These files are added in ruby#598
rhiroe added a commit to rhiroe/gem_rbs_collection that referenced this pull request Jul 28, 2024
resolved ruby#619

Symlink of _test/metadata.yaml and _test/steep_expectations.yml in 6.0 to 6.1.

These files are added in ruby#598
rhiroe added a commit to rhiroe/gem_rbs_collection that referenced this pull request Jul 28, 2024
resolved ruby#619

Add of _test/metadata.yaml and _test/steep_expectations.yml to activerecord 6.1 as in 6.0.

These files are added in ruby#598
rhiroe added a commit to rhiroe/gem_rbs_collection that referenced this pull request Jul 29, 2024
resolved ruby#619

Symlink of _test/metadata.yaml and _test/steep_expectations.yml in 6.0 to 6.1

These files are added in ruby#598
rhiroe added a commit to rhiroe/gem_rbs_collection that referenced this pull request Jul 29, 2024
- Symlink of _test/metadata.yaml in 6.0 to 6.1
- Annotated `User#articles` definition using `@dynamic` and removed _test/steep_expectations.yml

These files are added in ruby#598
github-actions bot pushed a commit that referenced this pull request Aug 2, 2024
- Symlink of _test/metadata.yaml in 6.0 to 6.1
- Annotated `User#articles` definition using `@dynamic` and removed _test/steep_expectations.yml

These files are added in #598
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants