-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
state_validator.rb
71 lines (57 loc) 路 1.79 KB
/
state_validator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true
module Spree
class Address::StateValidator
attr_reader :address
delegate :state, :state_name, :country, to: :address
def initialize(address)
@address = address
end
def perform
return unless state_required?
if country.present?
normalize_state if state.present?
normalize_state_name if state_name.present?
end
validate_not_blank
validate_matches_country
end
private
def normalize_state
# discard the 'state' attribute when having a country with no states
address.state = nil if country.states.blank?
end
def normalize_state_name
# discard the 'state_name' when having a valid 'state' and country combo
if state.present? && state.country == country
address.state_name = nil
return
end
# set the state from the state name if the country contains one with that name
states_from_name = country.states.with_name_or_abbr(state_name)
if states_from_name.size == 1
address.state = states_from_name.first
address.state_name = nil
end
end
def validate_not_blank
if state.blank? && state_name.blank?
address.errors.add(:state, :blank)
end
end
def validate_matches_country
if state.present? && state.country != country
address.errors.add(:state, :does_not_match_country)
end
end
# Don't require a state if disabled at config level or
# the associated country doesn't require states
def state_required?
Spree::Config.address_requires_state && country_requires_states?
end
def country_requires_states?
# default to `true` if country not present
return true if country.blank?
country.states_required
end
end
end