-
Notifications
You must be signed in to change notification settings - Fork 108
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
activerecord: CollectionProxy#build can take Hash-like object #598
Conversation
@tk0miya Thanks for your contribution! Please follow the instructions below for each change. Available commandsYou can use the following commands by commenting on this PR.
|
9915d72
to
d0100ff
Compare
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
$ 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 |
# instantiation of the actual post records. | ||
class CollectionProxy < Relation | ||
interface _EachPair | ||
def each_pair: () { ([untyped, untyped]) -> untyped } -> self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is #empty?
not necessary?
And #each
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
be96959
to
4253a73
Compare
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
4253a73
to
b0992ee
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
APPROVE
/merge |
- 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
- 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
The current definition of
#build
,#craete
and#create!
methods ofActiveRecord::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 knownas "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