Skip to content
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

Fix callbacks not being created when updating audit options #751

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ def set_audit(options)
has_many :audits, -> { order(version: :asc) }, as: :auditable, class_name: Audited.audit_class.name, inverse_of: :auditable
Audited.audit_class.audited_class_names << to_s

after_create :audit_create if audited_options[:on].include?(:create)
before_update :audit_update if audited_options[:on].include?(:update)
after_touch :audit_touch if audited_options[:on].include?(:touch) && ::ActiveRecord::VERSION::MAJOR >= 6
before_destroy :audit_destroy if audited_options[:on].include?(:destroy)
set_audited_callbacks

# Define and set after_audit and around_audit callbacks. This might be useful if you want
# to notify a party after the audit has been created or if you want to access the newly-created
Expand Down Expand Up @@ -528,11 +525,21 @@ def default_ignored_attributes
def normalize_audited_options
audited_options[:on] = Array.wrap(audited_options[:on])
audited_options[:on] = ([:create, :update, :touch, :destroy] - Audited.ignored_default_callbacks) if audited_options[:on].empty?
set_audited_callbacks

audited_options[:only] = Array.wrap(audited_options[:only]).map(&:to_s)
audited_options[:except] = Array.wrap(audited_options[:except]).map(&:to_s)
audited_options[:max_audits] ||= Audited.max_audits
end


def set_audited_callbacks
after_create :audit_create if audited_options[:on].include?(:create) && (audited? ? _create_callbacks.none? { _1.filter == :audit_create } : true)
before_update :audit_update if audited_options[:on].include?(:update) && ( audited? ? _update_callbacks.none? { _1.filter == :audit_update } : true)
after_touch :audit_touch if audited_options[:on].include?(:touch) && ::ActiveRecord::VERSION::MAJOR >= 6 && (audited? ? _touch_callbacks.none? { _1.filter == :audit_touch } : true)
before_destroy :audit_destroy if audited_options[:on].include?(:destroy) && ( audited? ? _destroy_callbacks.none? { _1.filter == :audit_destroy } : true )
end

def calculate_non_audited_columns
if audited_options[:only].present?
(column_names | default_ignored_attributes) - audited_options[:only]
Expand Down