Skip to content

Commit

Permalink
add base generators
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammed committed Aug 5, 2022
1 parent 673e70a commit de0e63e
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/generators/request_migrations/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require 'rails/generators'

module RequestMigrations
# Intial Files Generator class
class InstallGenerator < Rails::Generators::Base
namespace 'request_migrations:install'

desc 'Generates a request_migrations initializer configuration plus a base migration file.'

source_root File.expand_path('templates', __dir__)

class_option :api_version, type: :string, aliases: '-v', default: '1.1', desc: 'API current version'
class_option :api_prev_version, type: :string, aliases: '-pv', default: '1.0', desc: 'API previoues version'

def copy_base_file
copy_file '../../templates/base_migration.rb', 'app/migrations/base_migration.rb'
end

def create_initializer
@version = options[:api_version] || '1.1'
@prev_version = options[:api_prev_version] || '1.0'

template '../../templates/request_migrations.rb', 'config/initializers/request_migrations.rb'
end
end
end
28 changes: 28 additions & 0 deletions lib/generators/request_migrations/migration_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require 'rails/generators'

module RequestMigrations
# A default migration generator class
class MigrationGenerator < Rails::Generators::NamedBase
namespace 'request_migrations:migration'

ACTIONS = %w[index show create update delete].freeze
DESCRIPTION = 'description goes here'

source_root File.expand_path('../templates', __dir__)

class_option :actions, aliases: '-a', type: :array,
desc: "Select specific actions to generate (#{ACTIONS.join(', ')})"

class_option :description, aliases: '-d', type: :string,
desc: 'Add migration description'

def start
@actions = options[:actions] || ACTIONS
@description = options[:description] || DESCRIPTION

template 'migration.rb', "app/migrations/#{file_name}_migration.rb"
end
end
end
26 changes: 26 additions & 0 deletions lib/generators/templates/base_migration.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class BaseMigration < RequestMigrations::Migration
# Provide a useful description of the change
# description %(description goes here)

# Migrate inputs that contain a resource. The migration should mutate
# the input, whatever that may be.
# Replace resource below with the correct resource name, for eg: user
# migrate if: -> data { data in type: 'resource' } do |data|
# Mutate resource data
# Check link for more details https://github.com/keygen-sh/request_migrations#usage
# end

# Migrate the response. This is where you provide the migration input.
# Replace api/v1/resources below with the correct endpoint name, for eg: api/v1/users
# response if: -> res { res.successful? && res.request.params in controller: 'api/v1/resources',
# action: 'show' } do |res|
# data = JSON.parse(res.body, symbolize_names: true)

# Call our migrate definition above
# migrate!(data)

# res.body = JSON.generate(data)
# end
end
21 changes: 21 additions & 0 deletions lib/generators/templates/migration.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

class class <%= file_name.singularize.camelize %>Migration < RequestMigrations::Migration
# Provide a useful description of the change
description %(<%= @description %>)

# Migrate inputs that contain a resource_name. The migration should mutate
# the input, whatever that may be.
migrate if: -> data { data in type: 'resource_name' } do |data|
data[:key] = "value"
end

response if: -> res { res.request.params in action: <%= @actions.map{ |action| "'#{action}'" }.join(' | ') %> } do |res|
data = JSON.parse(res.body, symbolize_names: true)

# Call our migrate definition above
migrate!(data)

res.body = JSON.generate(data)
end
end
18 changes: 18 additions & 0 deletions lib/generators/templates/request_migrations.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

RequestMigrations.configure do |config|
# Define a resolver to determine the target version. Here, you can perform
# a lookup on the current user using request parameters, or simply use
# a header like we are here, defaulting to the latest version.
config.request_version_resolver = -> request {
# request.headers.fetch('Foo-Version') { config.current_version }
}

# Define the latest version of our application.
config.current_version = <%= @version %>

# Define previous versions and their migrations, in descending order.
config.versions = {
'<%= @prev_version %>' => %i[base_migration]
}
end
1 change: 1 addition & 0 deletions request_migrations.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ Gem::Specification.new do |spec|
spec.add_dependency "semverse", "~> 3.0"

spec.add_development_dependency "rspec-rails"
spec.add_development_dependency "generator_spec"
end
28 changes: 28 additions & 0 deletions spec/generators/install_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require 'spec_helper'
require 'generator_spec/test_case'
require 'generators/request_migrations/install_generator'

RSpec.describe RequestMigrations::InstallGenerator, type: :generator do
include GeneratorSpec::TestCase

destination File.expand_path('tmp', __dir__)

after do
prepare_destination # cleanup the tmp directory
end

before do
prepare_destination
run_generator
end

it 'generates app/migrations/base_migration.rb' do
assert_file('app/migrations/base_migration.rb')
end

it 'generates config/initializers/request_migrations.rb' do
assert_file('config/initializers/request_migrations.rb')
end
end
26 changes: 26 additions & 0 deletions spec/generators/migration_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require 'spec_helper'
require 'generator_spec/test_case'
require 'generators/request_migrations/migration_generator'

RSpec.describe RequestMigrations::MigrationGenerator, type: :generator do
include GeneratorSpec::TestCase

let(:file_name) { 'file_name' }

destination File.expand_path('tmp', __dir__)

after do
prepare_destination # cleanup the tmp directory
end

before do
prepare_destination
run_generator [file_name]
end

it 'generates app/migrations/#{file_name}_migration.rb' do
assert_file("app/migrations/#{file_name}_migration.rb")
end
end

0 comments on commit de0e63e

Please sign in to comment.