| Module | ActionController::FlexRestScaffolding::ClassMethods |
| In: |
lib/rest_scaffolding.rb
|
REST Scaffolding provides Flex REST actions for creating, reading, updating, and destroying records.
Also returns the schema for the record
Example:
class ContactsController < ActionController::Base flex_scaffold :contacts end
Adds Flex REST actions to the controller. The options are the same as for ActionView::Scaffolding.scaffold, except that the :suffix option is not currently supported.
These are merely the same as a resource albeit a couple of mods
# File lib/rest_scaffolding.rb, line 27
27: def flex_scaffold(model_id, options = {})
28: options.assert_valid_keys(:class_name, :suffix, :size)
29:
30: swf_size = options[:size] || FlexScaffold::Config.default_size
31: singular_name = model_id.to_s
32: class_name = options[:class_name] || singular_name.camelize
33: plural_name = singular_name.pluralize
34:
35: class_eval "# GET /contacts\n# GET /contacts.xml\ndef index\nflex_index\nend\n\ndef flex_index\n@\#{singular_name} = \#{class_name}.find(:all)\n@swf_name = \"_\#{plural_name}\"\n@page_title = \"\#{plural_name.camelize}\"\n@size = \"\#{swf_size}\"\n\nrespond_to do |format|\nformat.html { render :template => \"\#{FlexScaffold::Config.plugin_name}/index\" } # index.rhtml\nformat.xml { render :xml => @\#{singular_name}.to_xml }\nend\nend\n\n# GET /contacts/1\n# GET /contacts/1.xml\ndef show\n@\#{singular_name} = \#{class_name}.find(params[:id])\n\nrespond_to do |format|\nformat.html # show.rhtml\nformat.xml { render :xml => @\#{singular_name}.to_xml }\nend\nend\n\n# GET /contacts/new\ndef new\n@\#{singular_name} = \#{class_name}.new\nend\n\n# GET /contacts/1;edit\ndef edit\n@\#{singular_name} = \#{class_name}.find(params[:id])\nend\n\n# POST /contacts\n# POST /contacts.xml\ndef create\n@\#{singular_name} = \#{class_name}.new(params[:\#{singular_name}])\n\nrespond_to do |format|\nif @\#{singular_name}.save\nflash[:notice] = '\#{class_name} was successfully created.'\nformat.html { redirect_to contact_url(@\#{singular_name}) }\nformat.xml { head :created } # :location => contact_url(@\#{singular_name}) # removed from original\nelse\nformat.html { render :action => \"new\" }\nformat.xml { render :xml => @\#{singular_name}.errors.to_xml }\nend\nend\nend\n\n# PUT /contacts/1\n# PUT /contacts/1.xml\ndef update\n@\#{singular_name} = \#{class_name}.find(params[:id])\n\nrespond_to do |format|\nif @\#{singular_name}.update_attributes(params[:\#{singular_name}])\nflash[:notice] = '\#{class_name} was successfully updated.'\nformat.html { redirect_to contact_url(@\#{singular_name}) }\nformat.xml { head :ok }\nelse\nformat.html { render :action => \"edit\" }\nformat.xml { render :xml => @\#{singular_name}.errors.to_xml }\nend\nend\nend\n\n# DELETE /contacts/1\n# DELETE /contacts/1.xml\ndef destroy\n@\#{singular_name} = \#{class_name}.find(params[:id])\n@\#{singular_name}.destroy\n\nrespond_to do |format|\nformat.html { redirect_to contacts_url }\nformat.xml { head :ok }\nend\nend\n\n# GET /contacts/schema/1\n# GET /contacts/schema/1.xml\n# TODO: changes routes so that this does not require an ID\ndef schema\nresponse.headers[\"Content-Type\"] = \"text/xml\"\nrender :text => schema_xml(\#{class_name})\nend\n\n", __FILE__, __LINE__
36: end