First, what is Inherited Resources?
Inherited Resources is a gem/plugin that allows you to speed up development by making your controllers inherit all restful actions so you just have to focus on what is important. A Rails scaffold controller which responds to html, xml and json is as simple as:
class ProjectsController
However all actions are still customizable! For example, you can change the behavior of the create action on success just doing:
class ProjectsController
It also supports I18n (all flash messages are set with I18n), belongs to association (like a task belongs to project), nested belongs to (task belongs to project which belongs to company), polymorphic belongs to (comments belong to task or file or message) and optional belongs to (the same as previously, but you can request the resource with or without parents).
As you noticed, besides simplifying your controllers, InheritedResouces makes easy to reproduce your models behavior and relationship in controllers. And right now, it's also scopes and responder fluent!
has_scope: named_scope twin brother
Let's suppose that we have a ProjectsController and at some point you want to add some filters on the index action like showing just featured projects, selecting projects by methodology or even let the user choose how many projects he can see per page. The first thing to do? Add named scopes to your model:
class Project { :featured => true }
named_scope :by_methodology, proc {|methodology| { :conditions => { :methodology => methodology } } }
named_scope :limit, proc{|limit| :limit => limit.to_i }
end
The next step would be to add a lot of code in your controllers that check which named scopes you should call, based on the parameters sent right? Well, not anymore. Your controller can be as simple as:
class ProjectsController true
has_scope :by_methodology
has_scope :limit, :default => 10, :only => :index
end
Then for each request:
/projects
#=> acts like a normal request, but returning only 10 projects
/projects?featured=true
#=> calls the featured named scope and bring 10 featured projects
/projects?featured=true&by_methodology=agile&limit=20
#=> brings 20 featured projects with methodology agile
If you configure your routes, you could even have pretty urls with it:
/projects/agile/featured
#=> brings 10 featured projects with methodology agile
Yay!
Responder
A couple weeks ago, we wrote about ActionController::Responder. But you don't have to wait Rails 3 to be released to play with it, you can start now by using Inherited Resources.
Quick redirect
After using Inherited Resouces, I realized that most of the times I overwrite a create, update or destroy actions is to change the redirect destination. In our ProjectsController above, if we want to redirect to the edit page after create, we would have to do:
class ProjectsController
It wouldn't be cool if it have a shortcut? Now it has:
class ProjectsController
That simple, just give the url as a proc and since the proc does not expect any parameters, it assumes that you want to overwrite the redirect url.
Finally some DSL?
Last weeks, there was a discussion on Boston.rb group about different resource controllers gems. Reading some of the feedback there, I've decided to stab a DSL to Inherited Resources. It will mainly remove the duplication when you have to give a block, the previous example above could be written as:
class ProjectsController
Or even in the compact form:
class ProjectsController
Those changes were not applied yet, it depends mainly on you. If you like or not, let us know in the comments!
[…] It supports nested and/or polymorphic relationships, I18n and other stuff like named scopes. […]
[…] that, Inherited Resources was outdated and envy. It needed to be updated and it was: responders has been available in Inherited Resources for more than four months, and consequently in Rails […]