Skip to content

Commit

Permalink
Add make_default method on AddPaymentSourcesToWallet class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vassalloandrea committed Feb 7, 2020
1 parent 3ceec32 commit 27fb811
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
11 changes: 8 additions & 3 deletions core/app/models/spree/wallet/add_payment_sources_to_wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 27fb811

Please sign in to comment.