Class: PostalCode

Inherits:
ApplicationRecord show all
Defined in:
app/models/postal_code.rb

Overview

== Schema Information

Table name: postal_codes
Database name: primary

id :integer not null, primary key
area_codes :string(200)
code :string(10) not null
dst :boolean
latitude :decimal(15, 10)
longitude :decimal(15, 10)
msa :string(50)
msa_fip :integer
postal_type :string(1)
state_code :string(2)
utc :integer

Indexes

idx_postal_codes_trgm (code) USING gist
index_postal_codes_on_code (code) UNIQUE
index_postal_codes_on_state_code (state_code)

Constant Summary collapse

CANADA_POSTAL_CODE_FIRST_LETTER_TO_PROVINCE_CODE =
{
  "A" => "NL",
  "B" => "NS",
  "C" => "PE",
  "E" => "NB",
  "G" => "QC",
  "H" => "QC",
  "J" => "QC",
  "K" => "ON",
  "L" => "ON",
  "M" => "ON",
  "P" => "ON",
  "R" => "MB",
  "S" => "SK",
  "T" => "AB",
  "V" => "BC",
  "X" => "NT",
  "Y" => "YT"
}

Instance Attribute Summary collapse

Has many collapse

Belongs to collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#codeObject (readonly)



28
# File 'app/models/postal_code.rb', line 28

validates :code, :presence => true, :uniqueness => true

Class Method Details

.get_state_code_from_postal_code(post_code) ⇒ Object



58
59
60
61
62
63
# File 'app/models/postal_code.rb', line 58

def self.get_state_code_from_postal_code(post_code)
  return unless post_code.present?
  res = PostalCode.find_by(code: post_code)&.state&.code
  res ||= PostalCode::CANADA_POSTAL_CODE_FIRST_LETTER_TO_PROVINCE_CODE[post_code[0].upcase]
  return res
end

Instance Method Details

#address_attributesObject



54
55
56
# File 'app/models/postal_code.rb', line 54

def address_attributes
  {:street1 => 'Unknown', :zip => self.code, :state_code => self.state_code, :city => (self.cities.first.city_name rescue nil), :country_iso3 => self.state.country_iso3}
end

#addressesActiveRecord::Relation<Address>

Returns:

  • (ActiveRecord::Relation<Address>)

See Also:



29
# File 'app/models/postal_code.rb', line 29

has_many :addresses, :foreign_key => :zip, :primary_key => :code

#citiesActiveRecord::Relation<City>

Returns:

  • (ActiveRecord::Relation<City>)

See Also:



30
# File 'app/models/postal_code.rb', line 30

has_many :cities, :foreign_key => :code, :primary_key => :code

#postal_code_statisticsActiveRecord::Relation<PostalCodeStatistic>

Returns:

See Also:



31
# File 'app/models/postal_code.rb', line 31

has_many :postal_code_statistics, :foreign_key => :code, :primary_key => :code

#stateState

Returns:

See Also:



32
# File 'app/models/postal_code.rb', line 32

belongs_to :state, :foreign_key => :state_code, :primary_key => :code, optional: true