module Camping::Helpers

Public Instance Methods

R(c, *g) click to toggle source

Overrides Camping's routing helper to make it possible to route RESTful resources.

Some usage examples:

R(Kittens)            # /kittens
R(Kittens, 'new')     # /kittens/new
R(Kittens, 1, 'meow') # /kittens/1/meow
R(@kitten)            # /kittens/1
R(@kitten, 'meow')    # /kittens/1/meow
R(Kittens, 'list', :colour => 'black')  # /kittens/list?colour=black

The current output format is retained, so if the current @format is :XML, the URL will be /kittens/1.xml rather than /kittens/1.

Note that your controller names might not be loaded if you're calling R inside a view module. In that case you should use the fully qualified name (i.e. Myapp::Controllers::Kittens) or include the Controllers module into your view module.

# File lib/reststop.rb, line 416
def R(c, *g)
  if Controllers.constants.include?(cl = c.class.name.split("::").last.pluralize)
    path = "/#{cl.underscore}/#{c.id}"
    path << ".#{@format.to_s.downcase}" if @format
    path << "/#{g.shift}" unless g.empty? 
    self / path
  elsif c.respond_to?(:restful?) && c.restful?
    base = c.name.split("::").last.underscore
    id_or_action = g.shift
    if id_or_action =~ /\d+/
      id = id_or_action
      action = g.shift
    else
      action = id_or_action
    end
    
    path = "/#{base}"
    path << "/#{id}" if id
    path << "/#{action}" if action
    path << ".#{@format.to_s.downcase}" if @format 
    path << "?#{g.collect{|a|a.collect{|k,v| U.escape(k)+"="+U.escape(v)}.join("&")}.join("&")}" unless g.empty? # FIXME: undefined behaviour if there are multiple arguments left
    return path
  else
    _R(c, *g)
  end
end
Also aliased as: _R
_R(c, *g)
Alias for: R