Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def index
resources = apply_collection_includes(resources)
resources = order.apply(resources)
resources = paginate_resources(resources)
@resources = resources
page = Administrate::Page::Collection.new(dashboard, order: order)
Copy link
Copy Markdown
Contributor Author

@goosys goosys Mar 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nickcharlton @pablobm
If it's for the audit log, I feel like having @page would be nice too. Thoughts?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given how prevalent it is (it appears in all actions that render a page), I think it may make sense, yes 👍 Go for it.

filters = Administrate::Search.new(scoped_resource, dashboard, search_term).valid_filters

Expand All @@ -22,27 +23,29 @@ def index
end

def show
@resource = resource = requested_resource
render locals: {
page: Administrate::Page::Show.new(dashboard, requested_resource)
page: Administrate::Page::Show.new(dashboard, resource)
}
end

def new
resource = new_resource
@resource = resource = new_resource
authorize_resource(resource)
render locals: {
page: Administrate::Page::Form.new(dashboard, resource)
}
end

def edit
@resource = resource = requested_resource
render locals: {
page: Administrate::Page::Form.new(dashboard, requested_resource)
page: Administrate::Page::Form.new(dashboard, resource)
}
end

def create
resource = new_resource(resource_params)
@resource = resource = new_resource(resource_params)
authorize_resource(resource)

if resource.save
Expand All @@ -59,26 +62,28 @@ def create
end

def update
if requested_resource.update(resource_params)
@resource = resource = requested_resource
if resource.update(resource_params)
redirect_to(
after_resource_updated_path(requested_resource),
after_resource_updated_path(resource),
notice: translate_with_resource("update.success"),
status: :see_other
)
else
render :edit, locals: {
page: Administrate::Page::Form.new(dashboard, requested_resource)
page: Administrate::Page::Form.new(dashboard, resource)
}, status: :unprocessable_entity
end
end

def destroy
if requested_resource.destroy
@resource = resource = requested_resource
if resource.destroy
flash[:notice] = translate_with_resource("destroy.success")
else
flash[:error] = requested_resource.errors.full_messages.join("<br/>")
flash[:error] = resource.errors.full_messages.join("<br/>")
end
redirect_to after_resource_destroyed_path(requested_resource), status: :see_other
redirect_to after_resource_destroyed_path(resource), status: :see_other
end

private
Expand Down
17 changes: 17 additions & 0 deletions spec/example_app/app/controllers/admin/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,22 @@ def authenticate_admin
def pundit_user
@current_user
end

after_action :audit_log, only: %i[create update destroy]

def audit_log
if (resource = @resource)
Rails.logger.info(
sprintf(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah! It's so rare to see the use of a sprintf, fun!

"Audit Log: %<action>s %<class>s #%<id>d by %<user>s at %<time>s",
action: action_name.capitalize,
class: resource.class,
id: resource.id || 0,
user: pundit_user.name,
time: Time.zone.now.to_s
)
)
end
end
end
end
Loading