resource_controller extensions

6 views
Skip to first unread message

Jesper Rønn-Jensen

unread,
Nov 5, 2008, 11:02:12 AM11/5/08
to resource_controller
Hey

Are there any plugins/extensions to resource_controller

I have been googling for the past half hour unable to find anything
that could fit -- but I recall having seen something somewhere. Please
update this discussion with any resources to make it findable in the
future.

Especially I'm looking for something that can make other formats as
part of resource_controller (xml). Like so, but DRYed up so it's not
repeated for each controller:
class ProductsController < ResourceController::Base
# Setup XML responses
index.wants.xml { render :xml => @products }
show.wants.xml { render :xml => @product }
new_action.wants.xml { render :xml => @product }
create.wants.xml { render :xml => @product, :status
=> :created, :location => @product }
create.failure.wants.xml { render :xml => @product.errors, :status
=> :unprocessable_entity }
destroy.wants.xml { head :ok }
update.wants.xml { head :ok }
update.failure.wants.xml { render :xml => @product.errors, :status
=> :unprocessable_entity }
# Response doesn't make sense for edit action
end

from http://blog.billzajac.com/wordpress/articles/2008/02/23/using-xml-with-the-resource_controller-plugin/

Any help will be greatly appreciated,

Jesper Rønn-Jensen
Blog: http://justaddwater.dk/

Joe Fiorini

unread,
Nov 5, 2008, 11:23:34 AM11/5/08
to resource_...@googlegroups.com
Jesper,

AFAIK, there are no extensions to help. However, I think there are a few changes you could make that would make different actions respond differently. I would probably create a few private helper methods to encapsulate the repeated code. For what it's worth, here is my base RESTful resource_controller that is a little more DRY, because it does a few different responses depending on the method, although I know this could be improved and DRYed out a bit:

class ApplicationResourceController < ApplicationController
  resource_controller

  protect_from_forgery :only => [:update, :delete]

  index.wants.xml { render :xml => collection.to_xml, :status => :ok }
  show do
    failure.wants.xml { render :nothing => true, :status => 404 }
    wants.xml do 
      unless object
        render :nothing => true, :status => 404
      else
        render :xml => object.to_xml, :status => :ok 
      end
    end
  end
  
  update do
    failure.wants.xml do
      render :nothing => true, :status => :conflict if object.conflicting_name? params[:id]
    end
    
    wants.xml { render :xml => object.to_xml, :status => :ok }

  end

  create do
    wants.html { }
    wants.xml { render :xml => object.to_xml, :status => :created, :location => object_url(object) }
    failure.wants.xml { render :nothing => true, :status => :conflict if object.conflicting_name? params[:id] }
  end

  destroy do
    wants.xml { render :nothing => true, :status => :ok }
  end

end

Good luck with this!

- Joe
--
joe fiorini
http://www.faithfulgeek.org
// freelancing & knowledge sharing

hyuan

unread,
Nov 5, 2008, 11:38:45 AM11/5/08
to resource_controller
James talks about looping through actions which I think could help
some of the code being discussed here in his first post about r_c:

http://jamesgolick.com/2007/10/19/introducing-resource_controller-focus-on-what-makes-your-controller-special

He says:

Since I commonly use the same RJS template for several actions, I've
found this short-form really handy, because I can use a one-liner to
add the same response to several actions.
class PostsController < ResourceController::Base
[create, update].each { |action| action.wants.js {render :template
=> "post.rjs"} }
end

Han Yuan
> >http://blog.billzajac.com/wordpress/articles/2008/02/23/using-xml-wit...
>
> > Any help will be greatly appreciated,
>
> > Jesper Rønn-Jensen
> > Blog:http://justaddwater.dk/
>
> --
> joe fiorinihttp://www.faithfulgeek.org

Florent Piteau

unread,
Nov 5, 2008, 3:42:47 PM11/5/08
to resource_...@googlegroups.com
Hi,

You can also include some custom module in
ResourceController::Controller and use alias method chain on
init_default_actions
That way all ResourceController will have this default actions.
Here is a sample of what I use in order to respond to json in all my
resource controllers.

module MyResourceController::DefaultActions
def self.included(base)
base.extend ClassMethods
base.class_eval do
class << self
alias_method_chain :init_default_actions, :json
end
end
end

module ClassMethods

def init_default_actions_with_json(klass)
init_default_actions_without_json(klass)
klass.class_eval do

index.wants.json do
render :json
end

edit.wants.json do
render :json
end

new_action.wants.json do
render :json
end

create do
wants.json {
render :json
}

failure.wants.json {
render :json
}
end

update do
wants.json {
render :json
}

failure.wants.json {
render :json
}
end

destroy do
wants.json {
render :json
}
failure.wants.json {
render :json
}
end
end
end

end
end
ResourceController::Controller.send :include,
MyResourceController::DefaultActions

++
Florent


Joe Fiorini a écrit :

Jesper Rønn-Jensen

unread,
Nov 5, 2008, 8:23:41 PM11/5/08
to resource_controller
Thanks for your replies. However I cant get it to work nor Joes or
Florents examples.

Just out of curiosity, where would you put the file with the extended
code and what would you name it?

/jesper

Joe Fiorini

unread,
Nov 5, 2008, 8:27:31 PM11/5/08
to resource_...@googlegroups.com
I called mine application_resource_controller. It extended ResourceController::Base and then all the controllers that needed it just inherited ApplicationResourceController.

- Joe

Florent Piteau

unread,
Nov 6, 2008, 6:31:56 AM11/6/08
to resource_...@googlegroups.com
you will have to put my sample in
RAILS_APP/lib/my_resource_controller/default_actions.rb and do something
like require 'my_resource_controller/default_actions' intour environment.rb.

Good luck
++
Florent


Jesper Rønn-Jensen a écrit :

Jesper Rønn-Jensen

unread,
Nov 7, 2008, 8:17:17 AM11/7/08
to resource_controller
Thanks a lot for your helpful notes.

I extracted and combined your code to get something that worked for
me.

I put is as a plugin and put it up on github

/Jesper

Joe Fiorini

unread,
Nov 7, 2008, 8:19:10 AM11/7/08
to resource_...@googlegroups.com
Can you post a link? I'd like to see it!

Thanks!
Joe

Jesper Rønn-Jensen

unread,
Nov 7, 2008, 9:03:42 AM11/7/08
to resource_controller
Here is the link to the plugin "Resouce Controller Respond_to"

http://github.com/jesperronn/resource_controller_respond_to/tree/master

TODO (top of my head)
* add simple tests
* DRY up so that the code is not repeated for each format
* configure which formats to respond to

Please let me know if you like any commit rights -- i think that
opening up for easy development will just help us all.

/Jesper


PS. content from README below:
ResouceControllerRespondTo
==========================

Extends ResourceController plugin for Rails to make it respond_to .xml
and .json formats

ResourceController URL: http://github.com/giraffesoft/resource_controller/
This plugin URL: http://github.com/jesperronn/resource_controller_respond_to/



Example
=======

Just drop this in your plugins folder.
Also, make shure this plugin is loaded AFTER ResourceController>

You may also want to add the following line to your environment.rb --
in case it's not working
config.plugins =
[ :resource_controller, :resource_controller_respond_to, :all ]




Based on code by Florend Piteau and Joe Fiorini... Jesper merely
provided the glue and packed as plugin.
See http://groups.google.com/group/resource_controller/browse_thread/thread/ec9100db655bc5a9
for the complete discussion

Copyright (c) 2008 Jesper Rønn-Jensen (www.justaddwater.dk), released
under the MIT license

Reply all
Reply to author
Forward
0 new messages