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

Commit

Permalink
Update TransactionAddress#state_code validation
Browse files Browse the repository at this point in the history
Only validates this field if the country requires states.
Use `Address#states_required?` for this check instead of checking objects
presence.
  • Loading branch information
alepore committed Dec 4, 2017
1 parent e204e40 commit 4eb1528
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
9 changes: 4 additions & 5 deletions app/models/solidus_paypal_braintree/transaction_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) || ""
Expand Down Expand Up @@ -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?)

This comment has been minimized.

Copy link
@tvdeyen

tvdeyen Dec 4, 2017

Member

try! is recommended over try

This comment has been minimized.

Copy link
@alepore

alepore Dec 5, 2017

Author Contributor

👍

end
end
end
25 changes: 15 additions & 10 deletions spec/models/solidus_paypal_braintree/transaction_address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 4eb1528

Please sign in to comment.