Sha256: 40ecc56b778f11f29acee6ba139c5d16b8e218e015ad49b6844891342f5a39f5

Contents?: true

Size: 782 Bytes

Versions: 6

Compression:

Stored size: 782 Bytes

Contents

module Gaku
  class State < ActiveRecord::Base
    has_many :addresses

    belongs_to :country, foreign_key: :country_iso, primary_key: :iso

    validates :name, :country_iso, 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_iso
      state_info = Hash.new { |h, k| h[k] = [] }
      State.order('name ASC').each do |state|
        state_info[state.country_iso].push [state.id, state.name]
      end
      state_info
    end

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

    def to_s
      name
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
gaku-0.2.2 core/app/models/gaku/state.rb
gaku_core-0.2.2 app/models/gaku/state.rb
gaku-0.2.1 core/app/models/gaku/state.rb
gaku_core-0.2.1 app/models/gaku/state.rb
gaku-0.2.0 core/app/models/gaku/state.rb
gaku_core-0.2.0 app/models/gaku/state.rb