3

I am using Single-Table-Inheritance for a rails project. And I was wondering if it were possible to share functionality for some common functions between subclasses by implementing them in the superclass.

Is it possible to recycle the views of the superclass as well? Or should I write new views for the common functions? This wouldn't be very DRY. What were the DRY approach? Should I edit the routes.rb or is there another way to dynamically accomplish this?

Best, E.

Engin Kurutepe
  • 6,719
  • 3
  • 34
  • 64

1 Answers1

0

First, you can definitely share functionality between subclasses by implementing at the superclass level - that's one of the big draws for STI.

As for the view question: I assume we are talking about subclassing a model, not a controller. In that case, a single controller (with normal views) for the main model will usually work fine for all of the subclasses. There are some slightly tricky issues when it comes to the forms, and creation/editing though. In particular, you will need to pull out the subclass of the model from the parameters and add it after updating everything else. For example:

def create
  citation_class = params[:citation].try(:delete, :type)
  @citation = citations.new(params[:citation])
  @citation.type = citation_class
  flash[:notice] = 'Citation was successfully created.' if @citation.save

  respond_with @citation
end
Jonathon Jones
  • 711
  • 6
  • 8