Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Ensure payment_method_id cannot be null on sources #83

Merged
merged 1 commit into from
May 5, 2017
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
1 change: 1 addition & 0 deletions app/models/solidus_paypal_braintree/transaction_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def initialize(order, transaction)
def source
SolidusPaypalBraintree::Source.new nonce: transaction.nonce,
payment_type: transaction.payment_type,
payment_method: transaction.payment_method,
user: user
end

Expand Down
30 changes: 30 additions & 0 deletions db/migrate/20170505193712_add_null_constraint_to_sources.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class AddNullConstraintToSources < ActiveRecord::Migration
def up
payments = Spree::Payment.arel_table
sources = SolidusPaypalBraintree::Source.arel_table
join_sources = payments.join(sources).on(
payments[:source_id].eq(sources[:id]).and(
payments[:source_type].eq("SolidusPaypalBraintree::Source")
).and(
sources[:payment_method_id].eq(nil)
)
).join_sources

count = Spree::Payment.joins(join_sources).count
Rails.logger.info("Updating #{count} problematic sources")

Spree::Payment.joins(join_sources).find_each do |payment|
Rails.logger.info("Updating source #{payment.source_id} with payment method id #{payment.payment_method_id}")
SolidusPaypalBraintree::Source.where(id: payment.source_id).update_all(payment_method_id: payment.payment_method_id)
end

# We use a foreign key constraint on the model,
# but it doesnt make sense to have this model exist without a payment method
# as two of its methods delegate to the payment method.
change_column_null(:solidus_paypal_braintree_sources, :payment_method_id, false)
end

def down
change_column_null(:solidus_paypal_braintree_sources, :payment_method_id, true)
end
end
5 changes: 3 additions & 2 deletions spec/models/solidus_paypal_braintree/gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

RSpec.describe SolidusPaypalBraintree::Gateway do
let(:gateway) do
new_gateway
new_gateway.tap(&:save)
end

let(:braintree) { gateway.braintree }
Expand All @@ -15,7 +15,8 @@
SolidusPaypalBraintree::Source.new(
nonce: 'fake-valid-nonce',
user: user,
payment_type: payment_type
payment_type: payment_type,
payment_method: gateway
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
expect(subject.payment_type).to eq 'ApplePayCard'
end

it 'takes the payment method from the transaction' do
expect(subject.payment_method).to eq braintree_gateway
end

context 'order has a user' do
let(:user) { Spree.user_class.new }
let(:order) { Spree::Order.new user: user }
Expand Down