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 7d6f2c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 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)

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
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 = @requested_resource || @new_resource
Rails.logger.info(
sprintf(
"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

0 comments on commit 7d6f2c4

Please sign in to comment.