Rails App Template

I start a lot of projects. More than I’d like to admit. I finish and deploy very few, but I like cranking out the first iteration of an app in a weekend. Sometimes I do it just to learn some new feature, or try out an architecture idea. I don’t like to do this in the context of whatever existing app I’m working on, because I don’t want any baggage of whatever I was thinking last week. Last week is too long ago.

I’ve had this technique validated by my former boss Ward Cunningham. He (famously) worked with Kent Beck at Tektronix and they were research partners. During this time, they came up with lots of really great ideas, design patterns, and methodologies. And the technique they used for exploration went something like this (paraphrased from Ward):

Kent and I would get to work in the morning, and we’d just start writing code. If it went well, we might right code all morning and maybe all day. If it went really well, we’d work on the same project all week and get something great out. Usually these ideas then got turned over to other teams to keep working on.

But usually, these ideas didn’t work out very well. So we’d stop at lunch, throw it all away, and work on whatever we were supposed to be working on.

The next morning, we would both show up and Kent would usually say “I have an idea on that thing we were working on yesterday” and off we’d go.

Great things came out of this, TDD, some notable design patterns, and really XP.

The thing that holds me back from doing this more, is that frankly, it’s a pain in the ass to get all the basics in a rails app installed. Just to do anything, I usually like to have Rspec, rspec-rails, cucumber, authlogic, jquery, 960gs, and a handful of other plugins and libraries installed. They all need to be configured, databases need to be set up, etc.

Worst of all, most things need authentication, you know, so that you can actually have users. That means you need sign up, login, activation, password resets, emails, the list goes on.

Where am I going with this? Well, the ability to generate rails apps via custom templates was added in Rails 2.2. So I took it upon myself to stop shaving small yaks every time I wanted to hack on a new project and shave a big one. I present, the science department rails template.

rails my_app -d postgresql -m http://github.com/BJClark/rails-templates/blob/d8889e1c771847f75641503e478c130e959e1686/scidept.rb

Run this little command, answer 3 questions, change 1 line of code, and run

 rake spec && rake features

You now have a fully implemented and tested, rails/rspec/cucumber/authlogic/resource_controller/lesscss/jquery/960gs app, with signup, login, and password resets, checked into git and ready to hack.

