Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add make_default method on AddPaymentSourcesToWallet class #2913

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
vassalloandrea marked this conversation as resolved.
Show resolved Hide resolved
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