Today, I would like to show you an improved way to manage Single Table Inheritance with ActiveScaffold. It s a real time safer.
STI Models should hopefully have a big overlap of attributes, which is the base assumption for my implementation of STI in ActiveScaffold.
Basically, we will show an index view of parent sti controller, in which member actions such as edit, show,delete are linking to corresponding specific sti controller. Create link opens a popup menu showing the different sti models.
That s the concept let s try it out. I will use an example from the book: “Agile Web Development with Rails” written by Dave Thomas and David Heinemeier Hansson.
That example consist of the following models:
- Person (sti_parent)
- Employee (Parent of Manager)
It s quite tedious to set up an application with these models, therefore we will shorten that process.
Setup Example Application
- Download: one_step_active_scaffold
- Download: sti_blog.rb and rename it to model_setup.rb
- Execute following command: ruby one_step_activescaffold.rb sti jquery
Great, we ve prepared our example application, now we have to configure our controllers
ActiveScaffold configuration can be inherited, which is quite useful for sti, because many columns are shared.
class CustomersController < PeopleController
class EmployeesController < PeopleController
class ManagersController < EmployeesController
Definition of column set for each Controller
We have to define for each controller, which columns we would like to use. Please note that we do not have to define anything specific for manager controller because it should use same settings as employee controller.
class PeopleController < ApplicationController active_scaffold :person do |conf| conf.columns = [:type, :name, :email] end end
class CustomersController < PeopleController active_scaffold :customer do |conf| conf.columns = [:type, :name, :email, :balance] conf.update.columns = [:name, :email, :balance] conf.create.columns = [:name, :email, :balance] end end
class EmployeesController < PeopleController active_scaffold :employee do |conf| conf.columns = [:type, :name, :email, :boss, :department] conf.update.columns = [:name, :email, :boss, :department] conf.create.columns = [:name, :email, :boss, :department] end end
Parent Controller STI Child Models
One last step, we have to tell the parent controller, which sti_models he should be able to manage.
class PeopleController < ApplicationController active_scaffold :person do |conf| .... conf.sti_children = [:employee, :customer, :manager] .... end
Our Application is ready, start the server and go to: localhost:3000/people.
Hopefully, you will benefit as much as I did.