Skip to content

Commit

Permalink
Merge pull request #4955 from tvdeyen/make-OrderUpdateAttributes-conf…
Browse files Browse the repository at this point in the history
…igurable

Allow to set order_update_attributes_class
  • Loading branch information
kennyadsl authored Apr 14, 2023
2 parents 523796e + 8ac56e9 commit 04a4d68
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/app/controllers/spree/api/checkouts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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).call
if can?(:admin, @order) && user_id.present?
@order.associate_user!(Spree.user_class.find(user_id))
end
Expand Down
4 changes: 3 additions & 1 deletion core/app/models/spree/order_update_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +24,8 @@ def apply
order.save
end

alias_method :apply, :call

private

attr_reader :attributes, :payments_attributes, :order
Expand Down
7 changes: 7 additions & 0 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 "call"
# 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
Expand Down
4 changes: 2 additions & 2 deletions core/spec/models/spree/order/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions core/spec/models/spree/order_update_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 04a4d68

Please sign in to comment.