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

Returns a new instance of 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

#metadataObject

Returns the value of attribute metadata.



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

def 
  @metadata
end

#timestampObject

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

Class Method Details

.create!(attrs = {}) ⇒ Object



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

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

Instance Method Details

#_createObject



75
76
77
78
79
80
81
82
# File 'lib/activehistory/event.rb', line 75

def _create
  actions.delete_if { |a| a.diff.empty? }
  payload = JSON.generate(self.as_json)
  ActiveHistory.logger.debug("[ActiveHistory] POST /events WITH #{payload}")
  ActiveHistory.connection.post('/events', payload)
  @actions = []
  @persisted = true
end

#_updateObject



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

def _update
  return if actions.empty?
  actions.delete_if { |a| a.diff.empty? }
  payload = JSON.generate({actions: actions.as_json.map{ |json| json[:event_id] = id; json }})
  ActiveHistory.logger.debug("[ActiveHistory] POST /actions WITH #{payload}")
  ActiveHistory.connection.post('/actions', payload)
  @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
49
50
51
52
53
54
# 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
    if action
      action.diff.merge!(new_options[:diff]) if new_options.has_key?(:diff)
      action
    else
      action!({ subject_type: type, subject_id: id, type: :update }.merge(new_options))
    end

  else
    action
  end
end

#as_jsonObject



84
85
86
87
88
89
90
91
# File 'lib/activehistory/event.rb', line 84

def as_json
  {
    id:                   id,
    metadata:             ,
    timestamp:            timestamp.utc.iso8601(3),
    actions:              actions.as_json
  }
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  @persisted
end

#save!Object



62
63
64
# File 'lib/activehistory/event.rb', line 62

def save!
  persisted? ? _update : _create
end

#to_gid_param(options = {}) ⇒ Object



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

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

#to_global_id(options = {}) ⇒ Object



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

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

#to_sgid_param(options = {}) ⇒ Object



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

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

#to_signed_global_id(options = {}) ⇒ Object



105
106
107
# File 'lib/activehistory/event.rb', line 105

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