Before / After / Response
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
class PostsController < ResourceController::Base # block syntax create do before { @post.user = current_user } response do |wants| wants.html wants.js end end # chain syntax update.failure.wants.js # hybrid syntax update.failure do flash "There was a problem saving your object." wants.js end end
Alternate Model/Route/Object Naming
1 2 3 4 5 6 7 8 9 10 11 12
class PostsController < ResourceController::Base private def model_name 'blog_post' end def route_name 'article' end end
Alternate find syntax
1 2 3 4 5 6 7 8 9 10 11 12
class PostsController < ResourceController::Base private def object @object ||= end_of_association_chain.find_by_permalink(param) end def collection @collection ||= end_of_association_chain.find(:all, :page => {:current => params[:page], :size => 10}) end end
Specific Actions
1 2 3 4 5
class PostsController < ResourceController::Base actions :all, :except => :edit end
Possible Parents
1 2 3 4
class PostsController < ResourceController::Base belongs_to :user, :category end
Refactorings
No refactoring yet !
jamesgolick
November 5, 2007, November 05, 2007 20:28, permalink
First refactoring from my blog...
Alternate Model/Route/Object Naming
1 2 3 4 5
class PostsController < ResourceController::Base model_name :blog_post route_name :article end
macournoyer
November 5, 2007, November 05, 2007 22:24, permalink
A couple of suggestions. I'm used to the default respond_to rather then wants. And I don't know if the *_finder macros are even possible, but would look like a true dsl
Before / After / Response
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class PostsController < ApplicationController acts_as_resource_controller # block syntax create do before { @post.user = current_user } respond_to do |format| format.html format.js end end # chain syntax update.failure.respond_to %w(js html) end
Alternate find syntax
1 2 3 4 5
class PostsController < ResourceController::Base object_finder :find_by_permalink collection_finder { find :all, :page => {:current => params[:page], :size => 10} } end
jamesgolick
November 5, 2007, November 05, 2007 22:42, permalink
I love those. The *_finder macros are definitely possible. I really like the acts_as_resource_controller too. Currently, that syntax is just resource_controller, which I'm not crazy about.
I agree with you about the respond_to, although, I think for open syntax it should be responds_to, just because it reads like english - update responds to js and html. OTOH, I don't see much of a problem with having all of these names as aliases, and so I think I'll probably do that.
1
update.failure.responds_to %(js html)
danielharan
November 6, 2007, November 06, 2007 01:02, permalink
I prefer responds_to. Since we're on the topic, how about making the block optional?
1 2 3 4 5 6
create do before { @post.user = current_user } responds_to :html, :js do |format| format.gif { create_gif(@post) } end end
Backstory on my blog: http://jamesgolick.com/2007/11/5/resource_controller-redesign-my-api-at-refactormycode-com