-
-
Notifications
You must be signed in to change notification settings - Fork 258
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
fix: actions and field hydration #1992
Conversation
Code Climate has analyzed commit e9ec6f3 and detected 0 issues on this pull request. View more on Code Climate. |
@resource = resource if resource.present? | ||
@action = action if action.present? | ||
@user = user if user.present? | ||
@panel_name = panel_name if panel_name.present? | ||
def hydrate(**kwargs) | ||
# List of permitted keyword argument keys as symbols | ||
permited_kwargs_keys = %i[model resource action view panel_name user] | ||
|
||
# Check for unrecognized keys | ||
unrecognized_keys = kwargs.keys - permited_kwargs_keys | ||
raise ArgumentError, "Unrecognized argument(s): #{unrecognized_keys.join(', ')}" if unrecognized_keys.any? | ||
|
||
# Set instance variables with provided values | ||
kwargs.each do |key, value| | ||
instance_variable_set("@#{key}", value) | ||
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.
why the refactor here? it feels difficult to understand at a glance
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.
From PR description:
The existing code made it impossible to hydrate the field with
nil
values, resulting in undesired behavior. For instance, in cases where a field within an action relied on the@record
via a default Proc, it would correctly receive the default value when executed in a show view. However, when executed in an index view, the field attempted to hydrate withmodel: nil
, yet the priormodel
would remain hydrated, causing the value to be fetched incorrectly.
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.
What this does is if we use hydrate(model: nil)
will assign @model = nil
without touching the other variables... With the old code @model = model if model.present?
since @model.present? == false
the @model
will keep the last assigned model
even if we want to force hydrate it to nil...
@model = model | ||
@resource = resource | ||
@user = user | ||
@view = view |
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.
Pfff. what a good fix!
THanks!
@@ -16,7 +16,6 @@ def show | |||
@view = :new | |||
|
|||
@resource.hydrate(model: @model, view: @view, user: _current_user, params: params) | |||
@model = ActionModel.new @action.get_attributes_for_action |
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.
Not sure that this is the right decision.
I can't remember why, but at some point I know for a fact that it wasn't the right decision to use the @model
.
Let's talk about it.
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.
Tests are not failing, also tested it intensively manually and everything works as expected
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.
Ok, let's merge it but let's keep an eye on it and mention something in the upgrade guide that we changed something internal and if they see anything off, they should reach out to us.
|
This PR has been merged into Please check the release guide for more information. |
Description
Fixes #1946
This pull request addresses primarily two key issues: the class attributes for actions and the field hydration method.
Action class attributes
Relying on class attributes for actions is not convenient since undesired old values can remain populated on a new instance.
Field Hydration Method
The existing code made it impossible to hydrate the field with
nil
values, resulting in undesired behavior. For instance, in cases where a field within an action relied on the@record
via a default Proc, it would correctly receive the default value when executed in a show view. However, when executed in an index view, the field attempted to hydrate withmodel: nil
, yet the priormodel
would remain hydrated, causing the value to be fetched incorrectly.ActionModel
removedActionModel
was not being used only to pass it asmodel
onform_with
helper, and I think that was unecessary.Checklist:
Manual review steps
default
value as Proc reling on the@record
Example:
Example:
@record
is blank@record
is setted ( and the default proc is running with@record
properly hydrated on all executions )@record
from the show view since was impossible to hydrate with nil values)Manual reviewer: please leave a comment with output from the test if that's the case.