Class: PostalCode
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- PostalCode
- 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 =
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" }.freeze
Constants included from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
- #code ⇒ Object readonly
Has many collapse
- #addresses ⇒ ActiveRecord::Relation<Address>
- #cities ⇒ ActiveRecord::Relation<City>
- #postal_code_statistics ⇒ ActiveRecord::Relation<PostalCodeStatistic>
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 Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Attribute Details
#code ⇒ Object (readonly)
27 |
# File 'app/models/postal_code.rb', line 27 validates :code, presence: true, uniqueness: true |
Class Method Details
.get_state_code_from_postal_code(post_code) ⇒ Object
62 63 64 65 66 67 68 |
# File 'app/models/postal_code.rb', line 62 def self.get_state_code_from_postal_code(post_code) return if post_code.blank? res = PostalCode.find_by(code: post_code)&.state&.code res ||= PostalCode::CANADA_POSTAL_CODE_FIRST_LETTER_TO_PROVINCE_CODE[post_code[0].upcase] res end |
Instance Method Details
#address_attributes ⇒ Object
54 55 56 57 58 59 60 |
# File 'app/models/postal_code.rb', line 54 def address_attributes { street1: 'Unknown', zip: code, state_code: state_code, city: begin cities.first.city_name rescue StandardError nil end, country_iso3: state.country_iso3 } end |
#addresses ⇒ ActiveRecord::Relation<Address>
28 |
# File 'app/models/postal_code.rb', line 28 has_many :addresses, foreign_key: :zip, primary_key: :code |
#cities ⇒ ActiveRecord::Relation<City>
29 |
# File 'app/models/postal_code.rb', line 29 has_many :cities, foreign_key: :code, primary_key: :code |
#postal_code_statistics ⇒ ActiveRecord::Relation<PostalCodeStatistic>
30 |
# File 'app/models/postal_code.rb', line 30 has_many :postal_code_statistics, foreign_key: :code, primary_key: :code |
#state ⇒ State
31 |
# File 'app/models/postal_code.rb', line 31 belongs_to :state, foreign_key: :state_code, primary_key: :code, optional: true |