Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionally validate presence of state_province #335

Merged
merged 1 commit into from
May 14, 2015
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
5 changes: 1 addition & 4 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ class Address < ActiveRecord::Base

validates :address_1,
:city,
:state_province,
:postal_code,
:country,
presence: { message: I18n.t('errors.messages.blank_for_address') }

validates :state_province,
presence: { message: I18n.t('errors.messages.blank_for_address') },
state_province: true
validates :state_province, state_province: true

validates :country, length: { maximum: 2, minimum: 2 }

Expand Down
5 changes: 1 addition & 4 deletions app/models/mail_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ class MailAddress < ActiveRecord::Base

validates :address_1,
:city,
:state_province,
:postal_code,
:country,
:location,
presence: { message: I18n.t('errors.messages.blank_for_mail_address') }

validates :state_province,
presence: { message: I18n.t('errors.messages.blank_for_address') },
state_province: true
validates :state_province, state_province: true

validates :country, length: { maximum: 2, minimum: 2 }

Expand Down
3 changes: 1 addition & 2 deletions app/validators/state_province_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ class StateProvinceValidator < ActiveModel::EachValidator
COUNTRIES_NEEDING_VALIDATION = %w(US CA).freeze

def validate_each(record, attribute, value)
return if value.blank?
return unless COUNTRIES_NEEDING_VALIDATION.include?(record.country)
default_message = I18n.t('errors.messages.invalid_state_province')

unless value.size == 2
unless value.present? && value.size == 2
record.errors[attribute] << (options[:message] || default_message)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/locations/update_address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
update_street_address(address_1: '123', city: 'fair', state_province: '',
postal_code: '12345', country: 'US')
click_button 'Save changes'
expect(page).to have_content "State can't be blank for Address"
expect(page).to have_content t('errors.messages.invalid_state_province')
end

scenario 'with an empty zip' do
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/locations/update_mail_address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
update_mailing_address(address_1: '123', city: 'fair', state_province: '',
postal_code: '12345', country: 'US')
click_button 'Save changes'
expect(page).to have_content "State can't be blank for Mail Address"
expect(page).to have_content t('errors.messages.invalid_state_province')
end

scenario 'with an empty zip' do
Expand Down
12 changes: 7 additions & 5 deletions spec/models/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

it { is_expected.to validate_presence_of(:address_1).with_message("can't be blank for Address") }
it { is_expected.to validate_presence_of(:city).with_message("can't be blank for Address") }
it { is_expected.to validate_presence_of(:state_province).with_message("can't be blank for Address") }
it do
is_expected.to validate_presence_of(:state_province).
with_message(t('errors.messages.invalid_state_province'))
end
it { is_expected.to validate_presence_of(:postal_code).with_message("can't be blank for Address") }
it { is_expected.to validate_presence_of(:country).with_message("can't be blank for Address") }

Expand Down Expand Up @@ -93,12 +96,11 @@
end

context 'when country is not CA or US' do
it 'validates presence' do
address = build(:address, country: 'UK', state_province: '')
it 'does not validate presence' do
address = build(:address, country: 'ES', state_province: '')
address.save

expect(address.errors[:state_province].first).
to eq t('errors.messages.blank_for_address')
expect(address.errors[:state_province]).to be_empty
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions spec/models/mail_address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

it { is_expected.to validate_presence_of(:address_1).with_message("can't be blank for Mail Address") }
it { is_expected.to validate_presence_of(:city).with_message("can't be blank for Mail Address") }
it { is_expected.to validate_presence_of(:state_province).with_message("can't be blank for Mail Address") }
it do
is_expected.to validate_presence_of(:state_province).
with_message(t('errors.messages.invalid_state_province'))
end
it { is_expected.to validate_presence_of(:postal_code).with_message("can't be blank for Mail Address") }
it { is_expected.to validate_presence_of(:country).with_message("can't be blank for Mail Address") }

Expand Down Expand Up @@ -87,12 +90,11 @@
end

context 'when country is not CA or US' do
it 'validates presence' do
it 'does not validate presence' do
mail_address = build(:mail_address, country: 'UK', state_province: '')
mail_address.save

expect(mail_address.errors[:state_province].first).
to eq t('errors.messages.blank_for_mail_address')
expect(mail_address.errors[:state_province]).to be_empty
end
end
end
Expand Down