-
Notifications
You must be signed in to change notification settings - Fork 21.7k
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
Introduce ActiveModel::NestedAttributes
#49637
base: main
Are you sure you want to change the base?
Conversation
6bd7f9f
to
2140b20
Compare
@rafaelfranca this proposal is made in the same spirit as work like #49534. It aims to better integrate Active Mode with Action Pack and Action View in familiar and intuitive ways. |
b833645
to
8dd3be8
Compare
@rafaelfranca I'll subscribe to #44324. I had considered adding test coverage for Are I'm happy to adjust the current implementation to respond to changes in #44324, but I think it's worth emphasizing that the current approach works with I think a final version of this code could support both, I just want to emphasize that |
@seanpdoyle I opened this PR two years ago to solve the same problem. I'm curious to know what you think. |
195909e
to
9943b8a
Compare
# === Integration with ActiveModel::Attributes | ||
# | ||
# When attributes are declared with the +.attribute+ class method provided | ||
# by the Attributes module, support | ||
# for nested attributes will construct instances using available | ||
# Type information. For example, the +accepts_nested_attributes_for+ | ||
# declarations in the following code sample will use the Type that corresponds | ||
# to +:user+ when assigning to +author_attributes+ and the Type that | ||
# correpsonds to +:tags+ when assigning to +tags_attributes+: | ||
# | ||
# class Article | ||
# include ActiveModel::Model | ||
# include ActiveModel::Attributes | ||
# include ActiveModel::NestedAttributes | ||
# | ||
# attribute :author, :user | ||
# attribute :tags, :tag | ||
# | ||
# accepts_nested_attributes_for :author | ||
# accepts_nested_attributes_for :tags | ||
# end |
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.
@rafaelfranca in reference to #49637 (comment):
Does this style of definition match what you have in mind?
The implementation builds on top of #49910 (authored by @jonathanhefner and @p8) to first try utilizing defined Attributes before making any kind of inference to classes to build from Hashes:
ef4db5c
to
05bfcf0
Compare
c271fbe
to
5b420d2
Compare
Largely inspired by the Active Record implementation, this commit introduces support for assigning instances of objects to classes that `include ActiveModel::Model`. It aims to handle assignment of `_attributes`-suffixed values in an ActionView- and ActionPack-compliant way: ```ruby Author = Struct.new(:name) Category = Struct.new(:name) class Article include ActiveModel::Model include ActiveModel::NestedAttributes attr_accessor :author attr_accessor :tags accepts_nested_attributes_for :author accepts_nested_attributes_for :tags, class_name: Category end article = Article.new( author_attributes: { name: "Pseudo Nym" }, tags_attributes: { 0 => { name: "actionview" }, 1 => { name: "actionpack" }, } ) article.author.name # => "Pseudo Nym" article.tags.pluck(:name) # => ["actionview", "actionpack"] ```
Motivation / Background
Largely inspired by the Active Record implementation, this commit introduces support for assigning instances of objects to classes that
include ActiveModel::Model
.Detail
It aims to handle assignment of
_attributes
-suffixed values in an ActionView- and ActionPack-compliant way:Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]