Class: ActiveHistory::Event
- Inherits:
-
Object
- Object
- ActiveHistory::Event
- Includes:
- GlobalID::Identification
- Defined in:
- lib/activehistory/event.rb
Instance Attribute Summary collapse
-
#actions ⇒ Object
Returns the value of attribute actions.
-
#id ⇒ Object
Returns the value of attribute id.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
Class Method Summary collapse
Instance Method Summary collapse
- #_create ⇒ Object
- #_update ⇒ Object
- #action!(action) ⇒ Object
- #action_for(type, id, new_options = nil) ⇒ Object
- #as_json ⇒ Object
-
#initialize(attrs = {}) ⇒ Event
constructor
A new instance of Event.
- #persisted? ⇒ Boolean
- #save! ⇒ Object
- #to_gid_param(options = {}) ⇒ Object
- #to_global_id(options = {}) ⇒ Object
- #to_sgid_param(options = {}) ⇒ Object
- #to_signed_global_id(options = {}) ⇒ Object
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
#actions ⇒ Object
Returns the value of attribute actions.
11 12 13 |
# File 'lib/activehistory/event.rb', line 11 def actions @actions end |
#id ⇒ Object
Returns the value of attribute id.
11 12 13 |
# File 'lib/activehistory/event.rb', line 11 def id @id end |
#metadata ⇒ Object
Returns the value of attribute metadata.
11 12 13 |
# File 'lib/activehistory/event.rb', line 11 def @metadata end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
11 12 13 |
# File 'lib/activehistory/event.rb', line 11 def @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
#_create ⇒ Object
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 |
#_update ⇒ Object
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, =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 if action action.diff.merge!([:diff]) if .has_key?(:diff) action else action!({ subject_type: type, subject_id: id, type: :update }.merge()) end else action end end |
#as_json ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/activehistory/event.rb', line 84 def as_json { id: id, metadata: , 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
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(={}) to_global_id().to_param end |
#to_global_id(options = {}) ⇒ Object
97 98 99 |
# File 'lib/activehistory/event.rb', line 97 def to_global_id(={}) @global_id ||= GlobalID.create(self, { app: :activehistory }.merge()) end |
#to_sgid_param(options = {}) ⇒ Object
101 102 103 |
# File 'lib/activehistory/event.rb', line 101 def to_sgid_param(={}) to_signed_global_id().to_param end |
#to_signed_global_id(options = {}) ⇒ Object
105 106 107 |
# File 'lib/activehistory/event.rb', line 105 def to_signed_global_id(={}) SignedGlobalID.create(self, { app: :activehistory }.merge()) end |