Skip to content

Commit 34347d4

Browse files
committed
Ensure db:seed runs before spree_sample:load
1 parent 1050ca0 commit 34347d4

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class InstallGenerator < Rails::Generators::AppBase
3737

3838
class_option :migrate, type: :boolean, default: true, banner: 'Run Solidus migrations'
3939
class_option :seed, type: :boolean, default: true, banner: 'Load seed data (migrations must be run)'
40-
class_option :sample, type: :boolean, default: true, banner: 'Load sample data (migrations and seeds must be run)'
40+
class_option :sample, type: :boolean, default: true, banner: 'Load sample data (migrations must be run)'
4141
class_option :active_storage, type: :boolean, default: (
4242
Rails.gem_version >= Gem::Version.new("6.1.0")
4343
), banner: 'Install ActiveStorage as image attachments handler for products and taxons'
@@ -64,7 +64,7 @@ def self.exit_on_failure?
6464
def prepare_options
6565
@run_migrations = options[:migrate]
6666
@load_seed_data = options[:seed] && @run_migrations
67-
@load_sample_data = options[:sample] && @run_migrations && @load_seed_data
67+
@load_sample_data = options[:sample] && @run_migrations
6868
@selected_frontend = detect_frontend_to_install
6969
@selected_authentication = detect_authentication_to_install
7070
@selected_payment_method = detect_payment_method_to_install

core/spec/generators/solidus/install/install_generator_spec.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,6 @@
7272
expect(generator.instance_variable_get(:@load_sample_data)).to eq(false)
7373
end
7474

75-
it 'skips sample data if seeds are disabled' do
76-
generator = described_class.new([], ['--auto-accept', '--seed=false'])
77-
generator.prepare_options
78-
79-
expect(generator.instance_variable_get(:@run_migrations)).to eq(true)
80-
expect(generator.instance_variable_get(:@load_seed_data)).to eq(false)
81-
expect(generator.instance_variable_get(:@load_sample_data)).to eq(false)
82-
end
83-
8475
context 'supports legacy frontend option names' do
8576
it 'transform "solidus_frontend" into "classic"' do
8677
generator = described_class.new([], ['--auto-accept', '--frontend=solidus_frontend'])

sample/db/samples/shipping_methods.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
begin
44
north_america = Spree::Zone.find_by!(name: "North America")
55
rescue ActiveRecord::RecordNotFound
6-
puts "Couldn't find 'North America' zone. Did you run `rake db:seed` first?"
7-
puts "That task will set up the countries, states and zones required for Spree."
6+
puts <<~TEXT
7+
Couldn't find 'North America' zone. Did you run `rake db:seed` first?
8+
9+
That task will set up the countries, states and zones required for your store.
10+
TEXT
811
exit
912
end
1013

sample/db/samples/tax_rates.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# frozen_string_literal: true
22

3-
north_america = Spree::Zone.find_by!(name: "North America")
3+
begin
4+
north_america = Spree::Zone.find_by!(name: "North America")
5+
rescue ActiveRecord::RecordNotFound
6+
puts <<~TEXT
7+
Couldn't find 'North America' zone. Did you run `rake db:seed` first?
8+
9+
That task will set up the countries, states and zones required for your store.
10+
TEXT
11+
exit
12+
end
13+
414
clothing = Spree::TaxCategory.find_by!(name: "Default")
515
tax_rate = Spree::TaxRate.create(
616
name: "North America",

sample/lib/tasks/sample.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'spree/sample'
55

66
namespace :spree_sample do
77
desc 'Loads sample data'
8-
task load: :environment do
8+
task load: ['db:seed', :environment] do
99
if ARGV.include?("db:migrate")
1010
puts <<~TEXT
1111
Please run db:migrate separately from spree_sample:load.
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

3-
require 'spec_helper'
3+
require "spec_helper"
4+
require "rake"
45

56
describe "Load samples" do
67
it "doesn't raise any error" do
@@ -9,4 +10,21 @@
910
SpreeSample::Engine.load_samples
1011
}.to output.to_stdout
1112
end
13+
14+
it "has db:seed as a prerequisite" do
15+
Rails.application.load_tasks
16+
17+
task = Rake::Task["spree_sample:load"]
18+
seed_task = Rake::Task["db:seed"]
19+
expect(task.prerequisite_tasks).to include(seed_task)
20+
end
21+
end
22+
23+
describe "Load seeds multiple times" do
24+
it "doesn't duplicate records" do
25+
3.times { Spree::Core::Engine.load_seed }
26+
27+
expect(Spree::Store.count).to eq(1)
28+
expect(Spree::Zone.count).to eq(2)
29+
end
1230
end

0 commit comments

Comments
 (0)