Sha256: 9a2b41deb1419494346a1df504626fa2e4750e9d5a3f6de53b6636f4ce3c1c45

Contents?: true

Size: 993 Bytes

Versions: 8

Compression:

Stored size: 993 Bytes

Contents

module Spree
  class State < Spree::Base
    belongs_to :country, class_name: 'Spree::Country'
    has_many :addresses, dependent: :nullify

    has_many :zone_members,
      -> { where(zoneable_type: 'Spree::State') },
      class_name: 'Spree::ZoneMember',
      foreign_key: :zoneable_id

    has_many :zones, through: :zone_members, class_name: 'Spree::Zone'

    validates :country, :name, presence: true

    def self.find_all_by_name_or_abbr(name_or_abbr)
      where('name = ? OR abbr = ?', name_or_abbr, name_or_abbr)
    end

    # table of { country.id => [ state.id , state.name ] }, arrays sorted by name
    # blank is added elsewhere, if needed
    def self.states_group_by_country_id
      state_info = Hash.new { |h, k| h[k] = [] }
      self.order('name ASC').each { |state|
        state_info[state.country_id.to_s].push [state.id, state.name]
      }
      state_info
    end

    def <=>(other)
      name <=> other.name
    end

    def to_s
      name
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
spree_core-3.0.4 app/models/spree/state.rb
spree_core-3.0.3 app/models/spree/state.rb
spree_core-3.0.2 app/models/spree/state.rb
spree_core-3.0.1 app/models/spree/state.rb
spree_core-3.0.0 app/models/spree/state.rb
spree_core-3.0.0.rc4 app/models/spree/state.rb
spree_core-3.0.0.rc3 app/models/spree/state.rb
spree_core-3.0.0.rc1 app/models/spree/state.rb