PrototypeClassGenerator ======================= A Ruby on Rails generator for Prototype JavaScript classes and tests, Plugin Edition. == Description This tool creates a Prototype-style JavaScript class, a unit test - using the JS Test::Unit class from script.aculo.us - and all of the necessary testing prerequisite files, so its super easy to start building tests for your JavaScript classes, just like you do for your models, views, and controllers. == Credit Inspired by Ryan Bates' Railscast episode: "How to Make a Generator" (http://railscasts.com/episodes/58). Example ======= 1. Install the plugin. > ./script/plugin install http://sbecker.net/shared/plugins/prototype_class_generator 2. Create your first Prototype Class: > ./script/generate prototype_class FormValidator You should see some output like this: create public/javascripts/form_validator.js create public/javascripts/test/lib create public/javascripts/test/unit create public/javascripts/test/lib/test.css create public/javascripts/test/lib/unittest.js create public/javascripts/test/run_unit_tests.html create public/javascripts/test/unit/form_validator_test.html create public/javascripts/test/unit/index.html create entry for FormValidator class in JS unit test suite 3. Congratulations, you've got a valid Prototype class called FormValidator. Not only that, you've got a full JavaScript test harness to run within the browser to test your new class. Lets run it now. Open this file in your browser: "public/javascripts/test/run_unit_tests.html" If you've already got the rails app running on your machine, you should be able to get to it here: http://localhost:3000/javascripts/test/run_unit_tests.html This is a nice pre-built testing harness for all of your JS unit tests. 4. Create additional classes and tests. You've already got a FormValidator class, but now you also need an Accordion class. Great, lets generate the shell: > ./script/generate prototype_class Accordion This time you'll see some output like this: create public/javascripts/accordion.js exists public/javascripts/test/lib exists public/javascripts/test/unit identical public/javascripts/test/lib/test.css identical public/javascripts/test/lib/unittest.js identical public/javascripts/test/run_unit_tests.html create public/javascripts/test/unit/accordion_test.html skip public/javascripts/test/unit/index.html create entry for Accordion class in JS unit test suite Some of the prerequisite files we need already exist, so we didn't overwrite them. What we did do is: - create the Accordion class - create the Accordion unit test - add a link to the Accordion unit test to the existing unit test index page. Now if you view the unit test page in your browser, you'll notice its got a new link to run the Accordion unit test. And the FormValidator unit test is still there. Sweet! 5. Eliminate Doubt "But Scott! If I use this, I'll start to have a million javascript class files, my site will load slowly, and ySlow will give me an F!" No it won't, because you're already using my other plugin AssetPackager, which packs up all of your javascripts into one neatly minified package, aren't you? http://synthesis.sbecker.net/pages/asset_packager So go nuts, logically separate your code, test in isolation, and float up to developer heaven. Copyright (c) 2007 Scott Becker - http://synthesis.sbecker.net, released under the MIT license