Skip to content

Commit

Permalink
Merge pull request lynndylanhurley#1117 from kaevee/master
Browse files Browse the repository at this point in the history
Support namespace
  • Loading branch information
Maicol Bentancor authored Mar 27, 2018
2 parents d4c0e91 + 86da0c3 commit 5857e39
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 12 deletions.
8 changes: 4 additions & 4 deletions lib/generators/devise_token_auth/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ def create_initializer_file
end

def copy_migrations
if self.class.migration_exists?("db/migrate", "devise_token_auth_create_#{ user_class.underscore }")
say_status("skipped", "Migration 'devise_token_auth_create_#{ user_class.underscore }' already exists")
if self.class.migration_exists?("db/migrate", "devise_token_auth_create_#{ user_class.pluralize.gsub("::","").underscore }")
say_status("skipped", "Migration 'devise_token_auth_create_#{ user_class.pluralize.gsub("::","").underscore }' already exists")
else
migration_template(
"devise_token_auth_create_users.rb.erb",
"db/migrate/devise_token_auth_create_#{ user_class.pluralize.underscore }.rb"
"db/migrate/devise_token_auth_create_#{ user_class.pluralize.gsub("::","").underscore }.rb"
)
end
end
Expand All @@ -29,7 +29,7 @@ def create_user_model
else
inclusion = "include DeviseTokenAuth::Concerns::User"
unless parse_file_for_line(fname, inclusion)

active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
inject_into_file fname, after: "class #{user_class} < #{active_record_needle}\n" do <<-'RUBY'
# Include default devise modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class DeviseTokenAuthCreate<%= user_class.pluralize %> < ActiveRecord::Migration<%= "[#{Rails::VERSION::STRING[0..2]}]" if Rails::VERSION::MAJOR > 4 %>
class DeviseTokenAuthCreate<%= user_class.pluralize.gsub("::","") %> < ActiveRecord::Migration<%= "[#{Rails::VERSION::STRING[0..2]}]" if Rails::VERSION::MAJOR > 4 %>
def change
create_table(:<%= user_class.pluralize.underscore %>) do |t|
<% table_name = @user_class.pluralize.gsub("::","").underscore %>
create_table(:<%= table_name %>) do |t|
## Required
t.string :provider, :null => false, :default => "email"
t.string :uid, :null => false, :default => ""
Expand Down Expand Up @@ -46,10 +47,10 @@ class DeviseTokenAuthCreate<%= user_class.pluralize %> < ActiveRecord::Migration
t.timestamps
end

add_index :<%= user_class.pluralize.underscore %>, :email, unique: true
add_index :<%= user_class.pluralize.underscore %>, [:uid, :provider], unique: true
add_index :<%= user_class.pluralize.underscore %>, :reset_password_token, unique: true
add_index :<%= user_class.pluralize.underscore %>, :confirmation_token, unique: true
# add_index :<%= user_class.pluralize.underscore %>, :unlock_token, unique: true
add_index :<%= table_name %>, :email, unique: true
add_index :<%= table_name %>, [:uid, :provider], unique: true
add_index :<%= table_name %>, :reset_password_token, unique: true
add_index :<%= table_name %>, :confirmation_token, unique: true
# add_index :<%= table_name %>, :unlock_token, unique: true
end
end
3 changes: 2 additions & 1 deletion lib/generators/devise_token_auth/templates/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class <%= user_class.capitalize %> < ActiveRecord::Base

class <%= user_class %> < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
require 'test_helper'
require 'fileutils'
require 'generators/devise_token_auth/install_generator'

module DeviseTokenAuth
class InstallGeneratorTest < Rails::Generators::TestCase
tests InstallGenerator
destination Rails.root.join('tmp/generators')

# The namespaced user model for testing
let(:user_class) { "Azpire::V1::HumanResource::User" }
let(:namespace_path) { user_class.underscore }
let(:table_name) { user_class.pluralize.underscore.gsub("/","_") }

describe 'user model with namespace, clean install' do
setup :prepare_destination

