-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add resource instance variables and audit log sample #2478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nickcharlton @pablobm
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,5 +32,22 @@ def authenticate_admin | |
| def pundit_user | ||
| @current_user | ||
| end | ||
|
|
||
| after_action :audit_log, only: %i[create update destroy] | ||
nickcharlton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def audit_log | ||
| if (resource = @resource) | ||
| Rails.logger.info( | ||
| sprintf( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hah! It's so rare to see the use of a |
||
| "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 | ||
Uh oh!
There was an error while loading. Please reload this page.