Skip to content

Commit 8bf3f5e

Browse files
committed
Only allow an interactive installation if --interactive is provided
- Stop asking for input when running seeds - Ask for everything upfront in the installer - Add options for setting the admin email/password
1 parent bbe4923 commit 8bf3f5e

File tree

2 files changed

+47
-80
lines changed

2 files changed

+47
-80
lines changed

db/default/users.rb

+27-67
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,38 @@
11
# frozen_string_literal: true
22

3-
# see last line where we create an admin if there is none, asking for email and password
4-
def prompt_for_admin_password
5-
if ENV['ADMIN_PASSWORD']
6-
password = ENV['ADMIN_PASSWORD'].dup
7-
puts "Admin Password #{password}"
8-
else
9-
print "Password [test123]: "
10-
password = STDIN.gets.strip
11-
password = 'test123' if password.blank?
12-
end
3+
admin_role = Spree::Role.find_or_create_by(name: 'admin')
134

14-
password
5+
if Spree::User.admin.any?
6+
puts 'No admin user created.'
7+
return
158
end
169

17-
def prompt_for_admin_email
18-
if ENV['ADMIN_EMAIL']
19-
email = ENV['ADMIN_EMAIL'].dup
20-
puts "Admin User #{email}"
21-
else
22-
print "Email [[email protected]]: "
23-
email = STDIN.gets.strip
24-
email = '[email protected]' if email.blank?
25-
end
10+
email = ENV['ADMIN_EMAIL'] || '[email protected]'
11+
password = ENV['ADMIN_PASSWORD'] || 'test123'
2612

27-
email
28-
end
29-
30-
def create_admin_user
31-
if ENV['AUTO_ACCEPT']
32-
password = 'test123'
33-
34-
else
35-
puts 'Create the admin user (press enter for defaults).'
36-
# name = prompt_for_admin_name unless name
37-
email = prompt_for_admin_email
38-
password = prompt_for_admin_password
39-
end
40-
attributes = {
41-
password: password,
42-
password_confirmation: password,
43-
email: email,
44-
login: email
45-
}
46-
47-
load 'spree/user.rb'
13+
puts "Creating admin user with:"
14+
puts " - email: #{email}"
15+
puts " - password: #{password}"
16+
puts "(please use the ADMIN_EMAIL and ADMIN_PASSWORD environment variables to control how the default admin user is created)"
4817

49-
if Spree::User.find_by(email: email)
50-
puts "\nWARNING: There is already a user with the email: #{email}, so no account changes were made. If you wish to create an additional admin user, please run rake spree_auth:admin:create again with a different email.\n\n"
51-
else
52-
admin = Spree::User.new(attributes)
53-
if admin.save
54-
role = Spree::Role.find_or_create_by(name: 'admin')
55-
admin.spree_roles << role
56-
admin.save
57-
admin.generate_spree_api_key!
58-
puts "Done!"
59-
else
60-
puts "There were some problems with persisting a new admin user:"
61-
admin.errors.full_messages.each do |error|
62-
puts error
63-
end
64-
end
65-
end
18+
if Spree::User.find_by(email: email)
19+
warn "WARNING: There is already a user with the email: #{email}, so no account changes were made."
20+
return
6621
end
6722

68-
if Spree::User.admin.empty?
69-
create_admin_user
23+
admin = Spree::User.new(
24+
password: password,
25+
password_confirmation: password,
26+
email: email,
27+
login: email,
28+
)
29+
30+
if admin.save
31+
admin.spree_roles << admin_role
32+
admin.save
33+
admin.generate_spree_api_key!
7034
else
71-
puts 'Admin user has already been created.'
72-
puts 'Would you like to create a new admin user? (yes/no)'
73-
if ["yes", "y"].include? STDIN.gets.strip.downcase
74-
create_admin_user
75-
else
76-
puts 'No admin user created.'
77-
end
35+
warn "There were some problems while creating the admin user:"
36+
warn(admin.errors.full_messages.map { |m| "- #{m}" })
37+
warn "(attributes: #{admin.attributes.inspect})"
7838
end

lib/generators/solidus/auth/install/install_generator.rb

+20-13
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,38 @@ module Solidus
44
module Auth
55
module Generators
66
class InstallGenerator < Rails::Generators::Base
7-
class_option :auto_run_migrations, type: :boolean, default: false
8-
class_option :skip_migrations, type: :boolean, default: false
7+
class_option :auto_run_migrations, type: :boolean, desc: "Run migrations automatically"
8+
class_option :skip_migrations, type: :boolean, desc: "Skip migrations"
99

10-
def self.source_paths
11-
paths = superclass.source_paths
12-
paths << File.expand_path('templates', __dir__)
13-
paths.flatten
14-
end
10+
class_option :interactive, type: :boolean, default: false, desc: "Enable interactive mode"
11+
class_option :admin_email, type: :string
12+
class_option :admin_password, type: :string
13+
14+
source_root "#{__dir__}/templates"
1515

1616
def generate_devise_key
1717
template 'config/initializers/devise.rb', 'config/initializers/devise.rb', skip: true
1818
end
1919

2020
def add_migrations
21-
run 'bundle exec rake railties:install:migrations FROM=solidus_auth'
21+
admin_email = options[:admin_email] || (options[:interactive] && ask("Email:", default: '[email protected]'))
22+
admin_password = options[:admin_password] || (options[:interactive] && ask("Password:", default: 'test123'))
23+
24+
options = []
25+
options << "ADMIN_EMAIL=#{admin_email}" if admin_email
26+
options << "ADMIN_PASSWORD=#{admin_password}" if admin_password
27+
28+
rake "railties:install:migrations FROM=solidus_auth #{options.shelljoin}"
2229
end
2330

2431
def run_migrations
25-
return if options[:skip_migrations]
32+
if options[:skip_migrations] ||
33+
options[:auto_run_migrations] == false || # exclude nil
34+
options[:interactive] && no?('Would you like to run the migrations now?')
2635

27-
run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]'))
28-
if run_migrations
29-
run 'bundle exec rake db:migrate'
36+
say_status :skip, 'Skipping rake db:migrate, don\'t forget to run it!', :yellow
3037
else
31-
puts 'Skipping rake db:migrate, don\'t forget to run it!' # rubocop:disable Rails/Output
38+
rake 'db:migrate'
3239
end
3340
end
3441
end

0 commit comments

Comments
 (0)