Add resource instance variables and audit log sample#2478
Add resource instance variables and audit log sample#2478nickcharlton merged 1 commit intothoughtbot:mainfrom
Conversation
e77fb39 to
7d6f2c4
Compare
|
I'm ok with this, but I think it should be consistent and always be Thoughts, @nickcharlton? |
|
Ah yeah, I see. Yeah, consistency is important here. |
7d6f2c4 to
f54ada5
Compare
|
rebased |
| def audit_log | ||
| if (resource = @requested_resource || @new_resource) | ||
| Rails.logger.info( | ||
| sprintf( |
There was a problem hiding this comment.
Hah! It's so rare to see the use of a sprintf, fun!
|
|
||
| def new | ||
| resource = new_resource | ||
| @new_resource = resource = new_resource |
There was a problem hiding this comment.
@pablobm, where you thinking that this should be @resource and not @new_resource? I'm not sure if we made any changes here after that comment
There was a problem hiding this comment.
That's right. Unless there's a reason for it, I think this should be @resource for consistency.
There was a problem hiding this comment.
Actually... now I'm torn. I forgot that we have @requested_resource, so having @resource might be confusing 🤔
There was a problem hiding this comment.
I was reorganizing ApplicationController for a separate task, and I think @built_resource would be a good choice.
It clearly indicates that the resource is not yet saved, making it more explicit than simply using @resource.
There was a problem hiding this comment.
This makes me uneasy... 🤔 Increasing the number of instance variables increases complexity. In your audit_log for example, having to do if (resource = @requested_resource || @new_resource) is a smell. I would prefer to do if @resource. The action can be obtained from action if further filtering is required.
As mentioned, the presence of @requested_resource complicates things a bit... I'm looking at it now and I think that's the problem: a noun-method that should be a verb-method. I think instead we should do this in update, edit, destroy:
def update
@resource = find_resource(resource_params)
if @resource
redirect_to(
after_resource_updated_path(@resource),
notice: translate_with_resource("update.success"),
status: :see_other
)
else
render :edit, locals: {
page: Administrate::Page::Form.new(dashboard, @resource)
}, status: :unprocessable_entity
end
end
Which now relies on verb-method (also requires renaming the pre-existing find_resource):
def find_resource
find_resource_in_scope(params[:id]).tap do |resource|
authorize_resource(resource)
end
end
def find_resource_in_scope(param)
scoped_resource.find(param)
end
This is now consistent with index,new, and create, which retrieve the resource with a helper method and put it in an instance variable.
There was a problem hiding this comment.
I liked the approach with @requested_resource, so I hadn't considered changing it. But now, as you mentioned, I think using @resource is better.
|
|
||
| def create | ||
| resource = new_resource(resource_params) | ||
| @new_resource = resource = new_resource(resource_params) |
There was a problem hiding this comment.
And same thing here:
| @new_resource = resource = new_resource(resource_params) | |
| @resource = resource = new_resource(resource_params) |
| resources = order.apply(resources) | ||
| resources = paginate_resources(resources) | ||
| @resources = resources | ||
| page = Administrate::Page::Collection.new(dashboard, order: order) |
There was a problem hiding this comment.
@nickcharlton @pablobm
If it's for the audit log, I feel like having @page would be nice too. Thoughts?
There was a problem hiding this comment.
Given how prevalent it is (it appears in all actions that render a page), I think it may make sense, yes 👍 Go for it.
|
@goosys - Thoughts on this one? Particularly what I say at #2478 (comment) |
|
@pablobm |
|
@goosys, I might be wrong, but it looks like we just need to settle on |
f54ada5 to
8252274
Compare
|
@pablobm @nickcharlton The goal here is to make the resource available for post-processing in each action, so I think it’s reasonable to stop at this point for now. We could consider renaming |
|
Ah yeah, nice. I think that's it! Thanks! |
There's a few cases where it'd be helpful to be able to refer to the resource on the current action. This does that as `@resources` or as `@resource`. To show this in use, we include a sample of an audit log which refers to `@resource`.
8252274 to
d8891e2
Compare
I have made changes so that instance variables are stored in both the index and create actions, in order to capture controller operation logs and perform other post-processing tasks.
Please review.