Skip to content

Commit

Permalink
Move gems to Gemfile and improve rubocop config
Browse files Browse the repository at this point in the history
  • Loading branch information
mwolman committed Oct 4, 2023
1 parent b259f2a commit b939e55
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 87 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ jobs:
POSTGRES_HOST: 127.0.0.1
POSTGRES_PORT: 5432
ADAPTER: postgresql
- name: Run RuboCop
run: bundle exec rubocop .

36 changes: 25 additions & 11 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
require:
- rubocop-factory_bot
- rubocop-rspec

inherit_mode:
merge:
- Exclude

AllCops:
NewCops: enable
TargetRubyVersion: 2.7
Exclude:
- lib/active_outbox/generators/templates/migration.rb

Metrics/BlockLength:
Gemspec/RequireMFA:
Enabled: false

Lint/DuplicateMethods:
Exclude:
- app/admin/**/*
- config/**/*
- spec/**/*
- lib/active_outbox/configuration.rb

Metrics/MethodLength:
Max: 12

Style/StringLiteralsInInterpolation:
Enabled: true
EnforcedStyle: double_quotes
RSpec/MultipleMemoizedHelpers:
Max: 7

Layout/LineLength:
Max: 120
RSpec/NestedGroups:
Max: 4

Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented
Style/Documentation:
Enabled: false
13 changes: 10 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ source 'https://rubygems.org'

gemspec

group :test do
gem 'database_cleaner-active_record', '~> 2.0'
end
gem 'byebug', '~> 11.1.3'
gem 'database_cleaner-active_record', '~> 2.1.0'
gem 'pg', '~> 1.5.4'
gem 'pry-rails', '~> 0.3.9'
gem 'reek', '~> 6.1.4'
gem 'rspec', '~> 3.12.0'
gem 'rubocop', '~> 1.56.3', require: false
gem 'rubocop-rspec', '~> 2.24.1', require: false
gem 'simplecov', '~> 0.22.0'
gem 'sqlite3', '1.4.2'
19 changes: 14 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PATH
remote: .
specs:
active_outbox (0.0.2)
rails (~> 7.0)
rails (~> 7.0.8)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -114,7 +114,7 @@ GEM
net-protocol
net-protocol (0.2.1)
timeout
net-smtp (0.3.3)
net-smtp (0.4.0)
net-protocol
nio4r (2.5.9)
nokogiri (1.15.4)
Expand Down Expand Up @@ -201,6 +201,14 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
rubocop-capybara (2.19.0)
rubocop (~> 1.41)
rubocop-factory_bot (2.24.0)
rubocop (~> 1.33)
rubocop-rspec (2.24.1)
rubocop (~> 1.33)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
ruby-progressbar (1.13.0)
simplecov (0.22.0)
docile (~> 1.1)
Expand All @@ -217,7 +225,7 @@ GEM
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.11)
zeitwerk (2.6.12)

PLATFORMS
ruby
Expand All @@ -228,12 +236,13 @@ PLATFORMS
DEPENDENCIES
active_outbox!
byebug (~> 11.1.3)
database_cleaner-active_record (~> 2.0)
database_cleaner-active_record (~> 2.1.0)
pg (~> 1.5.4)
pry-rails (~> 0.3.9)
reek (~> 6.1.4)
rspec (~> 3.0)
rspec (~> 3.12.0)
rubocop (~> 1.56.3)
rubocop-rspec (~> 2.24.1)
simplecov (~> 0.22.0)
sqlite3 (= 1.4.2)

Expand Down
36 changes: 12 additions & 24 deletions active_outbox.gemspec
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
# frozen_string_literal: true

Gem::Specification.new do |s|
s.name = 'active_outbox'
s.version = '0.0.2'
s.summary = 'ActiveOutbox'
s.description = 'A Transactional Outbox implementation for ActiveRecord'
s.authors = ['Guillermo Aguirre']
s.email = '[email protected]'
s.files = Dir['LICENSE.txt', 'README.md', 'lib/**/*', 'lib/active_outbox.rb']
s.executables = ['outbox']
s.homepage =
'https://rubygems.org/gems/active_outbox'
s.license = 'MIT'
s.required_ruby_version = '>= 2.7.0'
Gem::Specification.new do |spec|
spec.authors = ['Guillermo Aguirre']
spec.files = Dir['LICENSE.txt', 'README.md', 'lib/**/*', 'lib/active_outbox.rb']
spec.name = 'active_outbox'
spec.summary = 'A Transactional Outbox implementation for ActiveRecord'
spec.version = '0.0.2'

# Dependencies
s.add_dependency 'rails', '~> 7.0'
spec.email = '[email protected]'
spec.executables = ['outbox']
spec.homepage = 'https://rubygems.org/gems/active_outbox'
spec.license = 'MIT'
spec.required_ruby_version = '>= 2.7.8'

# Development dependencies
s.add_development_dependency 'byebug', '~> 11.1.3'
s.add_development_dependency 'pg', '~> 1.5.4'
s.add_development_dependency 'pry-rails', '~> 0.3.9'
s.add_development_dependency 'reek', '~> 6.1.4'
s.add_development_dependency 'rspec', '~> 3.0'
s.add_development_dependency 'rubocop', '~> 1.56.3'
s.add_development_dependency 'simplecov', '~> 0.22.0'
s.add_development_dependency 'sqlite3', '1.4.2'
spec.add_dependency 'rails', '~> 7.0.8'
end
2 changes: 1 addition & 1 deletion lib/active_outbox/generators/active_outbox_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ActiveOutboxGenerator < ActiveRecord::Generators::Base
class_option :root_components_path, type: :string, default: Rails.root

