Skip to content

Commit

Permalink
Add resource instance variables and audit log sample
Browse files Browse the repository at this point in the history
  • Loading branch information
goosys committed Dec 26, 2023
1 parent 17e2774 commit 5cb4851
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/controllers/administrate/application_controller.rb
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)

render locals: {
Expand All @@ -26,7 +27,7 @@ def show
end

def new
resource = new_resource
@new_resource = resource = new_resource
authorize_resource(resource)
render locals: {
page: Administrate::Page::Form.new(dashboard, resource),
Expand All @@ -40,7 +41,7 @@ def edit
end

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

if resource.save
Expand Down
18 changes: 18 additions & 0 deletions spec/example_app/app/controllers/admin/application_controller.rb
Expand Up @@ -32,5 +32,23 @@ def authenticate_admin
def pundit_user
@current_user
end

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

def audit_log
if resource = @requested_resource || @new_resource
Rails.logger.info(
sprintf(
"Audit Log: %s %s #%d by %s at %s",
action_name.capitalize,
resource.class,
resource.id,
pundit_user.name,
Time.zone.now.to_s
)
)
end
end

end
end

0 comments on commit 5cb4851

Please sign in to comment.