-
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
Extracted attributes assingment from ActiveRecord to ActiveModel #10776
Conversation
end | ||
|
||
end | ||
UnknownAttributeError = ActiveModel::AttributeAssignment::UnknownAttributeError |
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 don't think this is still necessary.
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.
Existing rails apps could still use this constant to catch this exception and process it manually.
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.
Raising it has to trigger a deprecation then.
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.
Do you know how to do that?
Is there a reason why you didn't extract the tests too ? |
@dmathieu once I saw someone from core team saying that overlapping between AR and AM tests is good idea. No concrete reason. |
|
||
private | ||
|
||
def _assign_attribute(k, v) |
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.
Could you please indent the private methods as the contribution guideline states ? Also, I think that this will at least need a changelog entry. Not sure if you should add one in AR changelog as well.
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.
Nice pull request though, thank you!
What is the motivation for this extraction? |
Using outside of activerecord just like validation and others: Attributes assignment concept is something I personally use a lot for many types of objects. |
👍 for the idea from my side. @sgrif WDYT? |
👍 for the idea from me, as well. Would like to review the code again once it's been rebased onto master and had tests added/moved/etc |
609006e
to
4c14289
Compare
Updated the PR:
|
I would use this on Mongoid if this is merge on ActiveModel.! |
_assign_attributes(sanitize_for_mass_assignment(attributes)) | ||
end | ||
|
||
def _assign_attributes(attributes) |
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.
Should not this method be private?
4c14289
to
f39d770
Compare
@rafaelfranca removed all whitespace |
@@ -1,3 +1,23 @@ | |||
* Extracted `ActiveRecord::AttributeAssignment` to `ActiveModel::AttributesAssignment` |
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.
AttributesAssignment
=> AttributeAssignment
# cat = Cat.new | ||
# cat.assign_attributes(name: "Gorby", status: "yawning") | ||
# cat.name # => 'Gorby' | ||
# cate.status => 'yawning' |
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.
same here
…utesAssignment` Allows to use it for any object as an includable module.
f39d770
to
2606fb3
Compare
Minor style changes across the board. Changed an alias to an explicit method declaration, since the alias will not be documented otherwise.
Extracted attributes assingment from ActiveRecord to ActiveModel
❤️ |
I wonder if, once all non-db-specific logic (including recent one such as the user-facing casting half) is extracted to AM, it would make sense to have a more full-featured out-of-the-box implementation of active model for those that want it to behave not just as a form object but as much as a full AR as possible, except not persisted. That means all the same validations/errors, attribute assignment, casting, etc. Perhaps call it |
end | ||
UnknownAttributeError = ActiveSupport::Deprecation::DeprecatedConstantProxy.new( # :nodoc: | ||
'ActiveRecord::UnknownAttributeError', | ||
'ActiveModel::AttributeAssignment::UnknownAttributeError' |
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.
Given that end users may be rescuing from this error in their apps, it seems like a rather long and implementation-specific namespace. Perhaps keep it under ActiveModel::UnknownAttributeError
?
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.
Yep, I agree there ; this is too implementation-specific! 👍
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.
Agreed, let's leave this undeprecated, and just alias it. It's an unnecessary change for users, and forces them to care too much about implementation.
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.
@robin850 Would you care to open a PR?
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.
Leaving this undeprecated can cause confusion because while constant alias will work the caught exception instances will still have new name. It looks like people are just yelling about too long name space and want it to be sorter like in @egilburg's comment
active_model/forbidden_attributes_protection is not used since rails#10776
Remove redundant require: follow #10776
Now you are able to do:
This is just a proof of concept PR.
There is still some todos:
I am gonna add them if this will be approved to merge in.