Hack, fork, and profit!

  • As Keanu would say, “Whoa.”

    I’ve been working on something similar, though not nearly as thorough. Are you using rails’ template feature or something else?

    My “template.rb” file:
    http://gist.github.com/160441

    ps. looks like we travel in similar circles. My new boss has worked with (or knows) Cunningham & Beck as well!

  • marfarma

    So far I have nothing but grief — and I shouldn’t even be spending time on this. It just sounded so enticing! Well, if you can help:

    sudo rails cccc -m http://github.com/BJClark/rails-templates/raw/d8889e1c771847f75641503e478c130e959e1686/scidept.rb
    create
    create app/controllers
    create app/helpers
    create app/models
    create app/views/layouts
    create config/environments
    create config/initializers
    create config/locales
    create db
    create doc
    create lib
    create lib/tasks
    create log
    create public/images
    create public/javascripts
    create public/stylesheets
    create script/performance
    create test/fixtures
    create test/functional
    create test/integration
    create test/performance
    create test/unit
    create vendor
    create vendor/plugins
    create tmp/sessions
    create tmp/sockets
    create tmp/cache
    create tmp/pids
    create Rakefile
    create README
    create app/controllers/application_controller.rb
    create app/helpers/application_helper.rb
    create config/database.yml
    create config/routes.rb
    create config/locales/en.yml
    create config/initializers/backtrace_silencers.rb
    create config/initializers/inflections.rb
    create config/initializers/mime_types.rb
    create config/initializers/new_rails_defaults.rb
    create config/initializers/session_store.rb
    create config/environment.rb
    create config/boot.rb
    create config/environments/production.rb
    create config/environments/development.rb
    create config/environments/test.rb
    create script/about
    create script/console
    create script/dbconsole
    create script/destroy
    create script/generate
    create script/runner
    create script/server
    create script/plugin
    create script/performance/benchmarker
    create script/performance/profiler
    create test/test_helper.rb
    create test/performance/browsing_test.rb
    create public/404.html
    create public/422.html
    create public/500.html
    create public/index.html
    create public/favicon.ico
    create public/robots.txt
    create public/images/rails.png
    create public/javascripts/prototype.js
    create public/javascripts/effects.js
    create public/javascripts/dragdrop.js
    create public/javascripts/controls.js
    create public/javascripts/application.js
    create doc/README_FOR_APP
    create log/server.log
    create log/production.log
    create log/development.log
    create log/test.log
    applying template: http://github.com/BJClark/rails-templates/raw/d8889e1c771847f75641503e478c130e959e1686/scidept.rb
    executing echo TODO > README from /home/deploy/www/www.trackingplace.com/app/cccc
    gem rspec
    gem rspec-rails
    gem cucumber
    gem webrat
    gem nakajima-fixjour
    gem bmabey-email_spec
    gem binarylogic-authlogic
    gem giraffesoft-resource_controller
    gem less
    rake gems:install
    rake aborted!
    can’t activate , already activated rspec-1.2.8

    edit:// removed the full stack trace

    • BJ Clark

      Could you try changing the template to use rspec 1.2.8? It looks like that’s already installed and messing things up.

  • Marfarma

    Please delete my previous comment.

    It was inappropriate of me to dump the entire run log on you.

    Thanks for posting the template — and I’ll work my way through my errors over time.

  • Marfarma

    Didn’t see your reply.

    I’ll try that.

    Thanks

  • marfarma

    I can’t seem to let this go. I’ve abandoned my attempt to convert this template to sqlite — and gone so far as to install postgresql, so I could try the un-hacked one. However, I must not have it configured the same way you do, as I’m getting this:

    executing createuser -s /var/lib/postgresql/cccd from /var/lib/postgresql/cccd
    rake db:create
    FATAL: role “cccd” does not exist
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/connection_adapters/postgresql_adapter.rb:950:in `initialize’
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/connection_adapters/postgresql_adapter.rb:950:in `connect’

    when I run it.

    Help!

  • Marfarma

    I’m tracking this down — wanted to say that the login tuning rails has superuser priv. From the shell the create user command succeeds.

    Don’t understand why the same thing run from inside a rails session would fail.

    Any ideas welcome.

    • BJ Clark

      Try creating a user for Postgresql that is the same as the name of your project. That’s how I usually do it. I can look at extending this to use a specified user.

  • Marfarma

    Ok – I managed to get it to run clean.

    Needed to replace self.root with @root.split(‘/’).last

    Then it choaked on the scaffold_resource … “-s”,”–skip-migration”

    I had to lose the -s and switch the double dash in front of skip-migration to a single dash. I can’t figure out why that was a problem, but perhaps it’s related to rails 2.33 or rspec 1.2.8

    Now to move on to the final step, where I can run the tests and see if it actually works.

  • marfarma

    Well, the specs pass, as all but two features do.

    These are the two failing features:

    Failing Scenarios:
    cucumber features/authentication.feature:6 # Scenario: Signing Up
    cucumber features/authentication.feature:27 # Scenario: Scenario: Resetting my password

    In each case, the failure is the same:

    “When I open the email # features/step_definitions/email_steps.rb:30
    Could not find email . “

  • marfarma

    OK – further information about the options on the scaffold line — seems to be related to the cucumber / rspec issue described here: http://wiki.github.com/aslakhellesoy/cucumber/troubleshooting The error does seem to run through the rspec code.

    I’ll comment rspec out of the environment, and try that scaffod build again — and see what I get.

  • marfarma

    OK – now we’re cooking with gas!!

    rspec was parsing the options — so had to comment out the rspec and rspec-rails plugins, and UPGRADE the cucumber version, which no longer requires rspec.

    Then I could restore the scaffold_resource code with it’s original options.

    Further, the less gem needed a higher version than specified in the template, as well. I won’t be surprised if several other gems or plugins won’t need a higher version. Of course, once I get it all tested and completely working, I’ll lock the versions down again. Serves me right for running Rails 2.33.

    There ought to be a way to specify the Rails version a given template is compatible with …..

    • BJ Clark

      Yes, there should be! 🙂

      I haven’t upgraded to 2.3.3 yet, so I’m glad to hear you’ve got it working. When you get the right versions going, can you let me know and I’ll see if I can duplicate it on my machine with those new versions?

      Thanks for working this out, and I’m sorry for not replying, I’ve been on vacation. 🙂