Skip to content
Snippets Groups Projects
Commit e32a1b1b authored by James Lopez's avatar James Lopez
Browse files

changes to audit event service to cope with new event type

parent 6394d731
No related branches found
No related tags found
No related merge requests found
module Audit
module Changes
extend ActiveSupport::Concern
attr_accessor :current_user
 
module ClassMethods
attr_accessor :current_user
COLUMN_OVERRIDES = { email: :notification_email }
 
module ClassMethods
def audit_changes(column, options)
after_update -> { audit_event(column, options) }, if: ->(model) do
column = "notification_email" if column == :email
column = COLUMN_OVERRIDES[column] || column
 
model.public_send("#{column}_changed?".to_sym)
after_update -> { audit_event(column, options) }, if: ->(model) do
model.public_send("#{column}_changed?")
end
end
end
Loading
Loading
@@ -17,10 +18,14 @@ module Audit
def audit_event(column, options)
raise NotImplementedError, "#{self.class} has no current user assigned." unless self.current_user
 
options.merge!(action: :update_column, column: column)
options.merge!(action: :update,
column: column,
from: self.public_send("#{column}_was"),
to: self.public_send("#{column}")
)
 
AuditEventService.new(self.current_user, self, options)
.for_member(self).security_event
AuditEventService.new(self.current_user, self, options).
for_changes.security_event
end
end
end
Loading
Loading
@@ -44,6 +44,26 @@ class AuditEventService
self
end
 
def for_changes
action = @details[:action]
@details =
case action
when :update
{
change: @details[:as] || @details[:column],
from: @details[:from],
to: @details[:to],
author_name: @author.name,
target_id: @entity,
target_type: @entity.class,
target_details: @entity.name
}
end
self
end
def for_deploy_key(key_title)
action = @details[:action]
author_name = @author.name
Loading
Loading
Loading
Loading
@@ -5,11 +5,13 @@ describe Audit::Changes do
stub_const 'FooUser', create(:user)
FooUser.class_eval{ include Audit::Changes }
FooUser.class_eval{ audit_changes :email, as: 'email_address' }
FooUser.current_user = create(:user)
end
 
describe "non audit changes" do
it 'does not call the audit event service' do
expect(AuditEventService).not_to receive(:new)
expect_any_instance_of(AuditEventService).not_to receive(:for_changes)
 
FooUser.update!(name: 'new name')
end
Loading
Loading
@@ -17,7 +19,7 @@ describe Audit::Changes do
 
describe "audit changes" do
it 'calls the audit event service' do
expect(AuditEventService).to receive(:new)
expect_any_instance_of(AuditEventService).to receive(:for_changes).and_call_original
 
FooUser.update!(email: 'new@email.com')
end
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment