Skip to content

Commit 125b0d0

Browse files
authored
feat: Added base generator for ActiveRecord (devise-security#447)
* feat: Added base generator for ActiveRecord * test: Update simplecov filters
1 parent 9e2f7d1 commit 125b0d0

File tree

8 files changed

+157
-4
lines changed

8 files changed

+157
-4
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
test/rails_app/log/*
2-
test/rails_app/tmp/*
1+
test/dummy/log/*
2+
test/dummy/tmp/*
33
*~
44
coverage/*
55
*.sqlite3
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+
require_relative '../devise_security/migration_generator'
4+
5+
# nodoc
6+
module ActiveRecord
7+
# nodoc
8+
module Generators
9+
# Generator migration for DeviseSecurity
10+
# Usage:
11+
# rails generate active_record:devise_security
12+
class DeviseSecurityGenerator < ::DeviseSecurity::MigrationGenerator
13+
source_root File.expand_path('templates', __dir__)
14+
15+
def create_migration_file
16+
# TODO: Add some migration here
17+
# add_devise_security_migration 'some_migration_template'
18+
end
19+
end
20+
end
21+
end

lib/generators/active_record/templates/.keep

Whitespace-only changes.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails/generators'
4+
require 'rails/generators/active_record'
5+
6+
# nodoc
7+
module DeviseSecurity
8+
# Basic structure to support a generator that builds a migration
9+
class MigrationGenerator < ::Rails::Generators::Base
10+
include ::Rails::Generators::Migration
11+
12+
# Implement the required interface for Rails::Generators::Migration.
13+
def self.next_migration_number(dirname)
14+
::ActiveRecord::Generators::Base.next_migration_number(dirname)
15+
end
16+
17+
protected
18+
19+
# Creates a devise security migration template.
20+
#
21+
# @param template [String] The name of the migration template
22+
# @param extra_options [Hash] The options Additional options for the migration template (default: {})
23+
# @return [void]
24+
def add_devise_security_migration(template, extra_options = {})
25+
migration_dir = File.expand_path('db/migrate')
26+
return if self.class.migration_exists?(migration_dir, template)
27+
28+
migration_template(
29+
"#{template}.rb.erb",
30+
"db/migrate/#{template}.rb",
31+
{ migration_version: migration_version }.merge(extra_options)
32+
)
33+
end
34+
35+
# Checks if the Rails version is 6.1 or higher
36+
#
37+
# @return [Boolean] true if the Rails version is 6.1 or higher, false otherwise.
38+
def rails61_and_up?
39+
Rails.gem_version >= Gem::Version.new('6.1.0')
40+
end
41+
42+
# Retrieves the ActiveRecord configuration
43+
def ar_config
44+
# Rails 6.0+ uses ActiveRecord::Base.configurations.configs_for
45+
# Read more: https://github.com/rails/rails/pull/38256
46+
if ActiveRecord::Base.configurations.respond_to?(:configs_for)
47+
if rails61_and_up?
48+
# Rails 6.1+ uses ActiveRecord::Base.configurations.configs_for(env_name:, name:)
49+
# Read more: https://github.com/rails/rails/pull/38536
50+
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: 'primary').configuration_hash
51+
else
52+
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: 'primary').config
53+
end
54+
else
55+
ActiveRecord::Base.configurations[Rails.env]
56+
end
57+
end
58+
59+
# Retrieves the migration version
60+
def migration_version
61+
format(
62+
'[%<major>s.%<minor>s]',
63+
major: ActiveRecord::VERSION::MAJOR,
64+
minor: ActiveRecord::VERSION::MINOR
65+
)
66+
end
67+
end
68+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class CreateSomeTable < ActiveRecord::Migration<%= migration_version %>
2+
def up
3+
create_table :some_table do |t|
4+
t.string :adapter_name, default: '<%= ar_config['adapter'] %>'
5+
t.string :rails61_and_up, default: <%= rails61_and_up? %>
6+
t.timestamps null: false
7+
end
8+
end
9+
10+
def down
11+
drop_table :some_table
12+
end
13+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
if DEVISE_ORM == :active_record
6+
require 'generators/active_record/devise_security_generator'
7+
8+
class TestActiveRecordGenerator < Rails::Generators::TestCase
9+
tests ActiveRecord::Generators::DeviseSecurityGenerator
10+
destination File.expand_path('../dummy/tmp', __dir__)
11+
setup :prepare_destination
12+
13+
test 'all files are properly created with migration syntax' do
14+
assert_nothing_raised { run_generator }
15+
end
16+
end
17+
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 'test_helper'
4+
5+
if DEVISE_ORM == :active_record
6+
require 'generators/devise_security/migration_generator'
7+
8+
class TestMigrationGenerator < Rails::Generators::TestCase
9+
class Migration < ::DeviseSecurity::MigrationGenerator
10+
source_root File.expand_path('templates', __dir__)
11+
12+
def create_migration_file
13+
add_devise_security_migration 'create_some_table'
14+
end
15+
end
16+
17+
tests Migration
18+
destination File.expand_path('../dummy/tmp', __dir__)
19+
setup :prepare_destination
20+
21+
test 'all files are properly created with migration syntax' do
22+
assert_nothing_raised { run_generator }
23+
assert_migration 'db/migrate/create_some_table.rb', /create_table :some_table do |t|/
24+
end
25+
end
26+
end

test/test_helper.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
ENV['RAILS_ENV'] ||= 'test'
4+
ENV['DEVISE_ORM'] ||= 'active_record'
45

56
require 'simplecov'
67

@@ -11,8 +12,15 @@
1112
end
1213

1314
SimpleCov.start do
14-
add_filter 'gemfiles'
15-
add_filter 'test/dummy/db'
15+
add_filter %r{^/gemfiles/}
16+
add_filter %r{^/test/dummy/}
17+
if ENV['DEVISE_ORM'] == 'active_record'
18+
add_filter %r{/mongoid/}
19+
else
20+
add_filter %r{^/lib/generators/(active_record|migration_generator)}
21+
add_filter %r{^/test/generators}
22+
add_filter %r{/active_record/}
23+
end
1624
add_group 'ActiveRecord', 'active_record'
1725
add_group 'Expirable', /(?<!password_)expirable/
1826
add_group 'Mongoid', 'mongoid'

0 commit comments

Comments
 (0)