Class: ActiveHistory::Event

Inherits:
Object
  • Object
show all
Includes:
GlobalID::Identification
Defined in:
lib/activehistory/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Event



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/activehistory/event.rb', line 13

def initialize(attrs={})
  attrs.each do |k,v|
    self.send("#{k}=", v)
  end

  if id
    @persisted = true
  else
    @persisted = false
    @id ||= SecureRandom.uuid
  end

  @actions ||= []
  @timestamp ||= Time.now
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



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

def actions
  @actions
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#ipObject

Returns the value of attribute ip.



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

def ip
  @ip
end

#metadataObject

Returns the value of attribute metadata.



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

def 
  
end

#performed_by_idObject

Returns the value of attribute performed_by_id.



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

def performed_by_id
  @performed_by_id
end

#performed_by_typeObject

Returns the value of attribute performed_by_type.



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

def performed_by_type
  @performed_by_type
end

#session_idObject

Returns the value of attribute session_id.



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

def session_id
  @session_id
end

#timestampObject

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

#user_agentObject

Returns the value of attribute user_agent.



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

def user_agent
  @user_agent
end

Class Method Details

.create!(attrs = {}) ⇒ Object



50
51
52
53
54
# File 'lib/activehistory/event.rb', line 50

def self.create!(attrs={})
  event = self.new(attrs)
  event.save!
  event
end

Instance Method Details

#_createObject



68
69
70
71
72
# File 'lib/activehistory/event.rb', line 68

def _create
  ActiveHistory.connection.post('/events', self.as_json)
  @actions = []
  @persisted = true
end

#_updateObject



60
61
62
63
64
65
66
# File 'lib/activehistory/event.rb', line 60

def _update
  return if actions.empty?
  ActiveHistory.connection.post('/actions', {
    actions: actions.as_json.map{|json| json[:event_id] = id; json}
  })
  @actions = []
end

#action!(action) ⇒ Object



33
34
35
36
37
# File 'lib/activehistory/event.rb', line 33

def action!(action)
  action = ActiveHistory::Action.new(action)
  @actions << action
  action
end

#action_for(type, id, new_options = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/activehistory/event.rb', line 39

def action_for(type, id, new_options=nil)
  type = type.base_class.model_name.name if !type.is_a?(String)
  action = @actions.find { |a| a.subject_type.to_s == type.to_s && a.subject_id.to_s == id.to_s }
  
  if new_options
    action || action!({ subject_type: type, subject_id: id, type: :update }.merge(new_options))
  else
    action
  end
end

#as_jsonObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/activehistory/event.rb', line 74

def as_json
  {
    id:                   id,
    ip:                   ip,
    user_agent:           user_agent,
    session_id:           session_id,
    metadata:             ,
    performed_by_type:    performed_by_type,
    performed_by_id:      performed_by_id,
    timestamp:            timestamp.utc.iso8601(3),
    actions:              actions.as_json
  }
end

#persisted?Boolean



29
30
31
# File 'lib/activehistory/event.rb', line 29

def persisted?
  @persisted
end

#save!Object



56
57
58
# File 'lib/activehistory/event.rb', line 56

def save!
  persisted? ? _update : _create
end

#to_gid_param(options = {}) ⇒ Object



88
89
90
# File 'lib/activehistory/event.rb', line 88

def to_gid_param(options={})
  to_global_id(options).to_param
end

#to_global_id(options = {}) ⇒ Object



92
93
94
# File 'lib/activehistory/event.rb', line 92

def to_global_id(options={})
  @global_id ||= GlobalID.create(self, { app: :activehistory }.merge(options))
end

#to_sgid_param(options = {}) ⇒ Object



96
97
98
# File 'lib/activehistory/event.rb', line 96

def to_sgid_param(options={})
  to_signed_global_id(options).to_param
end

#to_signed_global_id(options = {}) ⇒ Object



100
101
102
# File 'lib/activehistory/event.rb', line 100

def to_signed_global_id(options={})
   SignedGlobalID.create(self, { app: :activehistory }.merge(options))
end