« Perforce Client Setup | Main | Lies, Damned Lies and Statistics »

Abstract ActiveRecord Classes by Convention

Listen to this articleListen to this article

Ruby on Rails provides a very simple mechanism for specifying that a model class is an abstract base class and therefore has no corresponding database table:

class MyAbstractClass < ActiveRecord::Base
  self.abstract_class = true
  ...
end

Code can then interrogate a model class to see if it is abstract:

puts "it's abstract" if MyAbstractClass.abstract_class?

Not so hard, however I pretty much always prefix the name of my abstract classes with, you guessed it, 'Abstract'. So, I added some code to the RedHill on Rails Core Plugin the other day to extend the definition of an abstract class to include the name:

def abstract_class?
  @@abstract_class || !(name =~ /^Abstract/).nil?
end

With that simple change, I no longer need to explicitly set self.abstract_class = true; it just works by magicconvention.

I suppose I could/should have created a plugin for it but I was feeling lazy :)

Comments

@@abstract_class || !(name =~ /^Abstract/).nil?

Why the double negation?

@@abstract_class or name =~ /^Abstract/

Score 1 for the nitpickers! Incidentally I do this kind of thing all the time, testing on the name or some other incidental but invariant property instead of setting up a flag in an "official" way.

Purely for compatibility reasons I want abstract_class? to return a boolean; =~ returns an integer or nil if there's no match.

Score 1 for the ninja coders :)

Because someone, somewhere, is testing for equality with true instead of testing for truth. The horror!

1. Thanks for the heads up on the abstract_class? overview
2. Your extensions conflict with the classes in my project: Abstract Paintings by Abstract Painters. Damn conventions! :)

Nic

I thought of making it anything ending in Base but that conflicted with my project: NavalBase; and AirforceBase ;-)

In all seriousness though, you've made me think that:

1. Anything which is fixed is not a convention it's a rule--conventions must be changeable; and
2. I'll break it out into a plugin which will be configurable :)

Simon

What about my AbstractBase model?

A little late to the show on this but what a great feature. I've needed this feature before but never came across this. Thanks that's great. Not sure about the Abstract name convention/rule though. Could bite you later.

Never knew you could do this. Might also be useful for refactoring models that have gotten too large. Although that might be considered non-pure as you'd want to aggregate these types of classes rather than inherit.

Thanks again!

Phil

Post a comment