Sha256: 9d8e641f79b4b277e3d887989187258d174dec96a2ede10f1c8018ee8d259746

Contents?: true

Size: 835 Bytes

Versions: 2

Compression:

Stored size: 835 Bytes

Contents

module Spree
  class State < ActiveRecord::Base
    belongs_to :country

    has_one :zone_member, :as => :zoneable
    has_one :zone, :through => :zone_member

    validates :country, :name, :presence => true

    attr_accessible :name, :abbr

    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] = [] }
      Spree::State.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

2 entries across 2 versions & 1 rubygems

Version Path
spree_core-1.1.1 app/models/spree/state.rb
spree_core-1.1.0 app/models/spree/state.rb