active_events 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/active_events.rb +142 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 74482bd9836c264f65a5e18f7a053152a7274877d8e7bef94d06919da728610b
4
+ data.tar.gz: cacd32b5799d9504b7c8c3a97886d76a7e6d4d62f2e2f34ebb7ef4ae7637b5a7
5
+ SHA512:
6
+ metadata.gz: 595b7127f2442eab696562ad464512d3e599f6130c3abe14fa4534f63f0f5612a5ae5ba4f6327e469bb809a0f73ce10f7c593e3be6e1c30b16862c133e130426
7
+ data.tar.gz: 528c3b9c662deca738b968201e80783c2381e27798fb5429ba138052918156c43f1bbe206d145c4563e6495967e6254b20244655f686a312a4c5df33862f1d1f
@@ -0,0 +1,142 @@
1
+ require 'active_record'
2
+
3
+ module ActiveEvents
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def watch_events
8
+ # don't allow multiple calls
9
+ return if included_modules.include?(ActiveEvents::InstanceMethods)
10
+
11
+ extend ActiveEvents::ActiveEventsClassMethods
12
+ include ActiveEvents::InstanceMethods
13
+
14
+ after_create :active_event_create
15
+ before_update :active_event_update
16
+ before_destroy :active_event_destroy
17
+ end
18
+ end
19
+
20
+ module ActiveBunnyEvents
21
+ require 'bunny_event'
22
+
23
+ class CreateEvent
24
+ include BunnyEvent
25
+
26
+ # define the event options for queueing this event. Each event type can have different options.
27
+ event_options exchange: '',
28
+ routing_key: 'create_event'
29
+
30
+ # We can define what the message payload looks like here.
31
+ def initialize(message)
32
+ @message = { message: message }.to_json
33
+ end
34
+ end
35
+
36
+ class UpdateEvent
37
+ include BunnyEvent
38
+
39
+ # define the event options for queueing this event. Each event type can have different options.
40
+ event_options exchange: '',
41
+ routing_key: 'create_event'
42
+
43
+ # We can define what the message payload looks like here.
44
+ def initialize(message)
45
+ @message = { message: message }.to_json
46
+ end
47
+ end
48
+
49
+ class DestroyEvent
50
+ include BunnyEvent
51
+
52
+ # define the event options for queueing this event. Each event type can have different options.
53
+ event_options exchange: '',
54
+ routing_key: 'create_event'
55
+
56
+ # We can define what the message payload looks like here.
57
+ def initialize(message)
58
+ @message = { message: message }.to_json
59
+ end
60
+ end
61
+ end
62
+
63
+ module InstanceMethods
64
+ include ActiveEvents::ActiveBunnyEvents
65
+
66
+ @bunny = nil
67
+
68
+ def event_attributes
69
+ event_attributes = attributes.except(*self.class.skipped_columns)
70
+ normalize_enum_changes(event_attributes)
71
+ end
72
+
73
+ def normalize_enum_changes(changes)
74
+ self.class.defined_enums.each do |name, values|
75
+ if changes.has_key?(name)
76
+ changes[name] = \
77
+ if changes[name].is_a?(Array)
78
+ changes[name].map { |v| values[v] }
79
+ elsif rails_below?('5.0')
80
+ changes[name]
81
+ else
82
+ values[changes[name]]
83
+ end
84
+ end
85
+ end
86
+ changes
87
+ end
88
+
89
+ def event_comment
90
+ ""
91
+ end
92
+
93
+ def init_bunny
94
+ return if @bunny.instance_of? BunnyEvents
95
+
96
+ @bunny = BunnyEvents.new
97
+ @bunny.init Bunny.new(ENV['AMQP_CONNECTION']).start
98
+ end
99
+
100
+ def entity_changes
101
+ respond_to?(:changes_to_save) ? changes_to_save : changes
102
+ end
103
+
104
+ def active_event_create
105
+ init_bunny
106
+ event = {event: 'create', changes: event_attributes, comment: event_comment}
107
+ @bunny.publish CreateEvent.new event
108
+ end
109
+
110
+ def active_event_update
111
+ unless (changes = entity_changes).empty?
112
+ init_bunny
113
+ event = {event: 'update', changes: changes, comment: event_comment}
114
+ @bunny.publish UpdateEvent.new event
115
+ end
116
+ end
117
+
118
+ def active_event_destroy
119
+ unless new_record?
120
+ init_bunny
121
+ event = {event: 'destroy', changes: event_attributes, comment: event_comment}
122
+ @bunny.publish DestroyEvent.new event
123
+ end
124
+ end
125
+ end
126
+
127
+ module ActiveEventsClassMethods
128
+ def event_columns
129
+ @event_columns ||= column_names - skipped_columns
130
+ end
131
+
132
+ def skipped_columns
133
+ @skipped_columns ||= calculate_skipped_columns
134
+ end
135
+
136
+ def calculate_skipped_columns
137
+ [inheritance_column]
138
+ end
139
+ end
140
+ end
141
+
142
+ ::ActiveRecord::Base.send :include, ActiveEvents
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_events
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Rafal Radolinski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Attaches configurable bunny events to create, update and destroy callbacks
14
+ in active record.
15
+ email: radolasd@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/active_events.rb
21
+ homepage: https://rubygems.org/gems/active_events
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.1.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: bunny_events gem extension for Active Record.
44
+ test_files: []