Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added base generator for ActiveRecord
- Loading branch information
1 parent
9e2f7d1
commit d988cc7
Showing
7 changed files
with
126 additions
and
2 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
test/rails_app/log/* | ||
test/rails_app/tmp/* | ||
test/dummy/log/* | ||
test/dummy/tmp/* | ||
*~ | ||
coverage/* | ||
*.sqlite3 | ||
|
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 | ||
|
||
require_relative '../devise_security/migration_generator' | ||
|
||
# nodoc | ||
module ActiveRecord | ||
# nodoc | ||
module Generators | ||
# Generator migration for DeviseSecurity | ||
# Usage: | ||
# rails generate active_record:devise_security | ||
class DeviseSecurityGenerator < ::DeviseSecurity::MigrationGenerator | ||
source_root File.expand_path('templates', __dir__) | ||
|
||
def create_migration_file | ||
# TODO: Add some migration here | ||
# add_devise_security_migration 'some_migration_template' | ||
end | ||
end | ||
end | ||
end |
Empty file.
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,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails/generators' | ||
require 'rails/generators/active_record' | ||
|
||
# nodoc | ||
module DeviseSecurity | ||
# Basic structure to support a generator that builds a migration | ||
class MigrationGenerator < ::Rails::Generators::Base | ||
include ::Rails::Generators::Migration | ||
|
||
def self.next_migration_number(dirname) | ||
::ActiveRecord::Generators::Base.next_migration_number(dirname) | ||
end | ||
|
||
protected | ||
|
||
def add_devise_security_migration(template, extra_options = {}) | ||
migration_dir = File.expand_path('db/migrate') | ||
return if self.class.migration_exists?(migration_dir, template) | ||
|
||
migration_template( | ||
"#{template}.rb.erb", | ||
"db/migrate/#{template}.rb", | ||
{ migration_version: migration_version }.merge(extra_options) | ||
) | ||
end | ||
|
||
def rails61_and_up? | ||
Rails.gem_version >= Gem::Version.new('6.1.0') | ||
end | ||
|
||
def ar_config | ||
if ActiveRecord::Base.configurations.respond_to?(:configs_for) | ||
if rails61_and_up? | ||
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: 'primary').configuration_hash | ||
else | ||
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: 'primary').config | ||
end | ||
else | ||
ActiveRecord::Base.configurations[Rails.env] | ||
end | ||
end | ||
|
||
def migration_version | ||
format( | ||
'[%<major>s.%<minor>s]', | ||
major: ActiveRecord::VERSION::MAJOR, | ||
minor: ActiveRecord::VERSION::MINOR | ||
) | ||
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,11 @@ | ||
class CreateSomeTable < ActiveRecord::Migration<%= migration_version %> | ||
def up | ||
create_table :some_table do |t| | ||
t.timestamps null: false | ||
end | ||
end | ||
|
||
def down | ||
drop_table :some_table | ||
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,15 @@ | ||
require 'test_helper' | ||
|
||
if DEVISE_ORM == :active_record | ||
require 'generators/active_record/devise_security_generator' | ||
|
||
class TestActiveRecordGenerator < Rails::Generators::TestCase | ||
tests ActiveRecord::Generators::DeviseSecurityGenerator | ||
destination File.expand_path("../../dummy/tmp", __FILE__) | ||
setup :prepare_destination | ||
|
||
test 'all files are properly created with migration syntax' do | ||
assert_nothing_raised { run_generator } | ||
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,24 @@ | ||
require 'test_helper' | ||
|
||
if DEVISE_ORM == :active_record | ||
require 'generators/devise_security/migration_generator' | ||
|
||
class TestMigrationGenerator < Rails::Generators::TestCase | ||
class Migration < ::DeviseSecurity::MigrationGenerator | ||
source_root File.expand_path('templates', __dir__) | ||
|
||
def create_migration_file | ||
add_devise_security_migration 'create_some_table' | ||
end | ||
end | ||
|
||
tests Migration | ||
destination File.expand_path("../../dummy/tmp", __FILE__) | ||
setup :prepare_destination | ||
|
||
test 'all files are properly created with migration syntax' do | ||
assert_nothing_raised { run_generator } | ||
assert_migration 'db/migrate/create_some_table.rb', /create_table :some_table do |t|/ | ||
end | ||
end | ||
end |