-
Notifications
You must be signed in to change notification settings - Fork 674
Optionally pass audited action to conditionals #754
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ module ClassMethods | |
| # audited except: :password | ||
| # end | ||
| # | ||
| # * +require_comment+ - Ensures that audit_comment is supplied before | ||
| # * +comment_required+ - Ensures that audit_comment is supplied before | ||
| # any create, update or destroy operation. | ||
| # * +max_audits+ - Limits the number of stored audits. | ||
|
|
||
|
|
@@ -375,7 +375,7 @@ def audit_destroy | |
| def write_audit(attrs) | ||
| self.audit_comment = nil | ||
|
|
||
| if auditing_enabled | ||
| if auditing_enabled(attrs[:action]) | ||
| attrs[:associated] = send(audit_associated_with) unless audit_associated_with.nil? | ||
|
|
||
| run_callbacks(:audit) { | ||
|
|
@@ -393,10 +393,16 @@ def presence_of_audit_comment | |
| end | ||
|
|
||
| def comment_required_state? | ||
| auditing_enabled && | ||
| action = if audited_options[:on].include?(:create) && new_record? | ||
| "create" | ||
| elsif audited_options[:on].include?(:update) && persisted? && changed? | ||
| "update" | ||
| else | ||
| nil | ||
| end | ||
| auditing_enabled(action) && | ||
| audited_changes.present? && | ||
| ((audited_options[:on].include?(:create) && new_record?) || | ||
| (audited_options[:on].include?(:update) && persisted? && changed?)) | ||
| (action == "create" || action == "update") | ||
| end | ||
|
|
||
| def combine_audits_if_needed | ||
|
|
@@ -420,7 +426,9 @@ def evaluate_max_audits | |
| end | ||
|
|
||
| def require_comment | ||
| if auditing_enabled && audit_comment.blank? | ||
| # this method is only called as before_destroy callback | ||
| # therefore it's safe to statically pass "destroy" | ||
| if auditing_enabled("destroy") && audit_comment.blank? | ||
| errors.add(:audit_comment, :blank) | ||
| throw(:abort) | ||
| end | ||
|
|
@@ -430,18 +438,21 @@ def require_comment | |
| alias_method "#{attr_name}_callback".to_sym, attr_name | ||
| end | ||
|
|
||
| def auditing_enabled | ||
| run_conditional_check(audited_options[:if]) && | ||
| run_conditional_check(audited_options[:unless], matching: false) && | ||
| def auditing_enabled(action = nil) | ||
| run_conditional_check(audited_options[:if], action: action) && | ||
| run_conditional_check(audited_options[:unless], action: action, matching: false) && | ||
| self.class.auditing_enabled | ||
| end | ||
|
|
||
| def run_conditional_check(condition, matching: true) | ||
| def run_conditional_check(condition, action:, matching: true) | ||
| return true if condition.blank? | ||
| return condition.call(self) == matching if condition.respond_to?(:call) | ||
| return send(condition) == matching if respond_to?(condition.to_sym, true) | ||
| result = case condition | ||
|
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. Rewritten in the style of |
||
| when Proc then condition.call(self, action) | ||
|
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. I thought about passing |
||
| when Symbol then method(condition.to_sym).arity == 0 ? send(condition) : send(condition, action) | ||
| else return true | ||
| end | ||
|
|
||
| true | ||
| result == matching | ||
| end | ||
|
|
||
| def reconstruct_attributes(audits) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kind of related documentation fix: The private method is called
require_comment, the option is calledcomment_required.