Skip to content

Commit

Permalink
Fix assigning nil to default_wallet_payment_source=
Browse files Browse the repository at this point in the history
Previously this was failing due to

    NoMethodError:
      undefined method `transaction' for nil:NilClass
  • Loading branch information
jhawthorn committed May 9, 2017
1 parent 93941eb commit 0451275
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion core/app/models/spree/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def default_wallet_payment_source=(wallet_payment_source)
return
end

wallet_payment_source.transaction do
Spree::WalletPaymentSource.transaction do
# Unset old default
default_wallet_payment_source.try!(:update!, default: false)
# Set new default
Expand Down
47 changes: 30 additions & 17 deletions core/spec/models/spree/wallet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,45 @@
to(wallet_credit_card)
)
end

context "assigning nil" do
it "remains unset" do
expect(subject.default_wallet_payment_source).to be_nil
subject.default_wallet_payment_source = nil
expect(subject.default_wallet_payment_source).to be_nil
end
end
end

context "with other payment source already default" do
context "with a default" do
let!(:wallet_credit_card) { subject.add(credit_card) }
let!(:wallet_store_credit) { subject.add(store_credit) }

before { subject.default_wallet_payment_source = wallet_credit_card }

it "sets the new payment source as the default" do
expect { subject.default_wallet_payment_source = wallet_store_credit }.to(
change(subject, :default_wallet_payment_source).
from(wallet_credit_card).
to(wallet_store_credit)
)
end
end
context "assigning a new default" do
let!(:wallet_store_credit) { subject.add(store_credit) }

context "with the same payment source already set to default" do
let!(:wallet_credit_card) { subject.add(credit_card) }
it "sets the new payment source as the default" do
expect {
subject.default_wallet_payment_source = wallet_store_credit
}.to change{ subject.default_wallet_payment_source }.from(wallet_credit_card).to(wallet_store_credit)
end
end

before { subject.default_wallet_payment_source = wallet_credit_card }
context "assigning same default" do
it "does not change the default payment source" do
expect {
subject.default_wallet_payment_source = wallet_credit_card
}.not_to change{ subject.default_wallet_payment_source }
end
end

it "does not change the default payment source" do
expect { subject.default_wallet_payment_source = wallet_credit_card }.not_to(
change(subject, :default_wallet_payment_source)
)
context "assigning nil" do
it "clears the default payment source" do
expect {
subject.default_wallet_payment_source = nil
}.to change{ subject.default_wallet_payment_source }.to nil
end
end
end

Expand Down

0 comments on commit 0451275

Please sign in to comment.