This module provides a number of REST methods to handle the show/create/update/destroy operations associated with a REST resource.

Methods
Public Instance methods
rest_operations_for(model_symbol, options={})

When the base class calls this method, a series of code templates are applied and new instance methods for show/create/update/destroy operations are added to it.

Base classes include: NotesController and CategoriesController

     # File app/controllers/restful_controller.rb, line 20
 20:     def rest_operations_for(model_symbol, options={})
 21:       # The Class of the model we are RESTing
 22:       model_klass = eval(model_symbol.to_s.capitalize)
 23: 
 24:       # if the model wants to include some child in the xml
 25:       include_str = options.key?(:include) ? "{ :include => :#{options[:include]} }" : ''
 26:       # nested resources need a reference to their parents for redirections: parent_child_url(@object)
 27:       redirect_url = options.key?(:parent) ? "#{options[:parent]}_#{model_symbol}_url" : "#{model_symbol}_url"
 28:   
 29:       if options.key?(:parent)
 30:         class_eval %(
 31:           before_filter :find_parent, :except => [ :index ]
 32:           def find_parent
 33:             if params[:node_id]
 34:               unless @node = Node.find_by_id(params[:node_id])
 35:                 render_optional_error_file :not_found
 36:               end
 37:             else
 38:               render_optional_error_file :not_found
 39:             end
 40:           end        
 41:         )
 42:       end
 43:       
 44:       
 45:       class_eval %(
 46:         before_filter :login_required
 47:         before_filter :find_or_initialize_model, :except => [ :index ]
 48:         def find_or_initialize_model
 49:           if params[:id]
 50:             unless @model = #{model_klass}.find_by_id(params[:id])
 51:               render_optional_error_file :not_found
 52:             end
 53:           else
 54:             @model = #{model_klass}.new(params[:#{model_symbol}])
 55:           end
 56:         end
 57:   
 58:         # GET /nodes
 59:         # Formats: xml
 60:         def index
 61:           @models = #{model_klass}.find(:all)
 62:           respond_to do |format|
 63:             format.html { head :method_not_allowed }
 64: 
 65:             format.xml { render :xml => @models.to_xml(#{include_str})}
 66:           end
 67:         end        
 68: 
 69:         # POST /nodes
 70:         # Formats: xml
 71:         def create
 72:           respond_to do |format|
 73:             format.html { head :method_not_allowed }
 74: 
 75:             if @model.save
 76:               @modified = true
 77:               format.xml { 
 78:                 headers['Location'] = #{redirect_url}(@model)
 79:                 render :xml => @model.to_xml(#{include_str}), :status => :created 
 80:               }
 81:             else
 82:               format.xml { 
 83:                 render :xml => @model.errors.to_xml, 
 84:                 :status => :unprocessable_entity 
 85:               }
 86:             end
 87:           end
 88:         end        
 89: 
 90: 
 91:         # GET /node/<id>
 92:         # Formats: xml
 93:         def show
 94:           respond_to do |format|
 95:             format.html { head :method_not_allowed }
 96:             format.xml { render :xml => @model.to_xml(#{include_str}) }
 97:           end
 98:         end
 99: 
100:         # PUT /node/<id>
101:         # Formats: xml
102:         def update
103:           respond_to do |format|
104:             format.html { head :method_not_allowed }
105: 
106:             if @model.update_attributes(params[:#{model_symbol}])
107:               @modified = true
108:               format.xml { render :xml => @model.to_xml(#{include_str}) }
109:             else
110:               format.xml { render :xml => @model.errors.to_xml, :status => :unprocessable_entity }
111:             end
112:           end
113:         end
114: 
115:         # DELETE /nodes/<id>
116:         # Formats: xml
117:         def destroy
118:           respond_to do |format|
119:             format.html { head :method_not_allowed }
120: 
121:             if @model.destroy
122:               @modified = true
123:               format.xml { head :ok }
124:             else
125:               format.xml { render :xml => @model.errors.to_xml, :status => :unprocessable_entity }
126:             end
127:           end
128:         end
129: 
130:       )
131:     end