From 27fb8115db02a3cae8c562c0758a05be90cbb1d9 Mon Sep 17 00:00:00 2001 From: Andrea Vassallo Date: Fri, 31 Jan 2020 15:48:06 +0100 Subject: [PATCH] Add make_default method on AddPaymentSourcesToWallet class Move the logic to make the last payment source as default outside the add_to_wallet method to take advantage if someone wants to change only the make default behavior. --- .../wallet/add_payment_sources_to_wallet.rb | 11 ++++++++--- .../add_payment_sources_to_wallet_spec.rb | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 core/spec/models/spree/wallet/add_payment_sources_to_wallet_spec.rb diff --git a/core/app/models/spree/wallet/add_payment_sources_to_wallet.rb b/core/app/models/spree/wallet/add_payment_sources_to_wallet.rb index 2dba598e713..0ffc22679f8 100644 --- a/core/app/models/spree/wallet/add_payment_sources_to_wallet.rb +++ b/core/app/models/spree/wallet/add_payment_sources_to_wallet.rb @@ -24,16 +24,21 @@ def add_to_wallet # add valid sources to wallet and optionally set a default if sources.any? # arbitrarily sort by id for picking a default - wallet_payment_sources = sources.sort_by(&:id).map do |source| + sources.sort_by(&:id).each do |source| order.user.wallet.add(source) end - order.user.wallet.default_wallet_payment_source = - wallet_payment_sources.last + make_default end end end + protected + + def make_default + order.user.wallet.default_wallet_payment_source = order.user.wallet_payment_sources.last + end + private attr_reader :order diff --git a/core/spec/models/spree/wallet/add_payment_sources_to_wallet_spec.rb b/core/spec/models/spree/wallet/add_payment_sources_to_wallet_spec.rb new file mode 100644 index 00000000000..d288848c572 --- /dev/null +++ b/core/spec/models/spree/wallet/add_payment_sources_to_wallet_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Spree::Wallet::AddPaymentSourcesToWallet, type: :model do + let(:order) { create(:order_ready_to_complete) } + + describe '#add_to_wallet' do + subject { described_class.new(order) } + + it 'saves the payment source' do + expect { subject.add_to_wallet }.to change { + order.user.wallet.wallet_payment_sources.count + }.by(1) + end + end +end