Module: ActiveResource::CustomMethods

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/active_resource/custom_methods.rb

Overview

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.json # PeopleController.register
PATCH/PUT /people/1/promote.json    # PeopleController.promote with :id => 1
DELETE    /people/1/deactivate.json # PeopleController.deactivate with :id => 1
GET       /people/active.json       # PeopleController.active

Using this module, Active Resource can use these custom REST methods just like the standard methods.

class Person < ActiveResource::Base
  self.site = "https://37s.sunrise.com"
end

Person.new(:name => 'Ryan').post(:register)  # POST /people/new/register.json
# => { :id => 1, :name => 'Ryan' }

Person.find(1).put(:promote, :position => 'Manager') # PUT /people/1/promote.json
Person.find(1).delete(:deactivate) # DELETE /people/1/deactivate.json

Person.get(:active)  # GET /people/active.json
# => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}]

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#delete(method_name, options = {}) ⇒ Object



115
116
117
# File 'lib/active_resource/custom_methods.rb', line 115

def delete(method_name, options = {})
  connection.delete(custom_method_element_url(method_name, options), self.class.headers)
end

#get(method_name, options = {}) ⇒ Object



94
95
96
# File 'lib/active_resource/custom_methods.rb', line 94

def get(method_name, options = {})
  self.class.format.decode(connection.get(custom_method_element_url(method_name, options), self.class.headers).body)
end

#patch(method_name, options = {}, body = "") ⇒ Object



107
108
109
# File 'lib/active_resource/custom_methods.rb', line 107

def patch(method_name, options = {}, body = "")
  connection.patch(custom_method_element_url(method_name, options), body, self.class.headers)
end

#post(method_name, options = {}, body = nil) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/active_resource/custom_methods.rb', line 98

def post(method_name, options = {}, body = nil)
  request_body = body.blank? ? encode : body
  if new?
    connection.post(custom_method_new_element_url(method_name, options), request_body, self.class.headers)
  else
    connection.post(custom_method_element_url(method_name, options), request_body, self.class.headers)
  end
end

#put(method_name, options = {}, body = "") ⇒ Object



111
112
113
# File 'lib/active_resource/custom_methods.rb', line 111

def put(method_name, options = {}, body = "")
  connection.put(custom_method_element_url(method_name, options), body, self.class.headers)
end