Classy Inheritance

Get Version

0.1.1

“You stay classy, inheritance” – Gibson

What

For now, Classy Inheritance adds a depends_on class method to your ActiveRecord model so that you can define requisite objects. More functionality coming for optional relationships.

You can define the relationship to be polymorphic:
class Picture < ActiveRecord::Base
  depends_on :image, :attrs => [:filename, :height, :width], :as => "imageable" 
end

This will look for “imageable_type” and “imageable_id” on the images table add set them accordingly.

Or you can define a standard belongs_to relationship:
class User < ActiveRecord::Base
  depends_on :profile, :attrs => [:first_name, :last_name, :email]
end

What does it do for me?

Well, for starters you get pass-through methods added to your model. So, in the above User example you would get the following:

  @user.first_name
  @user.last_name
  @user.email
  @user.first_name=
  @user.last_name=
  @user.email=

This means you can use these attributes on your form and in your controller instead of having to worry about creating/updating a separate Profile model.

For the above example, you’ll also get a “find_with_profile” class method that will do the :include => :profile addition to your find call for you.

Installing

$ sudo gem install classy-inheritance

# in environment.rb add:
require "classy-inheritance" 

How to submit patches

The Clone URL: git://github.com/stonean/classy-inheritance.git

Read the 8 steps for fixing other people’s code and for section 8b: Submit patch to Google Groups, use the Google Group above.

I’m new to git and this whole opensource project admin gig, so please be patient with my stumbling around.

License

This code is free to use under the terms of the MIT license.

Andrew Stone, 29th May 2008
Theme extended from Paul Battley