Skip to content

Commit b8d4885

Browse files
authored
Expose resource as an instance variable (#2478)
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`.
1 parent c691562 commit b8d4885

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

app/controllers/administrate/application_controller.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def index
99
resources = apply_collection_includes(resources)
1010
resources = order.apply(resources)
1111
resources = paginate_resources(resources)
12+
@resources = resources
1213
page = Administrate::Page::Collection.new(dashboard, order: order)
1314
filters = Administrate::Search.new(scoped_resource, dashboard, search_term).valid_filters
1415

@@ -22,27 +23,29 @@ def index
2223
end
2324

2425
def show
26+
@resource = resource = requested_resource
2527
render locals: {
26-
page: Administrate::Page::Show.new(dashboard, requested_resource)
28+
page: Administrate::Page::Show.new(dashboard, resource)
2729
}
2830
end
2931

3032
def new
31-
resource = new_resource
33+
@resource = resource = new_resource
3234
authorize_resource(resource)
3335
render locals: {
3436
page: Administrate::Page::Form.new(dashboard, resource)
3537
}
3638
end
3739

3840
def edit
41+
@resource = resource = requested_resource
3942
render locals: {
40-
page: Administrate::Page::Form.new(dashboard, requested_resource)
43+
page: Administrate::Page::Form.new(dashboard, resource)
4144
}
4245
end
4346

4447
def create
45-
resource = new_resource(resource_params)
48+
@resource = resource = new_resource(resource_params)
4649
authorize_resource(resource)
4750

4851
if resource.save
@@ -59,26 +62,28 @@ def create
5962
end
6063

6164
def update
62-
if requested_resource.update(resource_params)
65+
@resource = resource = requested_resource
66+
if resource.update(resource_params)
6367
redirect_to(
64-
after_resource_updated_path(requested_resource),
68+
after_resource_updated_path(resource),
6569
notice: translate_with_resource("update.success"),
6670
status: :see_other
6771
)
6872
else
6973
render :edit, locals: {
70-
page: Administrate::Page::Form.new(dashboard, requested_resource)
74+
page: Administrate::Page::Form.new(dashboard, resource)
7175
}, status: :unprocessable_entity
7276
end
7377
end
7478

7579
def destroy
76-
if requested_resource.destroy
80+
@resource = resource = requested_resource
81+
if resource.destroy
7782
flash[:notice] = translate_with_resource("destroy.success")
7883
else
79-
flash[:error] = requested_resource.errors.full_messages.join("<br/>")
84+
flash[:error] = resource.errors.full_messages.join("<br/>")
8085
end
81-
redirect_to after_resource_destroyed_path(requested_resource), status: :see_other
86+
redirect_to after_resource_destroyed_path(resource), status: :see_other
8287
end
8388

8489
private

spec/example_app/app/controllers/admin/application_controller.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,22 @@ def authenticate_admin
3232
def pundit_user
3333
@current_user
3434
end
35+
36+
after_action :audit_log, only: %i[create update destroy]
37+
38+
def audit_log
39+
if (resource = @resource)
40+
Rails.logger.info(
41+
sprintf(
42+
"Audit Log: %<action>s %<class>s #%<id>d by %<user>s at %<time>s",
43+
action: action_name.capitalize,
44+
class: resource.class,
45+
id: resource.id || 0,
46+
user: pundit_user.name,
47+
time: Time.zone.now.to_s
48+
)
49+
)
50+
end
51+
end
3552
end
3653
end

0 commit comments

Comments
 (0)