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

Proper single-table inheritance support for Audit#revision #368

Open
wants to merge 4 commits 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
15 changes: 12 additions & 3 deletions lib/audited/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,18 @@ def ancestors
# the object has been destroyed, this will be a new record.
def revision
clazz = auditable_type.constantize
(clazz.find_by_id(auditable_id) || clazz.new).tap do |m|
self.class.assign_revision_attributes(m, self.class.reconstruct_attributes(ancestors).merge(version: version))
end
attributes = self.class.reconstruct_attributes(ancestors).merge(version: version)
inheritance_column = clazz.inheritance_column.to_s

instance = if clazz.exists?(id: auditable_id)
clazz.find_by_id(auditable_id)
elsif attributes.key?(inheritance_column)
clazz.new(inheritance_column => attributes.delete(inheritance_column))
else
clazz.new
end

self.class.assign_revision_attributes(instance, attributes)
end

# Returns a hash of the changed attributes with the new values
Expand Down
4 changes: 2 additions & 2 deletions lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module ClassMethods
# * +except+ - Excludes fields from being saved in the audit log.
# By default, Audited will audit all but these fields:
#
# [self.primary_key, inheritance_column, 'lock_version', 'created_at', 'updated_at']
# [self.primary_key, 'lock_version', 'created_at', 'updated_at']
# You can add to those by passing one or an array of fields to skip.
#
# class User < ActiveRecord::Base
Expand Down Expand Up @@ -79,7 +79,7 @@ def has_associated_audits
end

def default_ignored_attributes
[primary_key, inheritance_column]
[primary_key]
end
end

Expand Down
20 changes: 20 additions & 0 deletions spec/audited/audit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ class TempModel < ::ActiveRecord::Base
expect(revision.name).to eq(user.name)
expect(revision).to be_a_new_record
end

context "single-table inheritance" do
it "should return the right subclass for existing records" do
sti_company = Models::ActiveRecord::Company::STICompany.create
revision = sti_company.audits.last.revision
expect(revision).to be_a(Models::ActiveRecord::Company::STICompany)
end

it "should return the right subclass for deleted records" do
sti_company = Models::ActiveRecord::Company::STICompany.create
sti_company.destroy
revision = sti_company.audits.last.revision

if Models::ActiveRecord::Company::STICompany.respond_to?(:attr_accessible)
expect(revision).to be_a(Models::ActiveRecord::Company)
else
expect(revision).to be_a(Models::ActiveRecord::Company::STICompany)
end
end
end
end

it "should set the version number on create" do
Expand Down
10 changes: 8 additions & 2 deletions spec/audited/auditor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

describe "configuration" do
it "should include instance methods" do
expect(Models::ActiveRecord::User.new).to be_a_kind_of( Audited::Auditor::AuditedInstanceMethods)
expect(Models::ActiveRecord::User.new).to be_a_kind_of(Audited::Auditor::AuditedInstanceMethods)
end

it "should include class methods" do
expect(Models::ActiveRecord::User).to be_a_kind_of( Audited::Auditor::AuditedClassMethods )
expect(Models::ActiveRecord::User).to be_a_kind_of(Audited::Auditor::AuditedClassMethods )
end

['created_at', 'updated_at', 'created_on', 'updated_on', 'lock_version', 'id', 'password'].each do |column|
Expand All @@ -17,6 +17,12 @@
end
end

context "single-table inheritance record" do
it "should audit type to retrieve object revisions without loosing inheritance information" do
expect(Models::ActiveRecord::Company::STICompany.non_audited_columns).not_to include('type')
end
end

it "should be configurable which attributes are not audited via ignored_attributes" do
Audited.ignored_attributes = ['delta', 'top_secret', 'created_at']
class Secret < ::ActiveRecord::Base
Expand Down