Module: CIA

Defined in:
lib/cia.rb,
lib/cia/event.rb,
lib/cia/version.rb,
lib/cia/auditable.rb,
lib/cia/attribute_change.rb,
lib/cia/source_validation.rb

Defined Under Namespace

Modules: Auditable, SourceValidation Classes: AttributeChange, Event

Constant Summary collapse

VERSION =
'0.5.10'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.exception_handlerObject

Returns the value of attribute exception_handler.



11
12
13
# File 'lib/cia.rb', line 11

def exception_handler
  @exception_handler
end

.non_recordable_attributesObject

Returns the value of attribute non_recordable_attributes.



12
13
14
# File 'lib/cia.rb', line 12

def non_recordable_attributes
  @non_recordable_attributes
end

Class Method Details

.amend_audit(options = {}, &block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/cia.rb', line 23

def self.amend_audit(options = {}, &block)
  if current_transaction
    audit(current_transaction.merge(options), &block)
  else
    audit(options, &block)
  end
end

.audit(options = {}) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/cia.rb', line 15

def self.audit(options = {})
  old = Thread.current[:cia_transaction]
  Thread.current[:cia_transaction] = options
  yield
ensure
  Thread.current[:cia_transaction] = old
end

.current_actorObject



39
40
41
# File 'lib/cia.rb', line 39

def self.current_actor
  current_transaction[:actor] if current_transaction
end

.current_actor=(user) ⇒ Object



35
36
37
# File 'lib/cia.rb', line 35

def self.current_actor=(user)
  current_transaction[:actor] = user if current_transaction
end

.current_transactionObject



31
32
33
# File 'lib/cia.rb', line 31

def self.current_transaction
  Thread.current[:cia_transaction]
end

.record(action, source) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cia.rb', line 43

def self.record(action, source)
  return unless current_transaction
  options = source.class.audited_attribute_options
  return if options and options[:if] and not source.send(options[:if])
  return if options and options[:unless] and source.send(options[:unless])

  changes = (source.cia_previous_changes || source.cia_changes).slice(*source.class.audited_attributes)
  if source.respond_to?(:audit_message)
    message = source.audit_message.presence
    source.audit_message = nil
  end

  return if not message and changes.empty? and action.to_s == "update"

  transaction_attributes = current_transaction.dup
  transaction_attributes.reject! { |k, v| non_recordable_attributes.include?(k) } if non_recordable_attributes

  event = CIA::Event.new(transaction_attributes.merge(
    action: action.to_s,
    source: source,
    message: message
  ))
  event.add_attribute_changes(changes)
  event.save!
  event
rescue Object => e
  if exception_handler
    exception_handler.call e
  else
    raise e
  end
end