Skip to content

Commit ab22a3f

Browse files
committed
Ignore readonly columns in audit
1 parent 3b4e52f commit ab22a3f

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

lib/audited/auditor.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def revision_with(attributes)
231231

232232
private
233233

234-
def audited_changes(for_touch: false)
234+
def audited_changes(for_touch: false, exclude_readonly_attrs: false)
235235
all_changes = if for_touch
236236
previous_changes
237237
elsif respond_to?(:changes_to_save)
@@ -240,6 +240,8 @@ def audited_changes(for_touch: false)
240240
changes
241241
end
242242

243+
all_changes = all_changes.except(*self.class.readonly_attributes.to_a) if exclude_readonly_attrs
244+
243245
filtered_changes = \
244246
if audited_options[:only].present?
245247
all_changes.slice(*self.class.audited_columns)
@@ -333,14 +335,14 @@ def audit_create
333335
end
334336

335337
def audit_update
336-
unless (changes = audited_changes).empty? && (audit_comment.blank? || audited_options[:update_with_comment_only] == false)
338+
unless (changes = audited_changes(exclude_readonly_attrs: true)).empty? && (audit_comment.blank? || audited_options[:update_with_comment_only] == false)
337339
write_audit(action: "update", audited_changes: changes,
338340
comment: audit_comment)
339341
end
340342
end
341343

342344
def audit_touch
343-
unless (changes = audited_changes(for_touch: true)).empty?
345+
unless (changes = audited_changes(for_touch: true, exclude_readonly_attrs: true)).empty?
344346
write_audit(action: "update", audited_changes: changes,
345347
comment: audit_comment)
346348
end

spec/audited/auditor_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,12 @@ def non_column_attr=(val)
358358
Models::ActiveRecord::OnUpdateDestroy.create!(name: "Bart")
359359
}.to_not change(Audited::Audit, :count)
360360
end
361+
362+
it "should save readonly columns" do
363+
expect {
364+
Models::ActiveRecord::UserWithReadOnlyAttrs.create!(name: "Bart")
365+
}.to change(Audited::Audit, :count)
366+
end
361367
end
362368

363369
describe "on update" do
@@ -409,6 +415,16 @@ def non_column_attr=(val)
409415
expect { @user.update_attribute :activated, "1" }.to_not change(Audited::Audit, :count)
410416
end
411417

418+
context "with readonly attributes" do
419+
before do
420+
@user = create_user_with_readonly_attrs(status: "active")
421+
end
422+
423+
it "should not save readonly columns" do
424+
expect { @user.update! status: "banned" }.to_not change(Audited::Audit, :count)
425+
end
426+
end
427+
412428
describe "with no dirty changes" do
413429
it "does not create an audit if the record is not changed" do
414430
expect {

spec/audited_spec_helpers.rb

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ def create_user(attrs = {})
33
Models::ActiveRecord::User.create({name: "Brandon", username: "brandon", password: "password", favourite_device: "Android Phone"}.merge(attrs))
44
end
55

6+
def create_user_with_readonly_attrs(attrs = {})
7+
Models::ActiveRecord::UserWithReadOnlyAttrs.create({name: "Brandon", username: "brandon", password: "password", favourite_device: "Android Phone"}.merge(attrs))
8+
end
9+
610
def build_user(attrs = {})
711
Models::ActiveRecord::User.new({name: "darth", username: "darth", password: "noooooooo"}.merge(attrs))
812
end

spec/support/active_record/models.rb

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ class UserWithEncryptedPassword < ::ActiveRecord::Base
5454
end
5555
end
5656

57+
class UserWithReadOnlyAttrs < ::ActiveRecord::Base
58+
self.table_name = :users
59+
audited
60+
attr_readonly :status
61+
end
62+
5763
class CommentRequiredUser < ::ActiveRecord::Base
5864
self.table_name = :users
5965
audited except: :password, comment_required: true

0 commit comments

Comments
 (0)