For one of my customers I was developing API, wrapped around some older service. This API was meant to be used by new application developed (using RoR of course) by customer itself. Since I was only wrapping old code I could finish quite soon, before main application was ready.
The problem was that API was not defined in every detail upfront. Since I did plain wrapping I did keep old business logic and flow with this API, and new application was not thinking the same way. This was seen before so we have agreed that integration will be done by me and we needed simple way to hook into new application with my integration code. One of obvious ways to achieve it is to use Rails plugin to keep code separated.
Things went OK but I want to share one thing I have learned. Using Rails plugin to wrap code is good idea with one issue – code reloading. Plugin code is not reloaded in development mode (like it is done with controllers or models) so changes in plugin will be visible after application restart. Not very comfortable for development…
Being lazy and didn’t want to restart application with every change I make, I have started search for some workaround:
I was just about to write that GeoKit is useful plugin but somewhat dormant (previous release was in Jan 08 if I recollect correctly), but I’ve checked project homepage and voila! It has new release! GeoKit was dived into gem (framework independent) and Rails plugin. That way You can use geokit-gem to make some distance calculations even if You didn’t like trigonometry at school :)
How often You had wished time wouldn't move so slow? Image (c) Orange42
GeoKit does provide some finders for ActiveRecord like
find_closest (hard to guess what it does?). Remember that GeoKit expects latitude and longitude fields in AR object to be not nil. If You have some objects with latitude or longitude equal to nil they will be always closest to any given point. Probably not what do You want.
It may be dependent on what are You trying to do, but for me setting default latitude and longitude to values I won’t spot in the wild in my dataset was good enough. But this was special case – I have set default to North Pole, and I was looking not only closest but also close – no further than single miles and I was sure this would not trigger with North Pole.
Why not set defaults to something like -200? You didn’t like trigonometry, do You? Functions used to calculate distance on the sphere are periodic, so You have chance You will hit false positive – latitude and longitude -200/-200 will me mapped into -90..90 and -180..180 ranges and could match anyway.
So when doing any searches and You can not assure Your data have always latitude and longitude You have to remember to add conditions
lat is not null && lng is not null to every query (like
Place.find_closest(:origin => [some_lat, some_lng], :conditions => 'lat is not null && lng is not null').
Anyway – GeoKit is nice gem/plugin and if You need to operate on latitude and longitude – use it, do not write own code. On the other hand, You could learn a bit from trigonometry :))