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

allow calling audited multiple times #734

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 32 additions & 6 deletions lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,24 @@ module ClassMethods
# end
#
def audited(options = {})
# don't allow multiple calls
return if included_modules.include?(Audited::Auditor::AuditedInstanceMethods)
audited? ? update_audited_options(options) : set_audit(options)
end

private

def audited?
included_modules.include?(Audited::Auditor::AuditedInstanceMethods)
end

def set_audit(options)
extend Audited::Auditor::AuditedClassMethods
include Audited::Auditor::AuditedInstanceMethods

class_attribute :audit_associated_with, instance_writer: false
class_attribute :audited_options, instance_writer: false
attr_accessor :audit_version, :audit_comment

self.audited_options = options
normalize_audited_options

self.audit_associated_with = audited_options[:associated_with]
set_audited_options(options)

if audited_options[:comment_required]
validate :presence_of_audit_comment
Expand Down Expand Up @@ -100,6 +104,23 @@ def audited(options = {})
def has_associated_audits
has_many :associated_audits, as: :associated, class_name: Audited.audit_class.name
end

def update_audited_options(new_options)
previous_audit_options = self.audited_options
set_audited_options(new_options)
self.reset_audited_columns

log_message = "#{self.name} is already audited, audit options will be updated\n"\
"before: #{previous_audit_options}\n"\
"after: #{self.audited_options}"
Logger.new($stdout).info(log_message)
end

def set_audited_options(options)
self.audited_options = options
normalize_audited_options
self.audit_associated_with = audited_options[:associated_with]
end
end

module AuditedInstanceMethods
Expand Down Expand Up @@ -530,6 +551,11 @@ def calculate_non_audited_columns
def class_auditing_enabled
Audited.store.fetch("#{table_name}_auditing_enabled", true)
end

def reset_audited_columns
@audited_columns = nil
@non_audited_columns = nil
end
end
end
end
16 changes: 16 additions & 0 deletions spec/audited/auditor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1270,4 +1270,20 @@ def stub_global_max_audits(max_audits)
}.to_not change(Audited::Audit, :count)
end
end

describe "call audit multiple times" do
it "should update audit options" do
user = Models::ActiveRecord::UserOnlyName.create
user.update(password: "new password 1", name: "new name 1")
expect(user.audits.last.audited_changes.keys).to eq(%w[name])

user.class.class_eval do
audited only: :password
end

user = Models::ActiveRecord::UserOnlyName.last
user.update(password: "new password 2", name: "new name 2")
expect(user.audits.last.audited_changes.keys).to eq(%w[password])
end
end
end
6 changes: 6 additions & 0 deletions spec/support/active_record/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class UserOnlyPassword < ::ActiveRecord::Base
audited only: :password
end

class UserOnlyName < ::ActiveRecord::Base
self.table_name = :users
attribute :non_column_attr if Rails.gem_version >= Gem::Version.new("5.1")
audited only: :name
end

class UserRedactedPassword < ::ActiveRecord::Base
self.table_name = :users
audited redacted: :password
Expand Down