Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Geocoding Changes
- def geocode_address
- geo=Geokit::Geocoders::MultiGeocoder.geocode (address)
- errors.add(:address, "Could not Geocode address") if !geo.success
- self.lat, self.lng = geo.lat,geo.lng if geo.success
- end
- That's an ugly method. 5 things that are wrong with it:
- 1. Use unless geo.success instead of [if !]
- 2. No need for the two if statements. Should be
- self.lat, self.lng = geo.lat, geo.lng if geo.success
- 3. Use self during assignment when referring to attributes
- 4. Indentation is dumb and wrong
- 5. I'm not sure if I missed it or covered it in problems 1-4/
- According to geokit-rails README
- acts_as_mappable :auto_geocode=>{:field=>:address, :error_message=>'Could not geocode address'}
- is equivalent to
- acts_as_mappable
- before_validation_on_create :geocode_address
- So, if we want the location to be updated everytime a talk's location is changed, as is currently expressed in the before_save
- before_save :geocode_location, :if => proc {|talk| talk.location_changed?}
- we can simply do the following instead
- in Talk.rb
- acts_as_mappable
- before_validation :geocode_location, :if => proc {|talk| talk.location_changed? }
- include GeoKit::Geocoders
- then the method becomes:
- private
- def geocode_location
- loc = MultiGeocoder.geocode(self.location)
- self.lat, self.lng = loc.lat, loc.lng if loc.success
- end
- All the tests still pass this way and it cleans up the code quite a bit.
- Because this method (geocode_location) is used in Talk.rb, Speaker.rb, and Event.rb, it might be beneficial to pull this information into a module and include it where needed, to help keep the code DRY
Add Comment
Please, Sign In to add comment