Class: Pinas::Location
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Pinas::Location
- Extended by:
- Geocoder::Model::ActiveRecord
- Defined in:
- lib/pinas/location.rb
Constant Summary collapse
- LEVEL =
{ 'national' => 0, 'region' => 1, 'province' => 2, 'town' => 3, 'barangay' => 4 }
Instance Method Summary collapse
- #ancestors ⇒ Object
- #barangay ⇒ Object
- #barangays ⇒ Object
- #coordinates ⇒ Object
- #country ⇒ Object
- #down_level ⇒ Object
- #for_geocoding ⇒ Object
- #formatted_display ⇒ Object
- #NCR? ⇒ Boolean
- #province ⇒ Object
- #provinces ⇒ Object
- #region ⇒ Object
- #related_location_id_maps ⇒ Object
- #town ⇒ Object
- #towns ⇒ Object
- #up_level ⇒ Object
Instance Method Details
#ancestors ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/pinas/location.rb', line 105 def ancestors @ancestors ||= try do output = [] level_diff = get_level_diff('national', self.location_type) if level_diff > 0 ancestor = self.parent level_diff.times do break if ancestor.nil? output << ancestor ancestor = ancestor.parent end end output end end |
#barangay ⇒ Object
85 86 87 |
# File 'lib/pinas/location.rb', line 85 def @barangay ||= get_type('barangay') end |
#barangays ⇒ Object
40 41 42 |
# File 'lib/pinas/location.rb', line 40 def descendants.where(location_type: 'barangay') end |
#coordinates ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/pinas/location.rb', line 48 def coordinates if location_type=='barangay' # erratic coordinates on barangays, lets use town { lat: parent.latitude.to_f, lon: parent.longitude.to_f } else { lat: latitude.to_f, lon: longitude.to_f } end end |
#country ⇒ Object
89 90 91 |
# File 'lib/pinas/location.rb', line 89 def country @country ||= 'Philippines' end |
#down_level ⇒ Object
97 98 99 |
# File 'lib/pinas/location.rb', line 97 def down_level LEVEL.rassoc(LEVEL[location_type] + 1).try(:[], 0) end |
#for_geocoding ⇒ Object
44 45 46 |
# File 'lib/pinas/location.rb', line 44 def for_geocoding "#{formatted_display || name}, Philippines" end |
#formatted_display ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/pinas/location.rb', line 60 def formatted_display reload if persisted? case location_type when 'barangay' display = "#{name}, #{parent.formatted_display}" when 'town' display = NCR?? "#{name}, NCR": "#{name}, #{province.name}" when 'province' display = province.name end display.try(:titleize) end |
#NCR? ⇒ Boolean
56 57 58 |
# File 'lib/pinas/location.rb', line 56 def NCR? region.code == '130000000' end |
#province ⇒ Object
81 82 83 |
# File 'lib/pinas/location.rb', line 81 def province @province ||= get_type('province') end |
#provinces ⇒ Object
32 33 34 |
# File 'lib/pinas/location.rb', line 32 def provinces descendants.where(location_type: 'province') end |
#region ⇒ Object
77 78 79 |
# File 'lib/pinas/location.rb', line 77 def region @region ||= get_type('region') end |
#related_location_id_maps ⇒ Object
101 102 103 |
# File 'lib/pinas/location.rb', line 101 def Hash[ *LEVEL.keys.map { |type| [type, get_ids(type)] }.flatten ] end |
#town ⇒ Object
73 74 75 |
# File 'lib/pinas/location.rb', line 73 def town @town ||= get_type('town') end |
#towns ⇒ Object
36 37 38 |
# File 'lib/pinas/location.rb', line 36 def towns descendants.where(location_type: 'town') end |