Plugins - CRUD Generator 2
Add to favoritesCG2 Update : 09/27/2006
CRUD Generator 2 is likely dead now that the new code for scaffold_resources has made its way into edge rails. See here for more info.
http://crudgenerator2.blogspot.com/2006/09/crud-generator-2-is-dead-long-live.html
CRUD Generator 2
==========
CRUD Generator 2 generates a CRUD and simply_restful compliant model, controller, and fully functional views for Ruby on Rails applications. The views are simple and meant to be that way so you can style or modify them to your hearts content later.
A full set of unit and functional tests are also generated which run all CRUD actions on the model and controller using a shared CRUD test that is mixed in and can be used by all of your tests if you choose.
This plugin is based on the original ‘crud_generator’ plugin written by Dan Peterson ( http://svn.dpiddy.net/plugins/crud_generator/ ). At the time I saw his plugin I was trying to see how DHH’s CRUD thinking and simply_restful really worked when the code hit the editor and few examples of documentation existed. Dan’s plugin was a great point of departure for me to learn more about plugins and REST/CRUD and I saw an opportunity to enhance it and learn some more about the subject myself.
Features/Enhancements:
Up to date with the functionality currently in ‘Edge Rails’ with the simply_restful plugin moved into core Rails.
Generates fully functional CRUD/simply_restful compliant migration, model, controller, and view for each entity you want to work with.
Uses ‘responds_to’ method in the controller for all relevant controller actions to present .html, .js, .xml
Controller creates appropriate flash[] messages for each action which you will see if your layout supports display of flash messages.
Designed to be used as a base upon which you can build your code. Not a simple scaffold that needs to be ripped out and replaced later. All generated code is usable out of the box right away and ready to be modified to meet your needs.
Generates a functional tests for each controller.
Generates a unit test for each controller that uses a shared set of methods for testing all CRUD functionality.
The CRUD unit tests are a mixin from the plugin. You can always comment them out, or copy and modify them into your own unit tests if needed.
Try It Before You Buy It!
If you’re the skeptical kind and want to try it out before installing check out the sample link below. The things controller is the default home page of this URL. You can feel free to CRUD whatever you want as the DB will be automatically refreshed each hour. Please don’t abuse it. :-)
Requirements:
Requires ‘Edge Rails’ since we depend on the simply_restful functionality as it is embedded in the current edge.
You must remove the ‘simply_restful’ plugin if you had it installed before in your vendor/plugins directory before upgrading to ‘Edge Rails’.
Requires installation of ‘crud_generator2’ plugin (see below).
A working database that you can run migrations against.
Installation (All steps run from your Rails application root directory):
Step 1:
(Windows) ruby script/plugin install -x http://crudgenerator2.googlecode.com/svn/trunk/crud_generator2
(Linux) script/plugin install -x http://crudgenerator2.googlecode.com/svn/trunk/crud_generator2
Note: The ’-x’ option is only useful if you are using subversion to manage your files in your application. This will install the plugin as an SVN external which will allow you to update it from the plugin’s source code repository each time you do an SVN update later on. If you’re not using Subversion or you don’t want it to be updated later automatically then just remove the ’-x’ flag.
Step 2:
(Windows) ruby script/generate crud Thing
(Linux) script/generate crud Thing
This step generates:
A Thing model (and associated migration)
A Things controller
A set of views using REST & CRUD principles
A set of tests
Here are examples of the files and folders that will be created if we ran this for the rails ‘test’ app on a windows machine for a hypothetical ‘Thing’ entity:
C:\temp\test> ruby script/generate crud Thing exists app/controllers/ exists app/helpers/ create app/views/things exists test/functional/ exists app/models/ exists test/unit/ create app/controllers/things_controller.rb create test/functional/things_controller_test.rb create app/helpers/things_helper.rb create app/models/thing.rb exists db/migrate create db/migrate/001_create_things.rb create test/unit/thing_test.rb create test/fixtures/things.yml create app/views/things/index.rhtml create app/views/things/new.rhtml create app/views/things/show.rhtml create app/views/things/edit.rhtml create app/views/things/_form.rhtml create app/views/things/_thing.rhtml C:\temp\test>
Step 3: Populate your DB with your new model’s table
rake migrate
Step 4: Add the new restful routes for your controller
Add to config/routes.rb
map.resources :things
Step 5 : Test it all with the unit and functional tests
rake test
Step 6 : Restart your web server depending on which you are using:
e.g.
mongrel_rails stop mongrel_rails start
Step 7 : Try it in your browser
Example CRUD/Restful URL’s with controller action, HTTP verb, and URL.
index (GET) : http://localhost:3000/things new (GET) : http://localhost:3000/things/new create (POST) : http://localhost:3000/things show (GET) : http://localhost:3000/things/1 edit (GET) : http://localhost:3000/things/1;edit update (PUT) : http://localhost:3000/things/1 destroy (DELETE) : http://localhost:3000/things/1
Step 8+ : The rest is up to you!
Enjoy!
Glenn Rempe glenn dot rempe at gmail dot com
http://crudgenerator2.blogspot.com/
http://crudgenerator2.googlecode.com/svn/trunk/crud_generator2
Rails' (MIT)
Controllers
