From 42413c9f991cab7df0f3bc889f4caee1493d1e7a Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Fri, 24 Feb 2023 11:54:08 +0100 Subject: [PATCH 1/2] Allow to set order_update_attributes_class Allows providing your own order update attributes class for checkout. --- api/app/controllers/spree/api/checkouts_controller.rb | 2 +- core/lib/spree/app_configuration.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/api/app/controllers/spree/api/checkouts_controller.rb b/api/app/controllers/spree/api/checkouts_controller.rb index 64b98220d3c..b23b0d64e14 100644 --- a/api/app/controllers/spree/api/checkouts_controller.rb +++ b/api/app/controllers/spree/api/checkouts_controller.rb @@ -42,7 +42,7 @@ def complete def update authorize! :update, @order, order_token - if OrderUpdateAttributes.new(@order, update_params, request_env: request.headers.env).apply + if Spree::Config.order_update_attributes_class.new(@order, update_params, request_env: request.headers.env).apply if can?(:admin, @order) && user_id.present? @order.associate_user!(Spree.user_class.find(user_id)) end diff --git a/core/lib/spree/app_configuration.rb b/core/lib/spree/app_configuration.rb index 05649689168..14b4863608a 100644 --- a/core/lib/spree/app_configuration.rb +++ b/core/lib/spree/app_configuration.rb @@ -426,6 +426,13 @@ def default_pricing_options # signature as Spree::OrderMailer.confirm_email. class_name_attribute :order_mailer_class, default: 'Spree::OrderMailer' + # Allows providing your own order update attributes class for checkout. + # + # @!attribute [rw] order_update_attributes_class + # @return [Class] a class that responds to "apply" + # with the same signature as Spree::OrderUpdateAttributes. + class_name_attribute :order_update_attributes_class, default: 'Spree::OrderUpdateAttributes' + # Allows providing your own Mailer for promotion code batch mailer. # # @!attribute [rw] promotion_code_batch_mailer_class From 8ac56e9cfc3d662c5cf88dd189fa4dd959d75b35 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 12 Apr 2023 11:02:41 +0200 Subject: [PATCH 2/2] Use call as public method for OrderUpdateAttributes class --- api/app/controllers/spree/api/checkouts_controller.rb | 2 +- core/app/models/spree/order_update_attributes.rb | 4 +++- core/lib/spree/app_configuration.rb | 2 +- core/spec/models/spree/order/payment_spec.rb | 4 ++-- core/spec/models/spree/order_update_attributes_spec.rb | 6 +++--- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/api/app/controllers/spree/api/checkouts_controller.rb b/api/app/controllers/spree/api/checkouts_controller.rb index b23b0d64e14..32152d949bc 100644 --- a/api/app/controllers/spree/api/checkouts_controller.rb +++ b/api/app/controllers/spree/api/checkouts_controller.rb @@ -42,7 +42,7 @@ def complete def update authorize! :update, @order, order_token - if Spree::Config.order_update_attributes_class.new(@order, update_params, request_env: request.headers.env).apply + if Spree::Config.order_update_attributes_class.new(@order, update_params, request_env: request.headers.env).call if can?(:admin, @order) && user_id.present? @order.associate_user!(Spree.user_class.find(user_id)) end diff --git a/core/app/models/spree/order_update_attributes.rb b/core/app/models/spree/order_update_attributes.rb index 5957b2b6bee..de334b312dd 100644 --- a/core/app/models/spree/order_update_attributes.rb +++ b/core/app/models/spree/order_update_attributes.rb @@ -15,7 +15,7 @@ def initialize(order, attributes, request_env: nil) # Assign the attributes to the order and save the order # @return true if saved, otherwise false and errors will be set on the order - def apply + def call order.validate_payments_attributes(@payments_attributes) assign_order_attributes @@ -24,6 +24,8 @@ def apply order.save end + alias_method :apply, :call + private attr_reader :attributes, :payments_attributes, :order diff --git a/core/lib/spree/app_configuration.rb b/core/lib/spree/app_configuration.rb index 14b4863608a..c0a6b8a14b4 100644 --- a/core/lib/spree/app_configuration.rb +++ b/core/lib/spree/app_configuration.rb @@ -429,7 +429,7 @@ def default_pricing_options # Allows providing your own order update attributes class for checkout. # # @!attribute [rw] order_update_attributes_class - # @return [Class] a class that responds to "apply" + # @return [Class] a class that responds to "call" # with the same signature as Spree::OrderUpdateAttributes. class_name_attribute :order_update_attributes_class, default: 'Spree::OrderUpdateAttributes' diff --git a/core/spec/models/spree/order/payment_spec.rb b/core/spec/models/spree/order/payment_spec.rb index 02b9c5a9ec4..e3ea791d98c 100644 --- a/core/spec/models/spree/order/payment_spec.rb +++ b/core/spec/models/spree/order/payment_spec.rb @@ -111,13 +111,13 @@ module Spree end it "keeps source attributes on assignment" do - OrderUpdateAttributes.new(order, { payments_attributes: [payment_attributes] }).apply + OrderUpdateAttributes.new(order, { payments_attributes: [payment_attributes] }).call expect(order.unprocessed_payments.last.source.number).to be_present end # For the reason of this test, please see spree/spree_gateway#132 it "keeps source attributes through OrderUpdateAttributes" do - OrderUpdateAttributes.new(order, { payments_attributes: [payment_attributes] }).apply + OrderUpdateAttributes.new(order, { payments_attributes: [payment_attributes] }).call expect(order.unprocessed_payments.last.source.number).to be_present end end diff --git a/core/spec/models/spree/order_update_attributes_spec.rb b/core/spec/models/spree/order_update_attributes_spec.rb index 082411bc836..ed674c7a94a 100644 --- a/core/spec/models/spree/order_update_attributes_spec.rb +++ b/core/spec/models/spree/order_update_attributes_spec.rb @@ -12,14 +12,14 @@ module Spree context 'empty attributes' do let(:attributes){ {} } it 'succeeds' do - expect(update.apply).to be_truthy + expect(update.call).to be_truthy end end context 'with coupon code' do let(:attributes){ { coupon_code: 'abc123' } } it "sets coupon code" do - expect(update.apply).to be_truthy + expect(update.call).to be_truthy expect(order.coupon_code).to eq('abc123') end end @@ -39,7 +39,7 @@ module Spree context 'with params and a request_env' do let(:request_env){ { 'USER_AGENT' => 'Firefox' } } it 'sets the request_env on the payment' do - expect(update.apply).to be_truthy + expect(update.call).to be_truthy expect(order.payments.length).to eq 1 expect(order.payments[0].request_env).to eq({ 'USER_AGENT' => 'Firefox' }) end