active_outbox 0.0.0 → 0.0.2

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: 2cd8cbc9b8aae5d2d62bdb50a0a9e8680f5eecee05009b26c9690853a71680a6
4
- data.tar.gz: a12cf4a81586d3a1cae47f38d1685d47cf7f96b655bd39b3abbdc9411448981f
3
+ metadata.gz: aa92d48552facbc79e9ec9ae9f58a599d533267a490f7199c5b4e87b9f62f83b
4
+ data.tar.gz: a04f7dbe86d0b7e53296079be077db720c50baa48f2b53237cb18423fb33209d
5
5
  SHA512:
6
- metadata.gz: cbcbd18628600a8d67e7fae2fa4d6ecdff185d0c038bca08c21d84bc2532a82afefe512a18a61dd7bba868d02c2032b6ed65e045b069dbf5169764018a462ac7
7
- data.tar.gz: 4990897cf50199b4267ba8df57541570d3e644ea8c3c71f51898c4b19be16dacffff9c03596c0c4c12c706dc9ffeb15332934f8c273b6cf8efa60339e3a54f95
6
+ metadata.gz: acfbc64c4ad058a44d56c170fd194d300841cdfd8bdcaa950b9d63b90ae23f392f3bfdf7c1240e0564d7312680362057360114e9e98e9aaafec842639dd0ab98
7
+ data.tar.gz: 6449a7b730797fb16e55fca130e0ee7d4813bda5d06c34c4093f23bb6e20d77e5bca07cdcc5ed212d30c42617b6035ffcb31c2fc0033b90ebb0653dd6fe6bf25
data/README.md CHANGED
@@ -1,2 +1,2 @@
1
- # active-outbox
1
+ # Active Outbox
2
2
  A Transactional Outbox implementation for ActiveRecord
data/bin/outbox ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rails'
4
+ require 'active_outbox'
5
+
6
+ rails g outbox ARGV[0]
@@ -0,0 +1,26 @@
1
+ module ActiveOutbox
2
+ class << self
3
+ attr_accessor :configuration
4
+
5
+ def configuration
6
+ @configuration ||= ActiveOutbox::Configuration.new
7
+ end
8
+
9
+ def reset
10
+ @configuration = ActiveOutbox::Configuration.new
11
+ end
12
+
13
+ def configure
14
+ yield(configuration)
15
+ end
16
+ end
17
+
18
+ class Configuration
19
+ attr_accessor :adapter, :outbox_mapping
20
+
21
+ def initialize
22
+ @adapter = :sqlite
23
+ @outbox_mapping = {}
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveOutbox
4
+ class OutboxConfigurationError < StandardError; end
5
+ class OutboxClassNotFoundError < OutboxConfigurationError
6
+ def message
7
+ <<~MESSAGE
8
+ Missing Outbox class definition in module. Use `rails generate outbox <outbox model name>`.
9
+ Define default class in `config/initializers/active_outbox.rb`:
10
+
11
+ Rails.application.reloader.to_prepare do
12
+ ActiveOutbox.configure do |config|
13
+ config.outbox_mapping = {
14
+ 'Default' => <outbox model name>,
15
+ 'Meetings' => 'Meetings::Outbox'
16
+ }
17
+ end
18
+ end
19
+ MESSAGE
20
+ end
21
+ end
22
+ end
@@ -1,9 +1,10 @@
1
+ require 'rails'
2
+ require 'rails/generators'
1
3
  require 'rails/generators/active_record'
2
4
 
3
5
  class OutboxGenerator < ActiveRecord::Generators::Base
4
6
  source_root File.expand_path("../templates", __FILE__)
5
7
 
6
- argument :domains, type: :array, default: [], banner: "domain domain"
7
8
  class_option :root_components_path, type: :string, default: Rails.root
8
9
 
9
10
  def create_migration_files
@@ -22,4 +23,16 @@ class OutboxGenerator < ActiveRecord::Generators::Base
22
23
  def table_name
23
24
  "#{name}_outboxes"
24
25
  end
26
+
27
+ def uuid_type
28
+ postgres? ? 'uuid' : 'string'
29
+ end
30
+
31
+ def json_type
32
+ postgres? ? 'jsonb' : 'string'
33
+ end
34
+
35
+ def postgres?
36
+ ActiveRecord::Base.connection.adapter_name.downcase == 'postgresql'
37
+ end
25
38
  end
@@ -1,11 +1,11 @@
1
1
  class OutboxCreate<%= table_name.camelize.singularize %> < ActiveRecord::Migration<%= migration_version %>
2
2
  def change
3
3
  create_table :<%= table_name %> do |t|
4
- t.uuid :identifier, null: false, index: { unique: true }
4
+ t.<%= uuid_type %> :identifier, null: false, index: { unique: true }
5
5
  t.string :event, null: false
6
- t.jsonb :payload
6
+ t.<%= json_type %> :payload
7
7
  t.string :aggregate, null: false
8
- t.uuid :aggregate_identifier, null: false, index: true
8
+ t.<%= uuid_type %> :aggregate_identifier, null: false, index: true
9
9
 
