Favorite pattern for adding functionality to an AR?

Do you have a preferred programming pattern for adding functionality to
an ActiveRecord? As an example, say I have some gnarly trig functions
for distance and bearing between pairs of latitude and longitude:

=== file: latlng.rb
module LatLng

def haversine_distance(lat1, lng1, lat2, lng2)

end

def bearing(lat1, lng1, lat2, lng2)

end
=== EOF

… and I want to mix in haversine_distance(other_ar) and
bearing(other_ar) methods into an ActiveRecord. The shim functions
would look like this:

===
def haversine_distance(other_ar)
haversine_distance(self.lat, self.lng, other_ar.lat, other_ar.lng)
end

def bearing(other_ar)
bearing(self.lat, self.lng, other_ar.lat, other_ar.lng)
end

BUT: What are your preferred methods for packing this up in to modules?
What are the preferred names for modules? Where do you put the files?
Do you include the computational methods in the same file as the shim
functions? Etc…

Extending AR functionality is relatively common, but I haven’t yet seen
a dominant pattern for doing so. I don’t expect really coherent answers
from such an open ended question, but I’m curious what techniques people
have seen and like.

  • ff

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs