Sha256: 641ceba12c2134f7687dcd66f0a9e510a52ff830a488871b95a896c11ebfd5f6

Contents?: true

Size: 785 Bytes

Versions: 10

Compression:

Stored size: 785 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

10 entries across 10 versions & 2 rubygems

Version Path
gaku-0.1.1 core/app/models/gaku/state.rb
gaku_core-0.1.1 app/models/gaku/state.rb
gaku-0.1.0 core/app/models/gaku/state.rb
gaku_core-0.1.0 app/models/gaku/state.rb
gaku_core-0.0.3 app/models/gaku/state.rb
gaku-0.0.3 core/app/models/gaku/state.rb
gaku-0.0.2 core/app/models/gaku/state.rb
gaku_core-0.0.2 app/models/gaku/state.rb
gaku-0.0.1 core/app/models/gaku/state.rb
gaku_core-0.0.1 app/models/gaku/state.rb