Abstract ActiveRecord Classes by Convention
Listen 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.
Posted by: evan | October 11, 2006 02:06 PM
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 :)
Posted by: Simon Harris
|
October 11, 2006 02:13 PM
Because someone, somewhere, is testing for equality with true instead of testing for truth. The horror!
Posted by: evan | October 11, 2006 05:13 PM
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
Posted by: Dr Nic | October 11, 2006 07:10 PM
I thought of making it anything ending in Base but that conflicted with my project: NavalBase; and AirforceBase ;-)
Posted by: Simon Harris
|
October 11, 2006 07:16 PM
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
Posted by: Simon Harris
|
October 11, 2006 07:21 PM
What about my AbstractBase model?
Posted by: Chris | October 12, 2006 06:09 AM
:P
Posted by: Simon Harris
|
October 12, 2006 08:11 AM
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
Posted by: Phil Thompson | September 6, 2007 08:46 AM