Module: Geocodable
Class Method Summary collapse
Instance Method Summary collapse
- #coordinates ⇒ Object
- #geocode!(coordinates) ⇒ Object
- #geocoded? ⇒ Boolean
- #latitude=(value) ⇒ Object
- #longitude=(value) ⇒ 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 |
#geocode!(coordinates) ⇒ Object
24 25 26 27 |
# File 'app/models/geocodable.rb', line 24 def geocode!(coordinates) self.latitude, self.longitude = coordinates update_columns(latitude: latitude, longitude: longitude) 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 |