2007-04-25 21:53:01 -04:00
|
|
|
module ActiveResource
|
2007-12-21 16:42:27 -05:00
|
|
|
# A module to support custom REST methods and sub-resources, allowing you to break out
|
|
|
|
# of the "default" REST methods with your own custom resource requests. For example,
|
|
|
|
# say you use Rails to expose a REST service and configure your routes with:
|
|
|
|
#
|
|
|
|
# map.resources :people, :new => { :register => :post },
|
2008-01-23 12:04:49 -05:00
|
|
|
# :member => { :promote => :put, :deactivate => :delete }
|
2007-12-21 16:42:27 -05:00
|
|
|
# :collection => { :active => :get }
|
|
|
|
#
|
2008-05-22 19:03:00 -04:00
|
|
|
# This route set creates routes for the following HTTP requests:
|
2007-12-21 16:42:27 -05:00
|
|
|
#
|
2008-05-22 19:03:00 -04:00
|
|
|
# POST /people/new/register.xml # PeopleController.register
|
|
|
|
# PUT /people/1/promote.xml # PeopleController.promote with :id => 1
|
|
|
|
# DELETE /people/1/deactivate.xml # PeopleController.deactivate with :id => 1
|
|
|
|
# GET /people/active.xml # PeopleController.active
|
2007-12-21 16:42:27 -05:00
|
|
|
#
|
|
|
|
# Using this module, Active Resource can use these custom REST methods just like the
|
|
|
|
# standard methods.
|
|
|
|
#
|
|
|
|
# class Person < ActiveResource::Base
|
|
|
|
# self.site = "http://37s.sunrise.i:3000"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Person.new(:name => 'Ryan).post(:register) # POST /people/new/register.xml
|
|
|
|
# # => { :id => 1, :name => 'Ryan' }
|
|
|
|
#
|
|
|
|
# Person.find(1).put(:promote, :position => 'Manager') # PUT /people/1/promote.xml
|
|
|
|
# Person.find(1).delete(:deactivate) # DELETE /people/1/deactivate.xml
|
|
|
|
#
|
|
|
|
# Person.get(:active) # GET /people/active.xml
|
|
|
|
# # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}]
|
|
|
|
#
|
2007-04-25 21:53:01 -04:00
|
|
|
module CustomMethods
|
2007-12-21 16:42:27 -05:00
|
|
|
def self.included(base)
|
|
|
|
base.class_eval do
|
2007-10-02 01:32:14 -04:00
|
|
|
extend ActiveResource::CustomMethods::ClassMethods
|
|
|
|
include ActiveResource::CustomMethods::InstanceMethods
|
|
|
|
|
2007-04-25 21:53:01 -04:00
|
|
|
class << self
|
|
|
|
alias :orig_delete :delete
|
2007-10-02 01:32:14 -04:00
|
|
|
|
2007-12-21 16:42:27 -05:00
|
|
|
# Invokes a GET to a given custom REST method. For example:
|
|
|
|
#
|
|
|
|
# Person.get(:active) # GET /people/active.xml
|
|
|
|
# # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}]
|
|
|
|
#
|
|
|
|
# Person.get(:active, :awesome => true) # GET /people/active.xml?awesome=true
|
|
|
|
# # => [{:id => 1, :name => 'Ryan'}]
|
|
|
|
#
|
|
|
|
# Note: the objects returned from this method are not automatically converted
|
2008-05-27 17:58:45 -04:00
|
|
|
# into ActiveResource::Base instances - they are ordinary Hashes. If you are expecting
|
|
|
|
# ActiveResource::Base instances, use the <tt>find</tt> class method with the
|
2007-12-21 16:42:27 -05:00
|
|
|
# <tt>:from</tt> option. For example:
|
|
|
|
#
|
|
|
|
# Person.find(:all, :from => :active)
|
|
|
|
def get(custom_method_name, options = {})
|
|
|
|
connection.get(custom_method_collection_url(custom_method_name, options), headers)
|
2007-04-25 21:53:01 -04:00
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
|
2007-12-21 16:42:27 -05:00
|
|
|
def post(custom_method_name, options = {}, body = '')
|
|
|
|
connection.post(custom_method_collection_url(custom_method_name, options), body, headers)
|
2007-04-25 21:53:01 -04:00
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
|
2007-12-21 16:42:27 -05:00
|
|
|
def put(custom_method_name, options = {}, body = '')
|
|
|
|
connection.put(custom_method_collection_url(custom_method_name, options), body, headers)
|
2007-04-25 21:53:01 -04:00
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
|
2007-04-25 21:53:01 -04:00
|
|
|
def delete(custom_method_name, options = {})
|
2007-12-21 16:42:27 -05:00
|
|
|
# Need to jump through some hoops to retain the original class 'delete' method
|
|
|
|
if custom_method_name.is_a?(Symbol)
|
2007-04-29 00:46:14 -04:00
|
|
|
connection.delete(custom_method_collection_url(custom_method_name, options), headers)
|
2007-04-25 21:53:01 -04:00
|
|
|
else
|
|
|
|
orig_delete(custom_method_name, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
|
2007-04-25 21:53:01 -04:00
|
|
|
module ClassMethods
|
|
|
|
def custom_method_collection_url(method_name, options = {})
|
|
|
|
prefix_options, query_options = split_options(options)
|
2007-12-28 12:03:58 -05:00
|
|
|
"#{prefix(prefix_options)}#{collection_name}/#{method_name}.#{format.extension}#{query_string(query_options)}"
|
2007-04-25 21:53:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module InstanceMethods
|
|
|
|
def get(method_name, options = {})
|
2007-04-29 00:46:14 -04:00
|
|
|
connection.get(custom_method_element_url(method_name, options), self.class.headers)
|
2007-04-25 21:53:01 -04:00
|
|
|
end
|
|
|
|
|
2007-06-19 14:40:28 -04:00
|
|
|
def post(method_name, options = {}, body = '')
|
2007-04-25 21:53:01 -04:00
|
|
|
if new?
|
2007-04-29 00:46:14 -04:00
|
|
|
connection.post(custom_method_new_element_url(method_name, options), (body.nil? ? to_xml : body), self.class.headers)
|
2007-04-25 21:53:01 -04:00
|
|
|
else
|
2007-04-29 00:46:14 -04:00
|
|
|
connection.post(custom_method_element_url(method_name, options), body, self.class.headers)
|
2007-04-25 21:53:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-06-19 14:40:28 -04:00
|
|
|
def put(method_name, options = {}, body = '')
|
2007-04-29 00:46:14 -04:00
|
|
|
connection.put(custom_method_element_url(method_name, options), body, self.class.headers)
|
2007-04-25 21:53:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete(method_name, options = {})
|
2007-04-29 00:46:14 -04:00
|
|
|
connection.delete(custom_method_element_url(method_name, options), self.class.headers)
|
2007-04-25 21:53:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
def custom_method_element_url(method_name, options = {})
|
2007-12-28 12:03:58 -05:00
|
|
|
"#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/#{method_name}.#{self.class.format.extension}#{self.class.send!(:query_string, options)}"
|
2007-04-25 21:53:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def custom_method_new_element_url(method_name, options = {})
|
2007-12-28 12:03:58 -05:00
|
|
|
"#{self.class.prefix(prefix_options)}#{self.class.collection_name}/new/#{method_name}.#{self.class.format.extension}#{self.class.send!(:query_string, options)}"
|
2007-04-25 21:53:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
end
|