Skip to content

Commit f82140f

Browse files
authored
Merge pull request #233 from nebulab/elia/fix-the-standard-generator
Fix the standard generator alias and remove interactivity from seeds
2 parents 8d21a46 + 720907e commit f82140f

9 files changed

Lines changed: 71 additions & 96 deletions

File tree

.circleci/config.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ jobs:
1616
name: solidusio_extensions/postgres
1717
ruby_version: '3.1'
1818
steps:
19-
- browser-tools/install-browser-tools
2019
- checkout
20+
- browser-tools/install-chrome
2121
- solidusio_extensions/run-tests-solidus-master
2222
- solidusio_extensions/store-test-results
2323

@@ -26,8 +26,8 @@ jobs:
2626
name: solidusio_extensions/mysql
2727
ruby_version: '3.0'
2828
steps:
29-
- browser-tools/install-browser-tools
3029
- checkout
30+
- browser-tools/install-chrome
3131
- solidusio_extensions/run-tests-solidus-current
3232
- solidusio_extensions/store-test-results
3333

@@ -36,15 +36,14 @@ jobs:
3636
name: solidusio_extensions/sqlite
3737
ruby_version: '2.7'
3838
steps:
39-
- browser-tools/install-browser-tools
4039
- checkout
40+
- browser-tools/install-chrome
4141
- solidusio_extensions/run-tests-solidus-older
4242
- solidusio_extensions/store-test-results
4343

4444
lint-code:
45-
executor: solidusio_extensions/sqlite-memory
45+
executor: solidusio_extensions/sqlite
4646
steps:
47-
- browser-tools/install-browser-tools
4847
- solidusio_extensions/lint-code
4948

5049
workflows:

Gemfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3')
2525
# the 'async' gem that relies on the latest ruby, since RubyGems doesn't
2626
# resolve gems based on the required ruby version.
2727
gem 'async', '< 3', require: false
28-
29-
# 'net/smtp' is required by 'mail', see:
30-
# - https://github.com/ruby/net-protocol/issues/10
31-
# - https://stackoverflow.com/a/72474475
32-
gem 'net-smtp', require: false
3328
end
3429

30+
# 'net/smtp' is required by 'mail', see:
31+
# - https://github.com/ruby/net-protocol/issues/10
32+
# - https://stackoverflow.com/a/72474475
33+
gem 'net-smtp', require: false
34+
3535
gemspec
3636

3737
# Use a local Gemfile to include development dependencies that might not be

bin/rails

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env ruby
22

3-
if %w[g generate].include? ARGV.first
3+
if %w[g generate].include?(ARGV.first) && ARGV[1] !~ /^(solidus:auth:|solidus_auth_devise:)/
44
exec "#{__dir__}/rails-engine", *ARGV
55
else
66
exec "#{__dir__}/rails-sandbox", *ARGV

bin/sandbox

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ cat <<RUBY >> Gemfile
7171
gem 'solidus', github: 'solidusio/solidus', branch: '$BRANCH'
7272
gem 'rails-i18n'
7373
gem 'solidus_i18n'
74+
gem 'net-smtp', require: false
7475
7576
gem '$extension_name', path: '..'
7677

db/default/users.rb

Lines changed: 27 additions & 67 deletions
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 [admin@example.com]: "
23-
email = STDIN.gets.strip
24-
email = 'admin@example.com' if email.blank?
25-
end
10+
email = ENV['ADMIN_EMAIL'] || 'admin@example.com'
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-
email = 'admin@example.com'
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

Lines changed: 20 additions & 13 deletions
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: 'admin@example.com'))
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
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
# frozen_string_literal: true
22

33
require_relative '../../solidus/auth/install/install_generator'
4+
45
module SolidusAuthDevise
56
module Generators
6-
InstallGenerator = ::Solidus::Auth::Generators::InstallGenerator
7+
class InstallGenerator < Rails::Generators::Base
8+
# Copy over any class option from the legacy install generator
9+
Solidus::Auth::Generators::InstallGenerator.class_options.each do |name, option|
10+
class_options[name] ||= option.dup
11+
end
12+
13+
def forward_to_spree_auth_install
14+
generate 'solidus:auth:install', *ARGV
15+
end
16+
end
717
end
818
end

lib/solidus_auth_devise/engine.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
# frozen_string_literal: true
22

33
require 'spree/auth/engine'
4-
5-
module SolidusAuthDevise
6-
Engine = ::Spree::Auth::Engine
7-
end

lib/spree/auth/engine.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,5 @@ def self.prepare_frontend
9595
end
9696
end
9797
end
98+
99+
SolidusAuthDevise::Engine = Spree::Auth::Engine

0 commit comments

Comments
 (0)