active_outbox 0.1.1 → 0.1.2
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.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ef51af40668768f1f8b3acfcb1e81417f78bd676a1639beb6de68d0dd51b728
|
4
|
+
data.tar.gz: 9e86368e8681ac70a24b4fce107bf786685f14ec1b0a8e89bd9e9a31eaae2117
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c79cd51c61496564df983885127498d97317df6ff891f6b524aca691fbb52d396a49f1d7226f25e8cb5f09b7261adc40dc817991ddbd4e8c852cfa43fab6ce69
|
7
|
+
data.tar.gz: 047f6720232490e3cb89379012ff6204e7f89e53b888cd20d2c45228bcb6f0ab9e0a04d6885087d5daedda2856e22640948cd35785b7c64108580195f34e78c0
|
data/README.md
CHANGED
@@ -37,11 +37,11 @@ gem install active_outbox
|
|
37
37
|
|
38
38
|
## Usage
|
39
39
|
### Setup
|
40
|
-
Create an initializer under `config/initializers/active_outbox.rb` and setup the default outbox class to the
|
40
|
+
Create an initializer under `config/initializers/active_outbox.rb` and setup the default outbox class to the `Outbox` model you will create in the next step.
|
41
41
|
```bash
|
42
42
|
rails g active_outbox:install
|
43
43
|
```
|
44
|
-
After creating the initializer, create an `Outbox` table using the provided generator and corresponding model. Any model name can be passed as an argument but if empty it will default to just `
|
44
|
+
After creating the initializer, create an `Outbox` table using the provided generator and corresponding model. Any model name can be passed as an argument but if empty it will default to just `outboxes`. The generated table name will be `model_name_outboxes`.
|
45
45
|
```bash
|
46
46
|
rails g active_outbox:model <optional model_name>
|
47
47
|
```
|
@@ -66,6 +66,12 @@ If you want to persist a custom event other than the provided base events, you c
|
|
66
66
|
user.save(outbox_event: 'YOUR_CUSTOM_EVENT')
|
67
67
|
```
|
68
68
|
## Advanced Usage
|
69
|
+
### Supporting UUIDs
|
70
|
+
By default our Outbox migration has an `aggregate_identifier` field which serves the purpose of identifying which record was involved in the event emission. We default to integer IDs, but if you're using UUIDs as a primary key for your records you have to adjust the migrations accordingly. To do so just run the model generator with the `--uuid` flag.
|
71
|
+
```bash
|
72
|
+
rails g active_outbox:model <optional model_name> --uuid
|
73
|
+
```
|
74
|
+
### Multiple Outbox mappings
|
69
75
|
If more granularity is desired multiple `Outbox` classes can be configured. After creating the needed `Outbox` classes for each module you can specify multiple mappings in the initializer.
|
70
76
|
```ruby
|
71
77
|
# frozen_string_literal: true
|
@@ -73,8 +79,8 @@ If more granularity is desired multiple `Outbox` classes can be configured. Afte
|
|
73
79
|
Rails.application.reloader.to_prepare do
|
74
80
|
ActiveOutbox.configure do |config|
|
75
81
|
config.outbox_mapping = {
|
76
|
-
'
|
77
|
-
'
|
82
|
+
'member' => 'Member::Outbox',
|
83
|
+
'user_access' => 'UserAccess::Outbox'
|
78
84
|
}
|
79
85
|
end
|
80
86
|
end
|
@@ -44,21 +44,20 @@ module ActiveOutbox
|
|
44
44
|
def create_outbox!(action, event_name)
|
45
45
|
outbox = outbox_model.new(
|
46
46
|
aggregate: self.class.name,
|
47
|
-
aggregate_identifier:
|
47
|
+
aggregate_identifier: send(self.class.primary_key),
|
48
48
|
event: @outbox_event || event_name,
|
49
49
|
identifier: SecureRandom.uuid,
|
50
50
|
payload: formatted_payload(action)
|
51
51
|
)
|
52
52
|
@outbox_event = nil
|
53
|
-
|
54
53
|
handle_outbox_errors(outbox) if outbox.invalid?
|
55
54
|
outbox.save!
|
56
55
|
end
|
57
56
|
|
58
57
|
def outbox_model
|
59
58
|
module_parent = self.class.module_parent
|
60
|
-
|
61
|
-
unless module_parent.const_defined?('OUTBOX_MODEL')
|
59
|
+
# sets _inherit_ option to false so it doesn't lookup in ancestors for the constant
|
60
|
+
unless module_parent.const_defined?('OUTBOX_MODEL', false)
|
62
61
|
outbox_model = outbox_model_name!.safe_constantize
|
63
62
|
module_parent.const_set('OUTBOX_MODEL', outbox_model)
|
64
63
|
end
|
@@ -71,9 +70,9 @@ module ActiveOutbox
|
|
71
70
|
end
|
72
71
|
|
73
72
|
def namespace_outbox_mapping
|
74
|
-
namespace = self.class.name.
|
73
|
+
namespace = self.class.module_parent.name.underscore
|
75
74
|
|
76
|
-
ActiveOutbox.config.outbox_mapping[namespace
|
75
|
+
ActiveOutbox.config.outbox_mapping[namespace]
|
77
76
|
end
|
78
77
|
|
79
78
|
def default_outbox_mapping
|
@@ -101,8 +100,10 @@ module ActiveOutbox
|
|
101
100
|
when :destroy
|
102
101
|
{ before: as_json, after: nil }
|
103
102
|
else
|
104
|
-
raise ActiveRecord::RecordNotSaved.new(
|
105
|
-
|
103
|
+
raise ActiveRecord::RecordNotSaved.new(
|
104
|
+
"Failed to create Outbox payload for #{self.class.name}: #{send(self.class.primary_key)}",
|
105
|
+
self
|
106
|
+
)
|
106
107
|
end
|
107
108
|
end
|
108
109
|
end
|
@@ -20,7 +20,13 @@ module ActiveOutbox
|
|
20
20
|
desc 'Creates the Outbox model migration'
|
21
21
|
|
22
22
|
argument :model_name, type: :string, default: ''
|
23
|
-
class_option :component_path,
|
23
|
+
class_option :component_path,
|
24
|
+
type: :string,
|
25
|
+
desc: 'Indicates where to create the outbox migration'
|
26
|
+
class_option :uuid,
|
27
|
+
type: :boolean,
|
28
|
+
default: false,
|
29
|
+
desc: 'Use UUID to identify aggregate records in events. Defaults to ID'
|
24
30
|
|
25
31
|
def create_migration_file
|
26
32
|
migration_path = "#{root_path}/db/migrate"
|
@@ -42,6 +48,10 @@ module ActiveOutbox
|
|
42
48
|
def table_name
|
43
49
|
model_name.blank? ? 'outboxes' : "#{model_name}_outboxes"
|
44
50
|
end
|
51
|
+
|
52
|
+
def aggregate_identifier_type
|
53
|
+
options['uuid'].present? ? ActiveOutbox::AdapterHelper.uuid_type : 'integer'
|
54
|
+
end
|
45
55
|
end
|
46
56
|
end
|
47
57
|
end
|
@@ -7,7 +7,7 @@ class ActiveOutboxCreate<%= table_name.camelize %> < ActiveRecord::Migration<%=
|
|
7
7
|
t.string :event, null: false
|
8
8
|
t.<%= ActiveOutbox::AdapterHelper.json_type %> :payload
|
9
9
|
t.string :aggregate, null: false
|
10
|
-
t.<%=
|
10
|
+
t.<%= aggregate_identifier_type %> :aggregate_identifier, null: false, index: true
|
11
11
|
|
12
12
|
t.timestamps
|
13
13
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_outbox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Guillermo Aguirre
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-11-
|
11
|
+
date: 2023-11-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-configurable
|