Module: Geocodable
Class Method Summary collapse
Instance Method Summary collapse
- #coordinates ⇒ Object
- #coordinates=(coordinates) ⇒ Object
- #geocode ⇒ Object
- #geocoded? ⇒ Boolean
- #latitude=(value) ⇒ Object
- #longitude=(value) ⇒ Object
- #needs_to_be_geocoded? ⇒ Boolean
- #save_with_geocoding ⇒ Object
- #update_with_geocoding(params) ⇒ Object
Class Method Details
.included(model) ⇒ Object
2 3 4 |
# File 'app/models/geocodable.rb', line 2 def self.included(model) model.scope :geocoded, -> { model.where.not(latitude: nil, longitude: nil) } end |
Instance Method Details
#coordinates ⇒ Object
20 21 22 |
# File 'app/models/geocodable.rb', line 20 def coordinates [latitude, longitude] end |
#coordinates=(coordinates) ⇒ Object
24 25 26 |
# File 'app/models/geocodable.rb', line 24 def coordinates=(coordinates) self.latitude, self.longitude = coordinates end |
#geocode ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'app/models/geocodable.rb', line 28 def geocode return false unless valid? return true unless needs_to_be_geocoded? self.coordinates = ModelGeocoder.geocode(self) add_geocoding_failed_error unless geocoded? geocoded? end |
#geocoded? ⇒ Boolean
16 17 18 |
# File 'app/models/geocodable.rb', line 16 def geocoded? coordinates.compact.present? end |
#latitude=(value) ⇒ Object
6 7 8 9 |
# File 'app/models/geocodable.rb', line 6 def latitude=(value) value = value.to_f.round(6) unless value.nil? write_attribute(:latitude, value) end |
#longitude=(value) ⇒ Object
11 12 13 14 |
# File 'app/models/geocodable.rb', line 11 def longitude=(value) value = value.to_f.round(6) unless value.nil? write_attribute(:longitude, value) end |
#needs_to_be_geocoded? ⇒ Boolean
38 39 40 |
# File 'app/models/geocodable.rb', line 38 def needs_to_be_geocoded? !geocoded? || has_address_changes? end |
#save_with_geocoding ⇒ Object
42 43 44 |
# File 'app/models/geocodable.rb', line 42 def save_with_geocoding geocode && save end |
#update_with_geocoding(params) ⇒ Object
46 47 48 49 |
# File 'app/models/geocodable.rb', line 46 def update_with_geocoding(params) self.attributes = params save_with_geocoding end |