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 keep create actions when combine old audits #728

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions lib/audited.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class << self
:ignored_attributes,
:ignored_default_callbacks,
:max_audits,
:keep_create_action,
:store_synthesized_enums
attr_writer :audit_class

Expand Down
4 changes: 4 additions & 0 deletions lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,9 @@ def combine_audits_if_needed

if max_audits && (extra_count = audits.count - max_audits) > 0
audits_to_combine = audits.limit(extra_count + 1)
if audited_options[:keep_create_action]
audits_to_combine = audits_to_combine.where.not(action: 'create')
end
combine_audits(audits_to_combine)
end
end
Expand Down Expand Up @@ -515,6 +518,7 @@ def normalize_audited_options
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
audited_options[:keep_create_action] ||= Audited.keep_create_action
end

def calculate_non_audited_columns
Expand Down
22 changes: 22 additions & 0 deletions spec/audited/auditor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,28 @@ class CallbacksSpecified < ::ActiveRecord::Base
end
end

context 'when set to keep create action' do
before do
Audited.keep_create_action = true
end

it 'should respect per model setting' do
stub_global_max_audits(4) do
expect(Models::ActiveRecord::MaxAuditsIgnoreKeepCreateActionUser.audited_options[:keep_create_action]).to be_falsey
end
end

it 'should keep create action' do
stub_global_max_audits(2) do
user = Models::ActiveRecord::User.create!(name: "Foobar 1")
user.update(name: "Foobar 2", audit_comment: "First audit comment")
user.update(name: "Foobar 3", audit_comment: "Second audit comment")
pp user.audits
expect(user.audits.first.action).to eq('create')
end
end
end

def stub_global_max_audits(max_audits)
previous_max_audits = Audited.max_audits
previous_user_audited_options = Models::ActiveRecord::User.audited_options.dup
Expand Down
5 changes: 5 additions & 0 deletions spec/support/active_record/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ class MaxAuditsUser < ::ActiveRecord::Base
audited max_audits: 5
end

class MaxAuditsIgnoreKeepCreateActionUser < ::ActiveRecord::Base
self.table_name = :users
audited max_audits: 6, keep_create_action: false
end

class Company < ::ActiveRecord::Base
audited
end
Expand Down