Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor generators #15

Merged
merged 9 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ AllCops:
NewCops: enable
TargetRubyVersion: 3.0
Exclude:
- lib/active_outbox/generators/templates/migration.rb
- lib/generators/active_outbox/templates/migration.rb

Gemspec/RequireMFA:
Enabled: false
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
active_outbox (0.0.7)
active_outbox (0.1.0)
dry-configurable (~> 1.0)
rails (>= 6.1)

Expand Down
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,13 @@ gem install active_outbox

## Usage
### Setup
Create an `Outbox` table using the provided generator and corresponding model.
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`.
```bash
rails g active_outbox outbox
rails g active_outbox:model <optional model_name>
```
After running the migration, create an initializer under `config/initializers/active_outbox.rb` and setup the default outbox class to the new `Outbox` model you just created.
```ruby
# frozen_string_literal: true

Rails.application.reloader.to_prepare do
ActiveOutbox.configure do |config|
config.outbox_mapping = {
'default' => 'Outbox'
}
end
end
```bash
rails g active_outbox:install
```

To allow models to store Outbox records on changes, you will have to include the `Outboxable` concern.
Expand Down
2 changes: 1 addition & 1 deletion active_outbox.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Gem::Specification.new do |spec|
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.7'
spec.version = '0.1.0'

spec.email = 'guillermoaguirre1@gmail.com'
spec.executables = ['outbox']
Expand Down
1 change: 0 additions & 1 deletion lib/active_outbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require 'active_outbox/adapter_helper'
require 'active_outbox/errors'
require 'active_outbox/generators/active_outbox_generator'
require 'active_outbox/outboxable'
require 'active_outbox/railtie' if defined?(Rails::Railtie)
require 'dry-configurable'
Expand Down
33 changes: 0 additions & 33 deletions lib/active_outbox/generators/active_outbox_generator.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/active_outbox/outboxable.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'active_support/concern'

module ActiveOutbox
module Outboxable
extend ActiveSupport::Concern
Expand Down
17 changes: 17 additions & 0 deletions lib/generators/active_outbox/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require 'rails/generators/base'

module ActiveOutbox
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __dir__)

desc 'Creates an initializer file at config/initializers/active_outbox.rb'

def create_initializer_file
copy_file('initializer.rb', Rails.root.join('config', 'initializers', 'active_outbox.rb'))
end
end
end
end
47 changes: 47 additions & 0 deletions lib/generators/active_outbox/model/model_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require 'rails'
require 'rails/generators/active_record'
require 'rails/generators/base'
require 'rails/generators/migration'

module ActiveOutbox
module Generators
class ModelGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __dir__)

include ActiveOutbox::AdapterHelper
include Rails::Generators::Migration

class << self
delegate :next_migration_number, to: ActiveRecord::Generators::Base
end

desc 'Creates the Outbox model migration'

argument :model_name, type: :string, default: ''
class_option :component_path, type: :string, desc: 'Indicates where to create the outbox migration'

def create_migration_file
migration_path = "#{root_path}/db/migrate"
migration_template(
'migration.rb',
"#{migration_path}/active_outbox_create_#{table_name}.rb",
migration_version: migration_version
)
end

def root_path
options['component_path'] || Rails.root
end

def migration_version
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
end

def table_name
model_name.blank? ? 'outboxes' : "#{model_name}_outboxes"
end
end
end
end
14 changes: 14 additions & 0 deletions lib/generators/active_outbox/templates/initializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

Rails.application.reloader.to_prepare do
ActiveOutbox.configure do |config|
# To configure which Outbox class maps to which domain
# See https://github.com/rootstrap/active_outbox#advanced-usage for advanced examples
config.outbox_mapping = {
'default' => 'Outbox'
}

# Configure database adapter
# config.adapter = :postgresql
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class OutboxCreate<%= table_name.camelize.singularize %> < ActiveRecord::Migration<%= migration_version %>
class ActiveOutboxCreate<%= table_name.camelize.singularize %> < ActiveRecord::Migration<%= migration_version %>
def change
create_table :<%= table_name %> do |t|
t.<%= ActiveOutbox::AdapterHelper.uuid_type %> :identifier, null: false, index: { unique: true }
Expand Down
139 changes: 0 additions & 139 deletions spec/generators/active_outbox_generator_spec.rb

This file was deleted.

48 changes: 48 additions & 0 deletions spec/lib/active_outbox/generators/install_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require 'spec_helper'
require 'generator_spec'
require 'tempfile'
require 'generators/active_outbox/install/install_generator'

RSpec.describe ActiveOutbox::Generators::InstallGenerator, type: :generator do
destination File.expand_path('tmp', __dir__)

let(:root) { double }
let(:initializer_file_path) { "#{destination_root}/config/initializers/active_outbox.rb" }
let(:actual_content) { File.read(initializer_file_path) }
let(:expected_content) do
<<~FILE
Rails.application.reloader.to_prepare do
ActiveOutbox.configure do |config|
# To configure which Outbox class maps to which domain
# See https://github.com/rootstrap/active_outbox#advanced-usage for advanced examples
config.outbox_mapping = {
'default' => 'Outbox'
}

# Configure database adapter
# config.adapter = :postgresql
end
end
FILE
end

before do
prepare_destination
allow(Rails).to receive(:root).and_return(root)
allow(root).to receive(:join).and_return(initializer_file_path)
end

after { FileUtils.rm_rf(destination_root) }

it 'creates the initializer' do
run_generator
assert_file initializer_file_path
end

it 'creates the correct file' do
run_generator
expect(actual_content).to include(expected_content)
end
end
Loading