Class: GeoMasterJp::City

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/geo_master_jp/city.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherited(child) ⇒ Object



14
15
16
17
18
19
# File 'app/models/geo_master_jp/city.rb', line 14

def self.inherited(child)
  child.belongs_to :prefecture, class_name: GeoMasterJp.config.alternative_class_name(:prefecture), foreign_key: :geo_master_jp_prefecture_code, primary_key: :code
  child.has_many   :towns,      class_name: GeoMasterJp.config.alternative_class_name(:town),       foreign_key: :geo_master_jp_city_code,       primary_key: :code

  super
end

Instance Method Details

#head_kanaObject



8
9
10
11
12
# File 'app/models/geo_master_jp/city.rb', line 8

def head_kana
  return '' if self.name_kana.blank?
  head_kanas = ['ア', 'カ', 'サ', 'タ', 'ナ', 'ハ', 'マ', 'ヤ', 'ラ', 'ワ']
  head_kanas[head_kanas.index{|i| i > self.name_kana[0] }.to_i - 1]
end

#town_grouped_select_optionsObject



25
26
27
28
29
# File 'app/models/geo_master_jp/city.rb', line 25

def town_grouped_select_options
  self.towns.group_by(&:head_kana).map do |head_kana, towns|
    [head_kana, towns.map{|town| [town.name, town.code] }]
  end.sort_by{|head_kana, _| head_kana }
end

#town_select_optionsObject



21
22
23
# File 'app/models/geo_master_jp/city.rb', line 21

def town_select_options
  self.towns.map{|town| [town.name, town.code] }
end