Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: handling primary key type in generators #1195

Merged
merged 14 commits into from
Aug 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/generators/devise_token_auth/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module DeviseTokenAuth
class InstallGenerator < Rails::Generators::Base
include Rails::Generators::Migration

class_option :primary_key_type, type: :string, desc: 'The type for primary key'

source_root File.expand_path('templates', __dir__)

argument :user_class, type: :string, default: 'User'
Expand Down Expand Up @@ -156,5 +158,18 @@ def database_name
def database_version
ActiveRecord::Base.connection.select_value('SELECT VERSION()')
end

def rails5?
Rails.version.start_with? '5'
end

def primary_key_type
primary_key_string if rails5?
end

def primary_key_string
key_string = options[:primary_key_type]
", id: :#{key_string}" if key_string
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class DeviseTokenAuthCreate<%= user_class.pluralize.gsub("::","") %> < ActiveRecord::Migration<%= "[#{Rails::VERSION::STRING[0..2]}]" if Rails::VERSION::MAJOR > 4 %>
def change
<% table_name = @user_class.pluralize.gsub("::","").underscore %>
create_table(:<%= table_name %>) do |t|
create_table(:<%= table_name %><%= primary_key_type %>) do |t|
## Required
t.string :provider, :null => false, :default => "email"
t.string :uid, :null => false, :default => ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ class InstallGeneratorTest < Rails::Generators::TestCase
test 'subsequent runs raise no errors' do
run_generator
end

test 'add primary key type with rails 5 when specified in rails generator' do
run_generator %w[--primary_key_type=uuid --force]
if Rails::VERSION::MAJOR >= 5
assert_migration 'db/migrate/devise_token_auth_create_users.rb', /create_table\(:users, id: :uuid\) do/
else
assert_migration 'db/migrate/devise_token_auth_create_users.rb', /create_table\(:users\) do/
end
end
end

describe 'existing user model' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ class InstallGeneratorTest < Rails::Generators::TestCase
test 'subsequent runs raise no errors' do
run_generator %W[#{user_class} auth]
end

test 'add primary key type with rails 5 when specified in rails generator' do
run_generator %W[#{user_class} auth --primary_key_type=uuid --force]
if Rails::VERSION::MAJOR >= 5
assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb", /create_table\(:#{table_name}, id: :uuid\) do/
else
assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb", /create_table\(:#{table_name}\) do/
end
end
end

describe 'existing user model' do
Expand Down