| Module | ActiveResource::CustomMethods |
| In: |
vendor/rails/activeresource/lib/active_resource/custom_methods.rb
|
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 },
:member => { :promote => :put, :deactivate => :delete }
:collection => { :active => :get }
This route set creates routes for the following HTTP requests:
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
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'}]
| delete | -> | orig_delete |
# File vendor/rails/activeresource/lib/active_resource/custom_methods.rb, line 68
68: def delete(custom_method_name, options = {})
69: # Need to jump through some hoops to retain the original class 'delete' method
70: if custom_method_name.is_a?(Symbol)
71: connection.delete(custom_method_collection_url(custom_method_name, options), headers)
72: else
73: orig_delete(custom_method_name, options)
74: end
75: end
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 into Active Resource instances - they are ordinary Hashes. If you are expecting Active Resource instances, use the find class method with the :from option. For example:
Person.find(:all, :from => :active)
# File vendor/rails/activeresource/lib/active_resource/custom_methods.rb, line 56
56: def get(custom_method_name, options = {})
57: connection.get(custom_method_collection_url(custom_method_name, options), headers)
58: end
# File vendor/rails/activeresource/lib/active_resource/custom_methods.rb, line 34
34: def self.included(base)
35: base.class_eval do
36: extend ActiveResource::CustomMethods::ClassMethods
37: include ActiveResource::CustomMethods::InstanceMethods
38:
39: class << self
40: alias :orig_delete :delete
41:
42: # Invokes a GET to a given custom REST method. For example:
43: #
44: # Person.get(:active) # GET /people/active.xml
45: # # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}]
46: #
47: # Person.get(:active, :awesome => true) # GET /people/active.xml?awesome=true
48: # # => [{:id => 1, :name => 'Ryan'}]
49: #
50: # Note: the objects returned from this method are not automatically converted
51: # into Active Resource instances - they are ordinary Hashes. If you are expecting
52: # Active Resource instances, use the <tt>find</tt> class method with the
53: # <tt>:from</tt> option. For example:
54: #
55: # Person.find(:all, :from => :active)
56: def get(custom_method_name, options = {})
57: connection.get(custom_method_collection_url(custom_method_name, options), headers)
58: end
59:
60: def post(custom_method_name, options = {}, body = '')
61: connection.post(custom_method_collection_url(custom_method_name, options), body, headers)
62: end
63:
64: def put(custom_method_name, options = {}, body = '')
65: connection.put(custom_method_collection_url(custom_method_name, options), body, headers)
66: end
67:
68: def delete(custom_method_name, options = {})
69: # Need to jump through some hoops to retain the original class 'delete' method
70: if custom_method_name.is_a?(Symbol)
71: connection.delete(custom_method_collection_url(custom_method_name, options), headers)
72: else
73: orig_delete(custom_method_name, options)
74: end
75: end
76: end
77: end
78: end
# File vendor/rails/activeresource/lib/active_resource/custom_methods.rb, line 60
60: def post(custom_method_name, options = {}, body = '')
61: connection.post(custom_method_collection_url(custom_method_name, options), body, headers)
62: end