diff --git a/app/models/solidus_paypal_braintree/source.rb b/app/models/solidus_paypal_braintree/source.rb index 33e5aac7..0b0a7dcb 100644 --- a/app/models/solidus_paypal_braintree/source.rb +++ b/app/models/solidus_paypal_braintree/source.rb @@ -10,6 +10,8 @@ class Source < ApplicationRecord belongs_to :customer, class_name: "SolidusPaypalBraintree::Customer" + validates :payment_type, presence: true + scope(:with_payment_profile, -> { joins(:customer) }) scope(:credit_card, -> { where(payment_type: CREDIT_CARD) }) diff --git a/db/migrate/20170508085402_add_not_null_constraint_to_sources_payment_type.rb b/db/migrate/20170508085402_add_not_null_constraint_to_sources_payment_type.rb new file mode 100644 index 00000000..624ebd97 --- /dev/null +++ b/db/migrate/20170508085402_add_not_null_constraint_to_sources_payment_type.rb @@ -0,0 +1,11 @@ +class AddNotNullConstraintToSourcesPaymentType < ActiveRecord::Migration + def change + reversible do |dir| + dir.up do + SolidusPaypalBraintree::Source.where(payment_type: nil). + update_all(payment_type: 'CreditCard') + end + end + change_column_null(:solidus_paypal_braintree_sources, :payment_type, false) + end +end diff --git a/spec/models/solidus_paypal_braintree/gateway_spec.rb b/spec/models/solidus_paypal_braintree/gateway_spec.rb index 9c2a88f1..b1a31acd 100644 --- a/spec/models/solidus_paypal_braintree/gateway_spec.rb +++ b/spec/models/solidus_paypal_braintree/gateway_spec.rb @@ -364,10 +364,19 @@ let(:other_payment_method) { FactoryGirl.create(:payment_method) } let(:source_without_profile) do - SolidusPaypalBraintree::Source.create!(payment_method_id: gateway.id, user_id: user.id) + SolidusPaypalBraintree::Source.create!( + payment_method_id: gateway.id, + payment_type: payment_type, + user_id: user.id + ) end + let(:source_with_profile) do - SolidusPaypalBraintree::Source.create!(payment_method_id: gateway.id, user_id: user.id).tap do |source| + SolidusPaypalBraintree::Source.create!( + payment_method_id: gateway.id, + payment_type: payment_type, + user_id: user.id + ).tap do |source| source.create_customer!(user: user) source.save! end @@ -429,11 +438,19 @@ let(:user) { FactoryGirl.create(:user) } let!(:source_without_profile) do - SolidusPaypalBraintree::Source.create!(payment_method_id: gateway.id, user_id: user.id) + SolidusPaypalBraintree::Source.create!( + payment_method_id: gateway.id, + payment_type: payment_type, + user_id: user.id + ) end let!(:source_with_profile) do - SolidusPaypalBraintree::Source.create!(payment_method_id: gateway.id, user_id: user.id).tap do |source| + SolidusPaypalBraintree::Source.create!( + payment_method_id: gateway.id, + payment_type: payment_type, + user_id: user.id + ).tap do |source| source.create_customer!(user: user) source.save! end diff --git a/spec/models/solidus_paypal_braintree/source_spec.rb b/spec/models/solidus_paypal_braintree/source_spec.rb index 4d537962..5b589bd1 100644 --- a/spec/models/solidus_paypal_braintree/source_spec.rb +++ b/spec/models/solidus_paypal_braintree/source_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' RSpec.describe SolidusPaypalBraintree::Source, type: :model do - it 'is invalid without a payment_type set', :pending do + it 'is invalid without a payment_type set' do expect(described_class.new).to be_invalid end