active_outbox 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fad444e8c23086a1afdf59fe196c7a866b7c70460013de5e6b686643851b6ce8
4
- data.tar.gz: 0be6703e8c5522ed853e59fe91b6bd4636c4ea7e819726955b67e84ed95eba93
3
+ metadata.gz: 0e7706d5483124adb87010ceb0c1dc612dbbef07e4cbb6f52eee2a0f817f99e9
4
+ data.tar.gz: f8762e7bc2517a5f2720d2d9617a29d8720cc18fa1cad2129ba017c5e95ef012
5
5
  SHA512:
6
- metadata.gz: 06bbf4b9548f6e1f1dd1e8eb7e66269a7bb696350a9eda5baafd4abb1cb0c8b3982062107dc62ae9a65a92f6b99578eebbed8f3beb63bacaab8ac924602a7972
7
- data.tar.gz: d37e3a4a6f447fc2fa48d34efe65eaebc25e1d4269d4f149e0b9d9ba8a01db4a797532df28b0cdbac8eb864e30cb03bcc581277df40278e6f4cf2916bd70bf7a
6
+ metadata.gz: 277b1350289860ef9c2a33406fee80d8858ddf7dae84b62c0e960fe442e1645087d7c15ecd610e03e937faf8de8114c15a884f5b7807e276be0b7f58fcbff17b
7
+ data.tar.gz: a93fb735486c3ed1abfc524798e260b2945844b72bbf2a671e8ef04b48de15ed52fca3756e79882359599040a69055871d97f29c15a07bab94617590c38d2b68
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 new `Outbox` model you just created.
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 `Outobx`. The generated table name will be `model_name_outboxes`.
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
- 'Member' => 'Member::Outbox',
77
- 'UserAccess' => 'UserAccess::Outbox'
82
+ 'member' => 'Member::Outbox',
83
+ 'user_access' => 'UserAccess::Outbox'
78
84
  }
79
85
  end
80
86
  end
@@ -16,6 +16,12 @@ module ActiveOutbox
16
16
  'string'
17
17
  end
18
18
 
19
+ def self.bigint_type
20
+ return 'bigint' if postgres? || mysql?
21
+
22
+ 'integer'
23
+ end
24
+
19
25
  def self.postgres?
20
26
  ActiveRecord::Base.connection.adapter_name.downcase == 'postgresql'
21
27
  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: try(:identifier) || id,
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.split('/').first
73
+ namespace = self.class.module_parent.name.underscore
75
74
 
76
- ActiveOutbox.config.outbox_mapping[namespace&.underscore]
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("Failed to create Outbox payload for #{self.class.name}: #{identifier}",
105
- self)
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, type: :string, desc: 'Indicates where to create the outbox migration'
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 : ActiveOutbox::AdapterHelper.bigint_type
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.<%= ActiveOutbox::AdapterHelper.uuid_type %> :aggregate_identifier, null: false, index: true
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.1
4
+ version: 0.1.3
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-08 00:00:00.000000000 Z
11
+ date: 2023-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-configurable