From d6b52627571838132603ed56383faa4089a6d511 Mon Sep 17 00:00:00 2001 From: Alessandro Lepore Date: Mon, 4 Dec 2017 18:26:10 +0100 Subject: [PATCH] Update TransactionAddress#state_code validation Only validates this field if the country requires states. Use `Address#states_required?` for this check instead of checking objects presence. --- .../transaction_address.rb | 9 +++---- .../transaction_address_spec.rb | 25 +++++++++++-------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app/models/solidus_paypal_braintree/transaction_address.rb b/app/models/solidus_paypal_braintree/transaction_address.rb index 809a60f4..48e47f8d 100644 --- a/app/models/solidus_paypal_braintree/transaction_address.rb +++ b/app/models/solidus_paypal_braintree/transaction_address.rb @@ -10,14 +10,14 @@ class TransactionAddress :city, :zip, :state_code, :address_line_1, :address_line_2 validates :first_name, :last_name, :address_line_1, :city, :zip, - :state_code, :country_code, presence: true + :country_code, presence: true before_validation do self.country_code = country_code.presence || "us" end validates :spree_country, presence: true - validates :spree_state, presence: true, if: :should_match_state_model? + validates :state_code, :spree_state, presence: true, if: :should_match_state_model? def initialize(attributes = {}) country_name = attributes.delete(:country_name) || "" @@ -57,10 +57,9 @@ def to_spree_address address end - # Check to see if this address should match to a state - # model in the database. + # Check to see if this address should match to a state model in the database def should_match_state_model? - spree_country.present? && spree_country.states.any? + spree_country.try!(:states_required?) end end end diff --git a/spec/models/solidus_paypal_braintree/transaction_address_spec.rb b/spec/models/solidus_paypal_braintree/transaction_address_spec.rb index d5c9c424..afaa6424 100644 --- a/spec/models/solidus_paypal_braintree/transaction_address_spec.rb +++ b/spec/models/solidus_paypal_braintree/transaction_address_spec.rb @@ -18,9 +18,8 @@ } end - before do - create :country, iso: 'US' - end + let!(:country) { create :country, iso: 'US', states_required: true } + let!(:state) { create :state, abbr: 'WA', country: country } it { is_expected.to be true } @@ -58,6 +57,12 @@ context "no state_code" do let(:valid_attributes) { super().except(:state_code) } it { is_expected.to be false } + + context "when country does not requires states" do + let!(:country) { create :country, iso: 'US', states_required: false } + + it { is_expected.to be true } + end end context "no country_code" do @@ -155,18 +160,18 @@ describe '#should_match_state_model' do subject { described_class.new(country_code: 'US').should_match_state_model? } - it { is_expected.to be false } + it { is_expected.to be_falsey } - context 'country exists' do - let!(:us) { create :country, iso: 'US' } + context 'country does not require states' do + before { create :country, iso: 'US', states_required: false } it { is_expected.to be false } + end - context 'country has states' do - let!(:state) { create :state, country: us } + context 'country requires states' do + before { create :country, iso: 'US', states_required: true } - it { is_expected.to be true } - end + it { is_expected.to be true } end end