10
10
  t.timestamps
11
11
  end
@@ -4,8 +4,7 @@ module ActiveOutbox
4
4
  module Outboxable
5
5
  extend ActiveSupport::Concern
6
6
 
7
- included do |base|
8
- base.extend(ClassMethods)
7
+ included do
9
8
  *namespace, klass = name.underscore.upcase.split('/')
10
9
  namespace = namespace.reverse.join('.')
11
10
 
@@ -40,27 +39,26 @@ module ActiveOutbox
40
39
  super(**options, &block)
41
40
  end
42
41
 
43
- module ClassMethods
44
- def parent_module_name
45
- module_parent == Object ? '' : module_parent.name
46
- end
47
-
48
- def outbox_model
49
- @outbox_model ||= ActiveOutbox::Base.subclasses.find do |klass|
50
- klass.name.include?(parent_module_name)
51
- end
52
- end
53
- end
54
-
55
42
  private
56
43
 
57
44
  def create_outbox!(action, event_name)
58
- outbox = self.class.outbox_model.new(
45
+ unless self.class.module_parent.const_defined?('OUTBOX_MODEL')
46
+ *namespace, klass = self.class.name.underscore.upcase.split('/')
47
+ namespace = namespace.reverse.join('.')
48
+ outbox_model_name = ActiveOutbox.configuration.outbox_mapping[self.class.module_parent.name.underscore] ||
49
+ ActiveOutbox.configuration.outbox_mapping['default']
50
+ raise OutboxClassNotFoundError if outbox_model_name.nil?
51
+
52
+ outbox_model = outbox_model_name.safe_constantize
53
+ self.class.module_parent.const_set('OUTBOX_MODEL', outbox_model)
54
+ end
55
+
56
+ outbox = self.class.module_parent.const_get('OUTBOX_MODEL').new(
59
57
  aggregate: self.class.name,
60
58
  aggregate_identifier: try(:identifier) || id,
61
59
  event: @outbox_event || event_name,
62
60
  identifier: SecureRandom.uuid,
63
- payload: payload(action)
61
+ payload: formatted_payload(action)
64
62
  )
65
63
  @outbox_event = nil
66
64
 
@@ -73,6 +71,16 @@ module ActiveOutbox
73
71
  outbox.save!
74
72
  end
75
73
 
74
+ def formatted_payload(action)
75
+ payload = payload(action)
76
+ case ActiveRecord::Base.connection.adapter_name.downcase
77
+ when 'postgresql'
78
+ payload
79
+ else
80
+ payload.to_json
81
+ end
82
+ end
83
+
76
84
  def payload(action)
77
85
  payload = { before: nil, after: nil }
78
86
  case action
@@ -0,0 +1,4 @@
1
+ module ActiveOutbox
2
+ class Railtie < Rails::Railtie
3
+ end
4
+ end
data/lib/active_outbox.rb CHANGED
@@ -1,2 +1,5 @@
1
- require 'active_outbox/base'
1
+ require 'active_outbox/configuration'
2
+ require 'active_outbox/errors'
2
3
  require 'active_outbox/outboxable'
4
+ require 'active_outbox/generators/outbox_generator'
5
+ require 'active_outbox/railtie' if defined?(Rails::Railtie)
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_outbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guillermo Aguirre
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-18 00:00:00.000000000 Z
11
+ date: 2023-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '7.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '7.0'
27
27
  - !ruby/object:Gem::Dependency
@@ -124,21 +124,25 @@ dependencies:
124
124
  version: 11.1.3
125
125
  description: A Transactional Outbox implementation for ActiveRecord
126
126
  email: guillermoaguirre1@gmail.com
127
- executables: []
127
+ executables:
128
+ - outbox
128
129
  extensions: []
129
130
  extra_rdoc_files: []
130
131
  files:
131
132
  - README.md
133
+ - bin/outbox
132
134
  - lib/active_outbox.rb
133
- - lib/active_outbox/base.rb
135
+ - lib/active_outbox/configuration.rb
136
+ - lib/active_outbox/errors.rb
134
137
  - lib/active_outbox/generators/outbox_generator.rb
135
138
  - lib/active_outbox/generators/templates/migration.rb
136
139
  - lib/active_outbox/outboxable.rb
140
+ - lib/active_outbox/railtie.rb
137
141
  homepage: https://rubygems.org/gems/active_outbox
138
142
  licenses:
139
143
  - MIT
140
144
  metadata: {}
141
- post_install_message:
145
+ post_install_message:
142
146
  rdoc_options: []
143
147
  require_paths:
144
148
  - lib
@@ -153,8 +157,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
157
  - !ruby/object:Gem::Version
154
158
  version: '0'
155
159
  requirements: []
156
- rubygems_version: 3.4.1
157
- signing_key:
160
+ rubygems_version: 3.1.6
161
+ signing_key:
158
162
  specification_version: 4
159
163
  summary: ActiveOutbox
160
164
  test_files: []
@@ -1,5 +0,0 @@
1
- module ActiveOutbox
2
- class Base < ActiveRecord::Base
3
- self.abstract_class = true
4
- end
5
- end