-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathentities.rb
More file actions
61 lines (49 loc) · 2.24 KB
/
entities.rb
File metadata and controls
61 lines (49 loc) · 2.24 KB
1
2
3
4
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true
module DfE
module Analytics
module Entities
extend ActiveSupport::Concern
included do
attr_accessor :event_tags
after_create_commit do
extracted_attributes = DfE::Analytics.extract_model_attributes(self)
object_id = self.object_id
send_event('create_entity', extracted_attributes) if extracted_attributes.any?
Rails.logger.info("Entity created with attributes: #{self}, #{object_id}")
Rails.logger.info("Entity created with attributes: #{extracted_attributes}")
end
after_destroy_commit do
extracted_attributes = DfE::Analytics.extract_model_attributes(self)
send_event('delete_entity', extracted_attributes) if extracted_attributes.any?
end
after_update_commit do
# in this after_update hook we don't have access to the new fields via
# attributes or saved changes in transactions, so we use the
# TransactionChanges module
updated_attributes = DfE::Analytics.extract_model_attributes(self, changed_attributes)
allowed_attributes = DfE::Analytics.extract_model_attributes(self).deep_merge(updated_attributes)
send_event('update_entity', allowed_attributes) if updated_attributes.any?
Rails.logger.info("Entity updated with attributes: #{self}")
Rails.logger.info("Entity updated with attributes: #{allowed_attributes}")
end
end
def changed_attributes
changed_attributes = {}
transaction_changed_attributes.each_key do |name|
changed_attributes.merge!(name => send(name))
end
changed_attributes
end
def send_event(type, data)
return unless DfE::Analytics.enabled?
event = DfE::Analytics::Event.new
.with_type(type)
.with_entity_table_name(self.class.table_name)
.with_data(data)
.with_tags(event_tags)
.with_request_uuid(RequestLocals.fetch(:dfe_analytics_request_id) { nil })
DfE::Analytics::SendEvents.do([event.as_json])
end
end
end
end