Skip to content

Commit de0e63e

Browse files
author
Mohammed
committed
add base generators
1 parent 673e70a commit de0e63e

File tree

8 files changed

+176
-0
lines changed

8 files changed

+176
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails/generators'
4+
5+
module RequestMigrations
6+
# Intial Files Generator class
7+
class InstallGenerator < Rails::Generators::Base
8+
namespace 'request_migrations:install'
9+
10+
desc 'Generates a request_migrations initializer configuration plus a base migration file.'
11+
12+
source_root File.expand_path('templates', __dir__)
13+
14+
class_option :api_version, type: :string, aliases: '-v', default: '1.1', desc: 'API current version'
15+
class_option :api_prev_version, type: :string, aliases: '-pv', default: '1.0', desc: 'API previoues version'
16+
17+
def copy_base_file
18+
copy_file '../../templates/base_migration.rb', 'app/migrations/base_migration.rb'
19+
end
20+
21+
def create_initializer
22+
@version = options[:api_version] || '1.1'
23+
@prev_version = options[:api_prev_version] || '1.0'
24+
25+
template '../../templates/request_migrations.rb', 'config/initializers/request_migrations.rb'
26+
end
27+
end
28+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails/generators'
4+
5+
module RequestMigrations
6+
# A default migration generator class
7+
class MigrationGenerator < Rails::Generators::NamedBase
8+
namespace 'request_migrations:migration'
9+
10+
ACTIONS = %w[index show create update delete].freeze
11+
DESCRIPTION = 'description goes here'
12+
13+
source_root File.expand_path('../templates', __dir__)
14+
15+
class_option :actions, aliases: '-a', type: :array,
16+
desc: "Select specific actions to generate (#{ACTIONS.join(', ')})"
17+
18+
class_option :description, aliases: '-d', type: :string,
19+
desc: 'Add migration description'
20+
21+
def start
22+
@actions = options[:actions] || ACTIONS
23+
@description = options[:description] || DESCRIPTION
24+
25+
template 'migration.rb', "app/migrations/#{file_name}_migration.rb"
26+
end
27+
end
28+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
class BaseMigration < RequestMigrations::Migration
4+
# Provide a useful description of the change
5+
# description %(description goes here)
6+
7+
# Migrate inputs that contain a resource. The migration should mutate
8+
# the input, whatever that may be.
9+
# Replace resource below with the correct resource name, for eg: user
10+
# migrate if: -> data { data in type: 'resource' } do |data|
11+
# Mutate resource data
12+
# Check link for more details https://github.com/keygen-sh/request_migrations#usage
13+
# end
14+
15+
# Migrate the response. This is where you provide the migration input.
16+
# Replace api/v1/resources below with the correct endpoint name, for eg: api/v1/users
17+
# response if: -> res { res.successful? && res.request.params in controller: 'api/v1/resources',
18+
# action: 'show' } do |res|
19+
# data = JSON.parse(res.body, symbolize_names: true)
20+
21+
# Call our migrate definition above
22+
# migrate!(data)
23+
24+
# res.body = JSON.generate(data)
25+
# end
26+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
class class <%= file_name.singularize.camelize %>Migration < RequestMigrations::Migration
4+
# Provide a useful description of the change
5+
description %(<%= @description %>)
6+
7+
# Migrate inputs that contain a resource_name. The migration should mutate
8+
# the input, whatever that may be.
9+
migrate if: -> data { data in type: 'resource_name' } do |data|
10+
data[:key] = "value"
11+
end
12+
13+
response if: -> res { res.request.params in action: <%= @actions.map{ |action| "'#{action}'" }.join(' | ') %> } do |res|
14+
data = JSON.parse(res.body, symbolize_names: true)
15+
16+
# Call our migrate definition above
17+
migrate!(data)
18+
19+
res.body = JSON.generate(data)
20+
end
21+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
RequestMigrations.configure do |config|
4+
# Define a resolver to determine the target version. Here, you can perform
5+
# a lookup on the current user using request parameters, or simply use
6+
# a header like we are here, defaulting to the latest version.
7+
config.request_version_resolver = -> request {
8+
# request.headers.fetch('Foo-Version') { config.current_version }
9+
}
10+
11+
# Define the latest version of our application.
12+
config.current_version = <%= @version %>
13+
14+
# Define previous versions and their migrations, in descending order.
15+
config.versions = {
16+
'<%= @prev_version %>' => %i[base_migration]
17+
}
18+
end

request_migrations.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ Gem::Specification.new do |spec|
1818
spec.add_dependency "semverse", "~> 3.0"
1919

2020
spec.add_development_dependency "rspec-rails"
21+
spec.add_development_dependency "generator_spec"
2122
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'generator_spec/test_case'
5+
require 'generators/request_migrations/install_generator'
6+
7+
RSpec.describe RequestMigrations::InstallGenerator, type: :generator do
8+
include GeneratorSpec::TestCase
9+
10+
destination File.expand_path('tmp', __dir__)
11+
12+
after do
13+
prepare_destination # cleanup the tmp directory
14+
end
15+
16+
before do
17+
prepare_destination
18+
run_generator
19+
end
20+
21+
it 'generates app/migrations/base_migration.rb' do
22+
assert_file('app/migrations/base_migration.rb')
23+
end
24+
25+
it 'generates config/initializers/request_migrations.rb' do
26+
assert_file('config/initializers/request_migrations.rb')
27+
end
28+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'generator_spec/test_case'
5+
require 'generators/request_migrations/migration_generator'
6+
7+
RSpec.describe RequestMigrations::MigrationGenerator, type: :generator do
8+
include GeneratorSpec::TestCase
9+
10+
let(:file_name) { 'file_name' }
11+
12+
destination File.expand_path('tmp', __dir__)
13+
14+
after do
15+
prepare_destination # cleanup the tmp directory
16+
end
17+
18+
before do
19+
prepare_destination
20+
run_generator [file_name]
21+
end
22+
23+
it 'generates app/migrations/#{file_name}_migration.rb' do
24+
assert_file("app/migrations/#{file_name}_migration.rb")
25+
end
26+
end

0 commit comments

Comments
 (0)