Skip to content

Commit 29528bb

Browse files
whois-marvin-42MaicolBen
authored andcommitted
Handling primary key type in generators (based on Devise implementation)
1 parent 18b5597 commit 29528bb

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

lib/generators/devise_token_auth/install_generator.rb

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module DeviseTokenAuth
44
class InstallGenerator < Rails::Generators::Base
55
include Rails::Generators::Migration
66

7+
class_option :primary_key_type, type: :string, desc: 'The type for primary key'
8+
79
source_root File.expand_path('templates', __dir__)
810

911
argument :user_class, type: :string, default: 'User'
@@ -156,5 +158,18 @@ def database_name
156158
def database_version
157159
ActiveRecord::Base.connection.select_value('SELECT VERSION()')
158160
end
161+
162+
def rails5?
163+
Rails.version.start_with? '5'
164+
end
165+
166+
def primary_key_type
167+
primary_key_string if rails5?
168+
end
169+
170+
def primary_key_string
171+
key_string = options[:primary_key_type]
172+
", id: :#{key_string}" if key_string
173+
end
159174
end
160175
end

lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class DeviseTokenAuthCreate<%= user_class.pluralize.gsub("::","") %> < ActiveRecord::Migration<%= "[#{Rails::VERSION::STRING[0..2]}]" if Rails::VERSION::MAJOR > 4 %>
22
def change
33
<% table_name = @user_class.pluralize.gsub("::","").underscore %>
4-
create_table(:<%= table_name %>) do |t|
4+
create_table(:<%= table_name %><%= primary_key_type %>) do |t|
55
## Required
66
t.string :provider, :null => false, :default => "email"
77
t.string :uid, :null => false, :default => ""

test/lib/generators/devise_token_auth/install_generator_test.rb

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ class InstallGeneratorTest < Rails::Generators::TestCase
4141
test 'subsequent runs raise no errors' do
4242
run_generator
4343
end
44+
45+
test 'add primary key type with rails 5 when specified in rails generator' do
46+
run_generator %w[--primary_key_type=uuid --force]
47+
if Rails::VERSION::MAJOR >= 5
48+
assert_migration 'db/migrate/devise_token_auth_create_users.rb', /create_table\(:users, id: :uuid\) do/
49+
else
50+
assert_migration 'db/migrate/devise_token_auth_create_users.rb', /create_table\(:users\) do/
51+
end
52+
end
4453
end
4554

4655
describe 'existing user model' do

test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb

+9
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ class InstallGeneratorTest < Rails::Generators::TestCase
4646
test 'subsequent runs raise no errors' do
4747
run_generator %W[#{user_class} auth]
4848
end
49+
50+
test 'add primary key type with rails 5 when specified in rails generator' do
51+
run_generator %W[#{user_class} auth --primary_key_type=uuid --force]
52+
if Rails::VERSION::MAJOR >= 5
53+
assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb", /create_table\(:#{table_name}, id: :uuid\) do/
54+
else
55+
assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb", /create_table\(:#{table_name}\) do/
56+
end
57+
end
4958
end
5059

5160
describe 'existing user model' do

0 commit comments

Comments
 (0)