ActiveResource does not currently support the invocation of custom method calls (which are available in the Simply RESTful routing). This patch lets custom methods be RESTfully invoked from ActiveResource:
Support custom REST methods, in synch with the Simply RESTful
plugin.
I.e. on the server routes config you would have:
map.resources :people, :new => { :register => :post },
:element => { :promote => :put, :deactivate => :delete }
:collection => { :active => :get }
Which creates routes for the following http requests
POST: /people/new;register #=> PeopleController.register
PUT: /people/1;promote #=> PeopleController.promote(:id => 1)
DELETE: /people/1;deactivate #=> PeopleController.deactivate(:id => 1)
GET: /people;active #=> PeopleController.active
This module provides the ability for ActiveResource to call these
custom REST methods:
class Person < ActiveResource::Base
self.site = "http://37s.sunrise.i:3000"
self.new_element_call do |call|
call.post :register
end
self.element_call do |call|
call.put :promote
call.delete :deactivate
end
self.collection_call do |call|
call.get :active
end
end
# Defined methods are now available as class/instance methods
# Element calls are available as instance methods
ryan = Person.new(:name => 'Ryan)
ryan.register #=> true
ryan.id #=> 1
ryan = Person.find(1)
ryan.promote(:position => 'Manager') #=> true
ryan.deactivate #=> true
# Collection calls are available as class methods
Person.active #=> [<Person::xxx>, <Person::xxx>]