Sha256: fc5cad2fc0ea2192693b092eca94977b3ddc6d2453e1c3e5835287c1a9ff3f21

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

module Spree
  Address.class_eval do

    def state_validate
      # Skip state validation without country (also required)
      # or when disabled by preference
      return if country.blank? || !Spree::Config[:address_requires_state] || country.id != 214

      # ensure associated state belongs to country
      if state.present?
        if state.country == country
          self.state_name = nil #not required as we have a valid state and country combo
        else
          if state_name.present?
            self.state = nil
          else
            errors.add(:state, :invalid)
          end
        end
      end

      # ensure state_name belongs to country without states, or that it matches a predefined state name/abbr
      if state_name.present?
        if country.states.present?
          states = country.states.find_all_by_name_or_abbr(state_name)

          if states.size == 1
            self.state = states.first
            self.state_name = nil
          else
            errors.add(:state, :invalid)
          end
        end
      end

      # ensure at least one state field is populated
      errors.add :state, :blank if state.blank? && state_name.blank?
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spree_zoned-0.5.38 app/models/spree/address_decorator.rb
spree_zoned-0.5.37 app/models/spree/address_decorator.rb
spree_zoned-0.5.36 app/models/spree/address_decorator.rb
spree_zoned-0.5.35 app/models/spree/address_decorator.rb
spree_zoned-0.5.34 app/models/spree/address_decorator.rb