Skip to content

Commit

Permalink
Limit number of audits stored
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Jan 31, 2018
1 parent 114a089 commit c113b28
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/audited/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ def undo
end
end

def merge!(other)
raise ArgumentError, "#{other.inspect} should belong to the same auditable" \
if other.auditable_type != auditable_type || other.auditable_id != auditable_id
update_column(:audited_changes, other.audited_changes.merge(audited_changes))
end

# Allows user to be set to either a string or an ActiveRecord object
# @private
def user_as_string=(user)
Expand Down
19 changes: 17 additions & 2 deletions lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ def audited(options = {})

class_attribute :audit_associated_with, instance_writer: false
class_attribute :audited_options, instance_writer: false
class_attribute :keep_audits, instance_writer: false
attr_accessor :version, :audit_comment

self.audited_options = options
normalize_audited_options

self.audit_associated_with = audited_options[:associated_with]
self.keep_audits = audited_options[:keep_audits]

if audited_options[:comment_required]
validates_presence_of :audit_comment, if: :auditing_enabled
Expand Down Expand Up @@ -204,7 +206,13 @@ def audit_create
def audit_update
unless (changes = audited_changes).empty? && audit_comment.blank?
write_audit(action: 'update', audited_changes: changes,
comment: audit_comment)
comment: audit_comment) do
if keep_audits && audits.count > keep_audits.to_i
first_audit, second_audit = audits.limit(2)
second_audit.merge!(first_audit)
first_audit.destroy!
end
end
end
end

Expand All @@ -216,7 +224,14 @@ def audit_destroy
def write_audit(attrs)
attrs[:associated] = send(audit_associated_with) unless audit_associated_with.nil?
self.audit_comment = nil
run_callbacks(:audit) { audits.create(attrs) } if auditing_enabled

if auditing_enabled
run_callbacks(:audit) {
audit = audits.create(attrs)
yield if block_given?
audit
}
end
end

def require_comment
Expand Down
25 changes: 25 additions & 0 deletions spec/audited/audit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,31 @@ class Models::ActiveRecord::CustomUserSubclass < Models::ActiveRecord::CustomUse
end
end

describe "merge!" do
it "should merge two audits" do
user = Models::ActiveRecord::User.create!(name: "John")
user.update(username: "john")
user.update(name: "John Doe")
expect(user.audits.count).to eq(3)

second, third = user.audits.offset(1)
third_created_at = third.created_at
third.merge!(second)
expect(third.reload.audited_changes).to eq({ "name" => ["John", "John Doe"], "username" => [nil, "john"] })
expect(third.created_at).to eq(third_created_at)
expect(user.audits.count).to eq(3)
end

it "should raise when audit belongs to other auditable" do
audit = Audited::Audit.new(auditable_type: "User", auditable_id: 1)
for_other = Audited::Audit.new(auditable_type: "User", auditable_id: 2)

assert_raises(ArgumentError) do
audit.merge!(for_other)
end
end
end

describe "as_user" do
it "should record user objects" do
Audited::Audit.as_user(user) do
Expand Down
30 changes: 30 additions & 0 deletions spec/audited/auditor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,36 @@ def non_column_attr=(val)
end
end

describe "keep_audits" do
it "should be nil by default" do
expect(Models::ActiveRecord::User.keep_audits).to be_nil
end

it "should delete old audits when keeped amount exceeded" do
previous_keep_audits = Models::ActiveRecord::User.keep_audits
begin
Models::ActiveRecord::User.keep_audits = 2
user = create_versions(2)
user.update(name: "John")
expect(user.audits.pluck(:version)).to eq([2, 3])
ensure
Models::ActiveRecord::User.keep_audits = previous_keep_audits
end
end

it "should not delete old audits when keeped amount not exceeded" do
previous_keep_audits = Models::ActiveRecord::User.keep_audits
begin
Models::ActiveRecord::User.keep_audits = 3
user = create_versions(2)
user.update(name: "John")
expect(user.audits.pluck(:version)).to eq([1, 2, 3])
ensure
Models::ActiveRecord::User.keep_audits = previous_keep_audits
end
end
end

describe "revisions" do
let( :user ) { create_versions }

Expand Down

0 comments on commit c113b28

Please sign in to comment.