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
class PostsController < ResourceController::Base
private
def model_name
'blog_post'
end
def route_name
'article'
end
end
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
class PostsController < ResourceController::Base actions :all, :except => :edit end
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...
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
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
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.
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?
create do
before { @post.user = current_user }
responds_to :html, :js do |format|
format.gif { create_gif(@post) }
end
end
Irais
May 1, 2011, May 01, 2011 18:42, permalink
TYVM you've solved all my poblrmes
TYVM you've solved all my poblrmes
Backstory on my blog: http://jamesgolick.com/2007/11/5/resource_controller-redesign-my-api-at-refactormycode-com