-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mohammed
committed
Aug 5, 2022
1 parent
673e70a
commit de0e63e
Showing
8 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |