Commit dff7db5a authored by James Lopez's avatar James Lopez

refactor changes framework and fix spec

parent defa48ea
module EE module EE
module Audit module Audit
module Changes module Changes
def audit_changes(column, options = {}) def audit_changes(current_user, column, options = {})
return unless model.send("#{column}_changed?") return unless changed?(column)
@column = column audit_event(current_user, parse_options(column, options))
@options = generate_options(options)
audit_event
end end
protected protected
...@@ -18,20 +15,28 @@ module EE ...@@ -18,20 +15,28 @@ module EE
private private
def generate_options(options) def changed?(column)
model.previous_changes.has_key?(column)
end
def changes(column)
model.previous_changes[column]
end
def parse_options(column, options)
options.tap do |options_hash| options.tap do |options_hash|
options_hash[:column] = @column options_hash[:column] = column
options_hash[:action] = :update options_hash[:action] = :update
unless options[:skip_changes] unless options[:skip_changes]
options_hash[:from] = model.public_send("#{@column}_was") options_hash[:from] = changes(column).first
options_hash[:to] = model.public_send("#{@column}") options_hash[:to] = changes(column).last
end end
end end
end end
def audit_event def audit_event(current_user, options)
AuditEventService.new(@current_user, model, @options). AuditEventService.new(current_user, model, options).
for_changes.security_event for_changes.security_event
end end
end end
......
...@@ -13,8 +13,8 @@ describe EE::Audit::Changes do ...@@ -13,8 +13,8 @@ describe EE::Audit::Changes do
it 'does not call the audit event service' do it 'does not call the audit event service' do
expect_any_instance_of(AuditEventService).not_to receive(:security_event) expect_any_instance_of(AuditEventService).not_to receive(:security_event)
user.name = 'new name' user.update!(name: 'new name')
foo_class.new.audit_changes(:email) foo_class.new.audit_changes(user, :email)
end end
end end
...@@ -22,8 +22,8 @@ describe EE::Audit::Changes do ...@@ -22,8 +22,8 @@ describe EE::Audit::Changes do
it 'calls the audit event service' do it 'calls the audit event service' do
expect_any_instance_of(AuditEventService).to receive(:security_event) expect_any_instance_of(AuditEventService).to receive(:security_event)
user.name = 'new name' user.update!(name: 'new name')
foo_class.new.audit_changes(:name) foo_class.new.audit_changes(user, :name)
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment