Module: Ledger::CreatesEvents

Defined in:
lib/ledger/creates_events.rb

Instance Method Summary collapse

Instance Method Details

#create_event(action, object, data = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ledger/creates_events.rb', line 5

def create_event action, object, data = {}
  unless event_scope.respond_to? :add_event
    raise "#{Ledger.event_scope_method} does not respond to #add_event.
    Either include Ledger::HasEvents in that scope, or set the
    'event_scope_method' in your Ledger configuration to an
    object that does."
  end

  object_name = object.class.name == "String" ? \
    object : object.class.name.downcase

  details = {
    action: action,
    object: object_name,
    actor: {},
    data: data || {}
  }

  details[:data].merge! object.respond_to?(:event_details) ? \
    object.event_details : \
    { id: object.id, name: object.name }

  details[:actor].merge! event_actor.respond_to?(:event_details) ? \
    event_actor.event_details : \
    { id: event_actor.id, email: event_actor.email, name: event_actor.name }

  event_scope.add_event Ledger::Event.new(details)
end

#event_actorObject



40
41
42
43
44
# File 'lib/ledger/creates_events.rb', line 40

def event_actor
  send(Ledger.event_actor_method)
rescue NoMethodError
  raise "Hey, I tried calling #{Ledger.event_scope_method} but it doesn't exist. You’ll need to set the `event_actor_method` configuration value to the method that returns your current user object."
end

#event_scopeObject



34
35
36
37
38
# File 'lib/ledger/creates_events.rb', line 34

def event_scope
  send(Ledger.event_scope_method)
rescue NoMethodError
  raise "Hey, I tried calling #{Ledger.event_scope_method} but it doesn't exist. You’ll need to set the `event_scope_method` configuration value to the method that returns your current account object."
end