Class: GeoMasterJp::Prefecture

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherited(child) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/geo_master_jp/prefecture.rb', line 20

def self.inherited(child)
  child.has_many :cities, class_name: GeoMasterJp.config.alternative_class_name(:city), 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_prefecture_code, primary_key: :code

  if GeoMasterJp.config.use_models.include?(:railway)
    child.has_many :stations, class_name: GeoMasterJp.config.alternative_class_name(:station), foreign_key: :geo_master_jp_prefecture_code, primary_key: :code
    child.has_many :prefectures_lines, class_name: GeoMasterJp.config.alternative_class_name(:prefectures_line), foreign_key: :geo_master_jp_prefecture_code, primary_key: :code
    child.has_many :lines, through: :prefectures_lines, class_name: GeoMasterJp.config.alternative_class_name(:line)
  end

  super
end

.select_options(transactions = nil) ⇒ Object



53
54
55
# File 'app/models/geo_master_jp/prefecture.rb', line 53

def self.select_options(transactions=nil)
  (transactions || all).map{|prefecture| [prefecture.name, prefecture.code] }
end

Instance Method Details

#city_grouped_select_optionsObject



37
38
39
40
41
# File 'app/models/geo_master_jp/prefecture.rb', line 37

def city_grouped_select_options
  self.cities.group_by(&:head_kana).map do |head_kana, cities|
    [head_kana, cities.map{|city| [city.name, city.code] }]
  end.sort_by{|head_kana, _| head_kana }
end

#city_select_optionsObject



33
34
35
# File 'app/models/geo_master_jp/prefecture.rb', line 33

def city_select_options
  self.cities.map{|city| [city.name, city.code] }
end

#head_kanaObject



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

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

#line_grouped_select_optionsObject



47
48
49
50
51
# File 'app/models/geo_master_jp/prefecture.rb', line 47

def line_grouped_select_options
  self.lines.group_by(&:head_kana).map do |head_kana, lines|
    [head_kana, lines.map{|line| [line.name, line.code] }]
  end.sort_by{|head_kana, _| head_kana }
end

#line_select_optionsObject



43
44
45
# File 'app/models/geo_master_jp/prefecture.rb', line 43

def line_select_options
  self.lines.map{|line| [line.name, line.code] }
end