Skip to content

Commit 5196741

Browse files
Manually fix standardrb violations WIP
Many of these are defining classes inside a block in tests. Still a bunch left to fix.
1 parent 50ab434 commit 5196741

File tree

15 files changed

+261
-234
lines changed

15 files changed

+261
-234
lines changed

core/app/models/spree/order.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ def add_payment_sources_to_wallet
698698
def add_default_payment_from_wallet
699699
builder = Spree::Config.default_payment_builder_class.new(self)
700700

701-
if payment = builder.build
701+
if (payment = builder.build)
702702
payments << payment
703703

704704
if bill_address.nil?

core/spec/models/spree/order/state_machine_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
let!(:payment) { create(:payment, state: "checkout", order:) }
8989

9090
before :all do
91+
# rubocop:disable Lint/ConstantDefinitionInBlock
9192
class Spree::Order
9293
checkout_flow do
9394
go_to_state :address
@@ -96,10 +97,12 @@ class Spree::Order
9697
# confirm step has been removed. Payment is the last step now
9798
end
9899
end
100+
# rubocop:enable Lint/ConstantDefinitionInBlock
99101
end
100102

101103
after :all do
102104
# restore the standard checkout flow to avoid leaking into other tests
105+
# rubocop:disable Lint/ConstantDefinitionInBlock
103106
class Spree::Order
104107
checkout_flow do
105108
go_to_state :address
@@ -108,6 +111,7 @@ class Spree::Order
108111
go_to_state :confirm
109112
end
110113
end
114+
# rubocop:enable Lint/ConstantDefinitionInBlock
111115
end
112116

113117
before do

core/spec/models/spree/order_spec.rb

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,44 @@
33
require "rails_helper"
44
require "spree/testing_support/shared_examples/state_change_tracking"
55

6+
class AvailabilityTestValidator
7+
def validate(line_item)
8+
if line_item.variant.sku == "UNAVAILABLE"
9+
line_item.errors.add(:quantity, ":(")
10+
false
11+
else
12+
true
13+
end
14+
end
15+
end
16+
17+
class EnsureUnitsTestValidator
18+
def validate(line_item)
19+
if line_item.quantity != 1
20+
line_item.errors.add(:quantity, ":(")
21+
end
22+
end
23+
end
24+
25+
class TruthNumberGenerator
26+
def initialize(options = {})
27+
end
28+
29+
def generate
30+
"42"
31+
end
32+
end
33+
34+
class TestOrderMerger
35+
def initialize(order)
36+
@order = order
37+
end
38+
39+
def merge!(other_order, user = nil)
40+
[@order, other_order, user]
41+
end
42+
end
43+
644
RSpec.describe Spree::Order, type: :model do
745
let(:store) { create(:store) }
846
let(:user) { create(:user, email: "solidus@example.com") }
@@ -410,15 +448,6 @@
410448

411449
describe "order_merger_class customization" do
412450
before do
413-
class TestOrderMerger
414-
def initialize(order)
415-
@order = order
416-
end
417-
418-
def merge!(other_order, user = nil)
419-
[@order, other_order, user]
420-
end
421-
end
422451
Spree::Config.order_merger_class = TestOrderMerger
423452
end
424453

@@ -867,15 +896,6 @@ def call
867896
end
868897

869898
context "with order number generator configured" do
870-
class TruthNumberGenerator
871-
def initialize(options = {})
872-
end
873-
874-
def generate
875-
"42"
876-
end
877-
end
878-
879899
before do
880900
expect(Spree::Config).to receive(:order_number_generator) do
881901
TruthNumberGenerator.new
@@ -1783,6 +1803,8 @@ def generate
17831803
# Class.new and define_method to avoid creating scope gates that would
17841804
# take this local variable out of scope.
17851805
inventory_unit = arbitrary_inventory_unit
1806+
1807+
# rubocop:disable Lint/ConstantDefinitionInBlock
17861808
TestInventoryUnitBuilder = Class.new do
17871809
def initialize(order)
17881810
end
@@ -1791,6 +1813,7 @@ def initialize(order)
17911813
[inventory_unit]
17921814
}
17931815
end
1816+
# rubocop:enable Lint/ConstantDefinitionInBlock
17941817

17951818
test_stock_config = Spree::Core::StockConfiguration.new
17961819
test_stock_config.inventory_unit_builder_class = TestInventoryUnitBuilder.to_s
@@ -1831,23 +1854,15 @@ def initialize(order)
18311854
subject { order.send(:ensure_inventory_units) }
18321855

18331856
before do
1834-
class TestValidator
1835-
def validate(line_item)
1836-
if line_item.quantity != 1
1837-
line_item.errors.add(:quantity, ":(")
1838-
end
1839-
end
1840-
end
1841-
18421857
test_stock_config = Spree::Core::StockConfiguration.new
1843-
test_stock_config.inventory_validator_class = TestValidator.to_s
1858+
test_stock_config.inventory_validator_class = EnsureUnitsTestValidator.to_s
18441859
stub_spree_preferences(stock: test_stock_config)
18451860
end
18461861

18471862
let(:order) { create :order_with_line_items, line_items_count: 2 }
18481863

18491864
it "uses the configured validator" do
1850-
expect_any_instance_of(TestValidator).to receive(:validate).twice.and_call_original
1865+
expect_any_instance_of(EnsureUnitsTestValidator).to receive(:validate).twice.and_call_original
18511866

18521867
subject
18531868
end
@@ -1873,19 +1888,8 @@ def validate(line_item)
18731888
subject { order.send(:validate_line_item_availability) }
18741889

18751890
before do
1876-
class TestValidator
1877-
def validate(line_item)
1878-
if line_item.variant.sku == "UNAVAILABLE"
1879-
line_item.errors.add(:quantity, ":(")
1880-
false
1881-
else
1882-
true
1883-
end
1884-
end
1885-
end
1886-
18871891
test_stock_config = Spree::Core::StockConfiguration.new
1888-
test_stock_config.availability_validator_class = TestValidator.to_s
1892+
test_stock_config.availability_validator_class = AvailabilityTestValidator.to_s
18891893
stub_spree_preferences(stock: test_stock_config)
18901894
end
18911895

0 commit comments

Comments
 (0)