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.
-
#ip ⇒ Object
Returns the value of attribute ip.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#performed_by_id ⇒ Object
Returns the value of attribute performed_by_id.
-
#performed_by_type ⇒ Object
Returns the value of attribute performed_by_type.
-
#session_id ⇒ Object
Returns the value of attribute session_id.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
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
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 ||= [] ||= 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 |
#ip ⇒ Object
Returns the value of attribute ip.
11 12 13 |
# File 'lib/activehistory/event.rb', line 11 def ip @ip end |
#metadata ⇒ Object
Returns the value of attribute metadata.
11 12 13 |
# File 'lib/activehistory/event.rb', line 11 def end |
#performed_by_id ⇒ Object
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_type ⇒ Object
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_id ⇒ Object
Returns the value of attribute session_id.
11 12 13 |
# File 'lib/activehistory/event.rb', line 11 def session_id @session_id end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
11 12 13 |
# File 'lib/activehistory/event.rb', line 11 def end |
#user_agent ⇒ Object
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
#_create ⇒ Object
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 |
#_update ⇒ Object
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, =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 action || action!({ subject_type: type, subject_id: id, type: :update }.merge()) else action end end |
#as_json ⇒ Object
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: .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(={}) to_global_id().to_param end |
#to_global_id(options = {}) ⇒ Object
92 93 94 |
# File 'lib/activehistory/event.rb', line 92 def to_global_id(={}) @global_id ||= GlobalID.create(self, { app: :activehistory }.merge()) end |
#to_sgid_param(options = {}) ⇒ Object
96 97 98 |
# File 'lib/activehistory/event.rb', line 96 def to_sgid_param(={}) to_signed_global_id().to_param end |
#to_signed_global_id(options = {}) ⇒ Object
100 101 102 |
# File 'lib/activehistory/event.rb', line 100 def to_signed_global_id(={}) SignedGlobalID.create(self, { app: :activehistory }.merge()) end |