Skip to content

Commit

Permalink
feat: Added base generator for ActiveRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmechlark committed Feb 16, 2024
1 parent 9e2f7d1 commit d988cc7
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
@@ -1,5 +1,5 @@
test/rails_app/log/*
test/rails_app/tmp/*
test/dummy/log/*
test/dummy/tmp/*
*~
coverage/*
*.sqlite3
Expand Down
21 changes: 21 additions & 0 deletions lib/generators/active_record/devise_security_generator.rb
@@ -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.
53 changes: 53 additions & 0 deletions lib/generators/devise_security/migration_generator.rb
@@ -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
11 changes: 11 additions & 0 deletions test/generators/templates/create_some_table.rb.erb
@@ -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
15 changes: 15 additions & 0 deletions test/generators/test_active_record_generator.rb
@@ -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
24 changes: 24 additions & 0 deletions test/generators/test_migration_generator.rb
@@ -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

0 comments on commit d988cc7

Please sign in to comment.