Module: ActiveResource::Singleton::ClassMethods

Defined in:
lib/active_resource/singleton.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#singleton_nameObject



8
9
10
# File 'lib/active_resource/singleton.rb', line 8

def singleton_name
  @singleton_name ||= model_name.element
end

Instance Method Details

#find(options = {}) ⇒ Object

Core method for finding singleton resources.

Arguments

Takes a single argument of options

Options

  • :params - Sets the query and prefix (nested URL) parameters.

Examples

Weather.find
# => GET /weather.json

Weather.find(:params => {:degrees => 'fahrenheit'})
# => GET /weather.json?degrees=fahrenheit

Failure or missing data

A failure to find the requested object raises a ResourceNotFound
exception.

Inventory.find
# => raises ResourceNotFound


63
64
65
# File 'lib/active_resource/singleton.rb', line 63

def find(options = {})
  find_singleton(options)
end

#singleton_path(prefix_options = {}, query_options = nil) ⇒ Object

Gets the singleton path for the object. If the query_options parameter is omitted, Rails will split from the prefix options.

Options

prefix_options - A hash to add a prefix to the request for nested URLs (e.g., :account_id => 19

would yield a URL like <tt>/accounts/19/purchases.json</tt>).

query_options - A hash to add items to the query string for the request.

Examples

Weather.singleton_path
# => /weather.json

class Inventory < ActiveResource::Base
  self.site =   "https://37s.sunrise.com"
  self.prefix = "/products/:product_id/"
end

Inventory.singleton_path(:product_id => 5)
# => /products/5/inventory.json

Inventory.singleton_path({:product_id => 5}, {:sold => true})
# => /products/5/inventory.json?sold=true


35
36
37
38
39
40
# File 'lib/active_resource/singleton.rb', line 35

def singleton_path(prefix_options = {}, query_options = nil)
  check_prefix_options(prefix_options)

  prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  "#{prefix(prefix_options)}#{singleton_name}.#{format.extension}#{query_string(query_options)}"
end