Class: Pinas::Location

Inherits:
ActiveRecord::Base
  • Object
show all
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

Instance Method Details

#ancestorsObject



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

#barangayObject



85
86
87
# File 'lib/pinas/location.rb', line 85

def barangay
  @barangay ||= get_type('barangay')
end

#barangaysObject



40
41
42
# File 'lib/pinas/location.rb', line 40

def barangays
  descendants.where(location_type: 'barangay')
end

#coordinatesObject



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

#countryObject



89
90
91
# File 'lib/pinas/location.rb', line 89

def country
  @country ||= 'Philippines'
end

#down_levelObject



97
98
99
# File 'lib/pinas/location.rb', line 97

def down_level
  LEVEL.rassoc(LEVEL[location_type] + 1).try(:[], 0)
end

#for_geocodingObject



44
45
46
# File 'lib/pinas/location.rb', line 44

def for_geocoding
  "#{formatted_display || name}, Philippines"  
end

#formatted_displayObject



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

Returns:

  • (Boolean)


56
57
58
# File 'lib/pinas/location.rb', line 56

def NCR?
  region.code == '130000000'
end

#provinceObject



81
82
83
# File 'lib/pinas/location.rb', line 81

def province
  @province ||= get_type('province')
end

#provincesObject



32
33
34
# File 'lib/pinas/location.rb', line 32

def provinces
  descendants.where(location_type: 'province')
end

#regionObject



77
78
79
# File 'lib/pinas/location.rb', line 77

def region
  @region ||= get_type('region')
end


101
102
103
# File 'lib/pinas/location.rb', line 101

def related_location_id_maps
  Hash[ *LEVEL.keys.map { |type| [type, get_ids(type)] }.flatten ]
end

#townObject



73
74
75
# File 'lib/pinas/location.rb', line 73

def town
  @town ||= get_type('town')
end

#townsObject



36
37
38
# File 'lib/pinas/location.rb', line 36

def towns
  descendants.where(location_type: 'town')
end

#up_levelObject



93
94
95
# File 'lib/pinas/location.rb', line 93

def up_level
  LEVEL.rassoc(LEVEL[location_type] - 1).try(:[], 0)
end