Skip to content

Commit

Permalink
Merge pull request #3645 from nebulab/kennyadsl/restore-2208
Browse files Browse the repository at this point in the history
Add ability to run validations in order updater
  • Loading branch information
spaghetticode authored Jun 4, 2020
2 parents 42a096f + 1a9af09 commit 6ad7e95
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 15 deletions.
10 changes: 6 additions & 4 deletions core/app/models/spree/order_merger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ def merge!(other_order, user = nil)
end

set_user(user)
persist_merge
if order.valid?
persist_merge

# So that the destroy doesn't take out line items which may have been re-assigned
other_order.line_items.reload
other_order.destroy
# So that the destroy doesn't take out line items which may have been re-assigned
other_order.line_items.reload
other_order.destroy
end
end

private
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/order_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def update_item_total
end

def persist_totals
order.save!(validate: false)
order.save!(validate: Spree::Config.run_order_validations_on_order_updater)
end

def log_state_change(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ Spree.config do |config|
# See https://github.com/solidusio/solidus/pull/3456 for more info.
config.raise_with_invalid_currency = false

# Set this configuration to `false` to avoid running validations when
# updating an order. Be careful since you can end up having inconsistent
# data in your database turning it on.
# See https://github.com/solidusio/solidus/pull/3645 for more info.
config.run_order_validations_on_order_updater = true

# Permission Sets:

# Uncomment and customize the following line to add custom permission sets
Expand Down
4 changes: 4 additions & 0 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ class AppConfiguration < Preferences::Configuration
# (default: +['admin']+)
preference :roles_for_auto_api_key, :array, default: ['admin']

# @!attribute [rw] run_order_validations_on_order_updater
# @return [Boolean] Whether to run validation when updating an order with the OrderUpdater
preference :run_order_validations_on_order_updater, :boolean, default: false

# @!attribute [rw] send_core_emails
# @return [Boolean] Whether to send transactional emails (default: true)
preference :send_core_emails, :boolean, default: true
Expand Down
9 changes: 9 additions & 0 deletions core/lib/spree/core/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ class Engine < ::Rails::Engine
caller
)
end

if Spree::Config.run_order_validations_on_order_updater != true
Spree::Deprecation.warn(
'Spree::Config.run_order_validations_on_order_updater set to false is ' \
'deprecated and will not be possibile in Solidus 3.0. Please switch this ' \
'value to true and check that everything works as expected.',
caller
)
end
end

# Load in mailer previews for apps to use in development.
Expand Down
1 change: 1 addition & 0 deletions core/lib/spree/testing_support/dummy_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class Application < ::Rails::Application
Spree.config do |config|
config.mails_from = "store@example.com"
config.raise_with_invalid_currency = false
config.run_order_validations_on_order_updater = true
config.use_combined_first_and_last_name_in_address = true

if ENV['ENABLE_ACTIVE_STORAGE']
Expand Down
3 changes: 2 additions & 1 deletion core/spec/models/spree/order/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,8 @@ def assert_state_changed(order, from, to)
context 'when the line items are not available' do
before do
order.line_items << FactoryBot.create(:line_item)
order.store = FactoryBot.build(:store)
order.store = FactoryBot.create(:store)

Spree::OrderUpdater.new(order).update

order.save!
Expand Down
8 changes: 7 additions & 1 deletion core/spec/models/spree/order_contents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@
end

context "completed order" do
let(:order) { Spree::Order.create! state: 'complete', completed_at: Time.current }
let(:order) do
Spree::Order.create!(
state: 'complete',
completed_at: Time.current,
email: "test@example.com"
)
end

before { order.shipments.create! stock_location_id: variant.stock_location_ids.first }

Expand Down
28 changes: 27 additions & 1 deletion core/spec/models/spree/order_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def create_adjustment(label, amount)
end

context "updating payment state" do
let(:order) { Order.new }
let(:order) { build(:order) }
let(:updater) { order.updater }
before { allow(order).to receive(:refund_total).and_return(0) }

Expand Down Expand Up @@ -570,5 +570,31 @@ def create_adjustment(label, amount)
expect(item).to have_received(:do_something)
end
end

context "with invalid associated objects" do
let(:order) { Spree::Order.create(ship_address: Spree::Address.new) }

before do
stub_spree_preferences(run_order_validations_on_order_updater: run_order_validations_on_order_updater)
end

context "when run_order_validations_on_order_updater is true" do
let(:run_order_validations_on_order_updater) { true }
subject { updater.update }

it "raises because of the invalid object" do
expect { subject }.to raise_error(ActiveRecord::RecordInvalid)
end
end

context "when run_order_validations_on_order_updater is false" do
let(:run_order_validations_on_order_updater) { false }
subject { updater.update }

it "does not raise because of the invalid object" do
expect { subject }.not_to raise_error
end
end
end
end
end
2 changes: 1 addition & 1 deletion core/spec/models/spree/promotion_handler/coupon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def expect_adjustment_creation(adjustable:, promotion:, promotion_code: nil)
end

context "applied alongside another valid promotion " do
let!(:order) { Order.create }
let!(:order) { create(:order) }

before do
order.coupon_code = "10off"
Expand Down
12 changes: 6 additions & 6 deletions frontend/app/controllers/spree/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ def populate
# 2,147,483,647 is crazy. See issue https://github.com/spree/spree/issues/2695.
if !quantity.between?(1, 2_147_483_647)
@order.errors.add(:base, t('spree.please_enter_reasonable_quantity'))
end

begin
@line_item = @order.contents.add(variant, quantity)
rescue ActiveRecord::RecordInvalid => error
@order.errors.add(:base, error.record.errors.full_messages.join(", "))
else
begin
@line_item = @order.contents.add(variant, quantity)
rescue ActiveRecord::RecordInvalid => error
@order.errors.add(:base, error.record.errors.full_messages.join(", "))
end
end

respond_with(@order) do |format|
Expand Down

0 comments on commit 6ad7e95

Please sign in to comment.