carmen-ruby/carmen

state_codes throws an exception for countries with no states

Closed this issue · 2 comments

I am doing some custom validation on my address object to ensure no asshattery with invalid regions, as follows. Note that I am manually rescuing the state_codes call as it will bomb if a country with no states is passed in.

def valid_state
state_codes = Carmen::state_codes(country_code || Carmen::default_country) rescue []
errors.add(:state_code, :blank) if state_code.blank? && !state_codes.blank?
errors.add(:state_code, :inclusion) unless state_codes.blank? || state_codes.include?(state_code)
end

jim commented

I've aded a new method, Carmen::states?, which will tell you if states are supported for a country code. Also, a Carmen::StatesNotSupported exception is raised when trying to retrieve states for an unsupported country instead of just bombing out.

Fab! Will get latest.