def create_migration_files
migration_path = "#{options["root_components_path"]}/db/migrate"
migration_path = "#{options['root_components_path']}/db/migrate"
migration_template(
'migration.rb',
"#{migration_path}/outbox_create_#{table_name}.rb",
Expand Down
2 changes: 2 additions & 0 deletions lib/active_outbox/generators/templates/migration.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class OutboxCreate<%= table_name.camelize.singularize %> < ActiveRecord::Migration<%= migration_version %>
def change
create_table :<%= table_name %> do |t|
Expand Down
82 changes: 45 additions & 37 deletions lib/active_outbox/outboxable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Outboxable
const_name = "#{klass}_#{value}"

unless module_parent::Events.const_defined?(const_name)
module_parent::Events.const_set(const_name, "#{const_name}#{namespace.blank? ? "" : "."}#{namespace}")
module_parent::Events.const_set(const_name, "#{const_name}#{namespace.blank? ? '' : '.'}#{namespace}")
end

event_name = module_parent::Events.const_get(const_name)
Expand All @@ -24,32 +24,23 @@ module Outboxable
end

def save(**options, &block)
@outbox_event = options[:outbox_event].underscore.upcase if options[:outbox_event].present?

assign_outbox_event(options)
super(**options, &block)
end

def save!(**options, &block)
@outbox_event = options[:outbox_event].underscore.upcase if options[:outbox_event].present?

assign_outbox_event(options)
super(**options, &block)
end

private

def create_outbox!(action, event_name)
unless self.class.module_parent.const_defined?('OUTBOX_MODEL')
*namespace, _klass = self.class.name.underscore.upcase.split('/')
namespace.reverse.join('.')
outbox_model_name = ActiveOutbox.configuration.outbox_mapping[self.class.module_parent.name.underscore] ||
ActiveOutbox.configuration.outbox_mapping['default']
raise OutboxClassNotFoundError if outbox_model_name.nil?

outbox_model = outbox_model_name.safe_constantize
self.class.module_parent.const_set('OUTBOX_MODEL', outbox_model)
end
def assign_outbox_event(options)
@outbox_event = options[:outbox_event].underscore.upcase if options[:outbox_event].present?
end

outbox = self.class.module_parent.const_get('OUTBOX_MODEL').new(
def create_outbox!(action, event_name)
outbox = outbox_model.new(
aggregate: self.class.name,
aggregate_identifier: try(:identifier) || id,
event: @outbox_event || event_name,
Expand All @@ -58,42 +49,59 @@ def create_outbox!(action, event_name)
)
@outbox_event = nil

if outbox.invalid?
outbox.errors.each do |error|
errors.import(error, attribute: "outbox.#{error.attribute}")
end
handle_outbox_errors(outbox) if outbox.invalid?
outbox.save!
end

def outbox_model
module_parent = self.class.module_parent

unless module_parent.const_defined?('OUTBOX_MODEL')
outbox_model = outbox_model_name!.safe_constantize
module_parent.const_set('OUTBOX_MODEL', outbox_model)
end

outbox.save!
module_parent.const_get('OUTBOX_MODEL')
end

def formatted_payload(action)
payload = payload(action)
case ActiveRecord::Base.connection.adapter_name.downcase
when 'postgresql'
payload
else
payload.to_json
def outbox_model_name!
namespace_outbox_mapping || default_outbox_mapping || raise(OutboxClassNotFoundError)
end

def namespace_outbox_mapping
namespace = self.class.name.split('/').first

ActiveOutbox.configuration.outbox_mapping[namespace&.underscore]
end

def default_outbox_mapping
ActiveOutbox.configuration.outbox_mapping['default']
end

def handle_outbox_errors(outbox)
outbox.errors.each do |error|
errors.import(error, attribute: "outbox.#{error.attribute}")
end
end

def payload(action)
payload = { before: nil, after: nil }
def formatted_payload(action)
payload = construct_payload(action)
AdapterHelper.postgres? ? payload : payload.to_json
end

def construct_payload(action)
case action
when :create
payload[:after] = as_json
{ before: nil, after: as_json }
when :update
# previous_changes => { 'name' => ['bob', 'robert'] }
changes = previous_changes.transform_values(&:first)
payload[:before] = as_json.merge(changes)
payload[:after] = as_json
{ before: as_json.merge(changes), after: as_json }
when :destroy
payload[:before] = as_json
{ before: as_json, after: nil }
else
raise ActiveRecord::RecordNotSaved.new("Failed to create Outbox payload for #{self.class.name}: #{identifier}",
self)
end
payload
end
end
end
File renamed without changes.
10 changes: 5 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
db_config = if ENV['ADAPTER'] == 'postgresql'
{
adapter: 'postgresql',
username: ENV['POSTGRES_USER'],
host: ENV['POSTGRES_HOST'],
port: ENV['POSTGRES_PORT']
username: ENV.fetch('POSTGRES_USER', nil),
host: ENV.fetch('POSTGRES_HOST', nil),
port: ENV.fetch('POSTGRES_PORT', nil)
}
else
{
Expand Down Expand Up @@ -82,12 +82,12 @@ def self.name
DatabaseCleaner.clean_with(:truncation)
end

config.before(:each) do
config.before do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.start
end

config.after(:each) do
config.after do
DatabaseCleaner.clean
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/outboxable_test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module OutboxableTestHelpers
end

match_when_negated(notify_expectation_failures: true) do |actual|
expect { actual.call }.to_not change(outbox_class, :count)
expect { actual.call }.not_to change(outbox_class, :count)
end

supports_block_expectations
Expand Down

0 comments on commit b939e55

Please sign in to comment.