Class: GeoMasterJp::Prefecture
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- GeoMasterJp::Prefecture
- Defined in:
- app/models/geo_master_jp/prefecture.rb
Class Method Summary collapse
Instance Method Summary collapse
- #city_grouped_select_options ⇒ Object
- #city_select_options ⇒ Object
- #head_kana ⇒ Object
- #line_grouped_select_options ⇒ Object
- #line_select_options ⇒ Object
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.(transactions=nil) (transactions || all).map{|prefecture| [prefecture.name, prefecture.code] } end |
Instance Method Details
#city_grouped_select_options ⇒ Object
37 38 39 40 41 |
# File 'app/models/geo_master_jp/prefecture.rb', line 37 def 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_options ⇒ Object
33 34 35 |
# File 'app/models/geo_master_jp/prefecture.rb', line 33 def self.cities.map{|city| [city.name, city.code] } end |
#head_kana ⇒ Object
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_options ⇒ Object
47 48 49 50 51 |
# File 'app/models/geo_master_jp/prefecture.rb', line 47 def 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_options ⇒ Object
43 44 45 |
# File 'app/models/geo_master_jp/prefecture.rb', line 43 def self.lines.map{|line| [line.name, line.code] } end |