Module: Tap::Controller::RestRoutes

Defined in:
lib/tap/controller/rest_routes.rb

Overview

Adds REST routing to a Tap::Controller.

class Projects < Tap::Controller
  include RestRoutes

  # GET /projects
  def index...

  # GET /projects/*args
  def show(*args)...

  # POST /projects/*args
  def create(*args)...

  # PUT /projects/*args
  # POST /projects/*args?_method=put
  def update(*args)...

  # DELETE /projects/*args
  # POST /projects/*args?_method=delete
  def destroy(*args)...

  # extension...

  # POST /projects/*args?_method=another
  def another(*args)...
end

Relation to RESTful Rails

Unlike the REST syntax in Rails, ‘/projects/new’ is treated like a show where the id is ‘new’. Also missing is the routing for urls like ‘/projects/arg;edit/’. See these resources:

Instance Method Summary collapse

Instance Method Details

#rest_action(args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tap/controller/rest_routes.rb', line 47

def rest_action(args)
  case request.request_method
  when /GET/i
    if args.empty?
      :index
    else
      :show
    end
  when /POST/i
    case _method = request[:_method]
    when /put/i  
      :update
    when /delete/i  
      :destroy
    when nil
      :create
    else 
      if action?(_method)
        _method
      else
        raise Server::ServerError.new("unknown post method: #{_method}")
      end
    end
  when /PUT/i  then :update
  when /DELETE/i then :destroy
  else raise Server::ServerError.new("unknown request method: #{request.request_method}")
  end
end

#routeObject



41
42
43
44
45
# File 'lib/tap/controller/rest_routes.rb', line 41

def route
  blank, *route = request.path_info.split("/").collect {|arg| unescape(arg) }
  route.unshift rest_action(route)
  route
end