before do
run_generator %W(#{user_class} auth)
end

test 'user model (with namespace) is created, concern is included' do
assert_file "app/models/#{namespace_path}.rb" do |model|
assert_match(/include DeviseTokenAuth::Concerns::User/, model)
end
end

test 'initializer is created' do
assert_file 'config/initializers/devise_token_auth.rb'
end

test 'migration is created for user model with namespace' do
assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb"
end

test 'migration file for user model with namespace contains rails version' do
if Rails::VERSION::MAJOR >= 5
assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb", /#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}/
else
assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb"
end
end

test 'subsequent runs raise no errors' do
run_generator %W(#{user_class} auth)
end
end

describe 'existing user model' do
setup :prepare_destination

before do
@dir = File.join(destination_root, "app", "models")

@fname = File.join(@dir, "user.rb")

# make dir if not exists
FileUtils.mkdir_p(@dir)

# account for rails version 5
active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'

@f = File.open(@fname, 'w') {|f|
f.write <<-RUBY
class User < #{active_record_needle}
def whatever
puts 'whatever'
end
end
RUBY
}

run_generator
end

test 'user concern is injected into existing model' do
assert_file 'app/models/user.rb' do |model|
assert_match(/include DeviseTokenAuth::Concerns::User/, model)
end
end

test 'subsequent runs do not modify file' do
run_generator
assert_file 'app/models/user.rb' do |model|
matches = model.scan(/include DeviseTokenAuth::Concerns::User/m).size
assert_equal 1, matches
end
end
end


describe 'routes' do
setup :prepare_destination

before do
@dir = File.join(destination_root, "config")

@fname = File.join(@dir, "routes.rb")

# make dir if not exists
FileUtils.mkdir_p(@dir)

@f = File.open(@fname, 'w') {|f|
f.write <<-RUBY
Rails.application.routes.draw do
patch '/chong', to: 'bong#index'
end
RUBY
}

run_generator %W(#{user_class} auth)
end

test 'route method for user model with namespace is appended to routes file' do
assert_file 'config/routes.rb' do |routes|
assert_match(/mount_devise_token_auth_for '#{user_class}', at: 'auth'/, routes)
end
end

test 'subsequent runs do not modify file' do
run_generator %W(#{user_class} auth)
assert_file 'config/routes.rb' do |routes|
matches = routes.scan(/mount_devise_token_auth_for '#{user_class}', at: 'auth'/m).size
assert_equal 1, matches
end
end

describe 'subsequent models' do
before do
run_generator %w(Mang mangs)
end

test 'migration is created' do
assert_migration 'db/migrate/devise_token_auth_create_mangs.rb'
end

test 'route method is appended to routes file' do
assert_file 'config/routes.rb' do |routes|
assert_match(/mount_devise_token_auth_for 'Mang', at: 'mangs'/, routes)
end
end

test 'devise_for block is appended to routes file' do
assert_file 'config/routes.rb' do |routes|
assert_match(/as :mang do/, routes)
assert_match(/# Define routes for Mang within this block./, routes)
end
end
end
end

describe 'application controller' do
setup :prepare_destination

before do
@dir = File.join(destination_root, "app", "controllers")

@fname = File.join(@dir, "application_controller.rb")

# make dir if not exists
FileUtils.mkdir_p(@dir)

@f = File.open(@fname, 'w') {|f|
f.write <<-RUBY
class ApplicationController < ActionController::Base
def whatever
'whatever'
end
end
RUBY
}

run_generator %W(#{user_class} auth)
end

test 'controller concern is appended to application controller' do
assert_file 'app/controllers/application_controller.rb' do |controller|
assert_match(/include DeviseTokenAuth::Concerns::SetUserByToken/, controller)
end
end

test 'subsequent runs do not modify file' do
run_generator %W(#{user_class} auth)
assert_file 'app/controllers/application_controller.rb' do |controller|
matches = controller.scan(/include DeviseTokenAuth::Concerns::SetUserByToken/m).size
assert_equal 1, matches
end
end
end
end
end

0 comments on commit 5857e39

Please sign in to comment.