Stop using the rails command

— December 28, 2006 at 20:22 PST


Here's a tip that may make your life a little simpler when you need to start a new Rails project. Don't use the rails shell command.

We have a few people building Rails apps at work, and I wanted to make it dead simple for people to start a new Rails app and have it include all our standard customizations. Instead of hacking the generator for the rails command, I decided to use Subversion to help me out.

I ran the rails command one last time to create a pristine app. I checked it in to svn as the exemplar project and went to town customizing it. I set up the usual svn stuff like ignoring logs and database.yml, added datbase.yml.example, set up externals for standard plugins, etc. Now starting a new app is just

svn copy /rails/exemplar /rails/new_app
svn checkout /rails/new_app

Notice that I don't have to do the svn import for new projects. And the svn:ignores etc. are already there.

There are of course a few things you might want to tweak for new projects, like the session cookie name or anything else that uses the name of the app. And when you upgrade to a new version of Rails you'll need to run the upgrade scripts and such on each app. But with svn working for you, you can diff revisions to see what you need to update, which is also helpful.

16 commentsrails, subversion, svn, tools

Comments
  1. monki2006-12-28 21:08:16

    Lovely.

    I don't know how many times I have put off and dreaded the rails command.

    Thank you for the continuing insights.

  2. BillSaysThis2006-12-31 02:44:32

    This also seems like a way to have all your default stuff, plugins, gems, scripts, configurations and the like, put together once rather than on each new project. Cool.

  3. Chris Anderson2006-12-31 21:37:22

    Josh, Thanks for the helpful tip. I thought about doing this some time ago, but came to a standstill because Subversion doesn't support cross-respository copying. For bigger teams or larger projects, it can get ugly to share repositories, but there's no way to preserve the svn:externals and other properties when importing into a new repository - at least that I can think of. That's why I wrote svn_tools - which handles bootstrapping a new app generated from "rails" pretty cleanly.

    Maybe someone has an idea about how to preserve properties across repositories - that could give us some pretty killer application templates... generic blogs, ready to be customized, etc.

    Chris

  4. Robert2007-01-02 03:08:25

    Nice Josh. I use some similiar, a bash script, that will create an edge rails project, add the type of database to the database.yml file, and import it into your svn repo. http://robertrevans.com/article/christmas-nugget

  5. Deirdre Saoirse Moen2007-01-02 22:50:59

    Doh! Why didn't I think of that?

    I use a shell script too, but it never occurred to me to have svn do the lifting.

  6. Keith2007-01-03 02:09:52

    Cool.

    I've been doing something similar by using an svn export command but then I had to write a bunch of rake tasks to do all the svn:ignores and so on.

    I should have thought of the svn copy. Great idea! thanks.

  7. Graham2007-01-03 15:14:54

    Josh - great post, I've been thinking about this. @billsaysthis, et al: What I've actually been looking for is a good reference exemplar, that has restful_authentication, authorization, and a few admin pages already set up. Any ideas?

  8. Bruce Williams2007-01-04 03:39:44

    Josh, great idea! It's so simple and makes so much sense that, like others here, I can't believe it hasn't popped up before.

    Neat!

  9. Robert2007-01-04 05:41:59

    Graham: That would be quite easy to do with the script I've written. Just add in the ./script/generate ... stuff into the script itself. If you need to add more things to your migration files, you can just open them up within the script and add what you want. Should be easy enough.

  10. jeroen2007-01-09 07:55:41

    Funny, I just finished a webinterface that, behind the scenes, does exactly the same. People can also select a JAVA or .NET project and that will instead copy the corresponding project template into a new project.

    You mention you set up svn externals for commonly used plugins. That sounds like a cool idea, but won't the plugins automatically get updated on an "svn up"? That could be dangerous if you for example, the plugins is updated to only support rails 2.0 and you project uses 1.x

  11. Jos2007-01-10 00:19:28

    I just came across your blog and I am very impressed. I am new to rails and you have some good stuff here. As far as this post, I am new to subverion. I have been looking for a way to keep creating rails projects dry and I believe the way you have setup is it. I am a little confused as I tried to get this working. Here are the paths to my svn repository and path to rails projects.

    /home/svn_repos/exemplar$ /home/rails_projects/exemplar$

    I tried running this from the following directoy but I don't think this really matters but anyway:

    /home/svnrepos$ svn copy exemplar/ newapp/

    I tried from:

    /home/railsprojects$ svn copy examplar/ newapp/

    Neither work. I tried from both the svnrepos directory and railsproject directory but again I dont think this should matter:

    $ svn copy file:///home/svnrepos/exemplar file:///home/svnrepos/new_app

    Because I am new, I think I am overlooking something. Any suggestions?

    I look forward to reading up on your new posts. Thanks a bunch!

  12. Josh Susser2007-01-10 07:00:44

    jeroen: You can do svn:externals and link it to a particular revision if you are worried about plugins not being backward compatible. There are actually better solutions for managing plugins like Piston or svk, and at my work we're moving toward using packages for that. But this has been a great way for us to get started and still very useful.

  13. Josh Susser2007-01-10 07:03:18

    Jos: You should read up on Subversion and how to use the copy command. The last one looks to me like it should work - you definitely want to be doing the svn copy using repository URLs, not local pathnames.

  14. Sandro Paganotti2007-01-10 09:31:15

    I love this kind of posts! Easy and very helpful! Thank you Josh.

  15. amcvega2007-01-11 07:55:29

    Hmmm...I can't seem to get this to work.

    I tried to use svn copy between respositories but I get an error. According to the manual (http://svnbook.red-bean.com/en/1.1/re07.html) "Note: You can only copy files within a single repository. Subversion does not support cross-repository copying."

    I don't think it's possible to copy one repository into another.

  16. Filipe Rocha2007-01-16 06:55:30

    Well Josh, that's KISS and I like it. Thanks for the tip.

Sorry, comments for this article are closed.