Keeping Your Ruby Application Version in a Warm, DRY Place

by Mike Zazaian at 2009-08-08 20:23:32 UTC in rails

a short guide on how to keep the version of your Rails app accessible for use throughout its views, while keeping your code flexible and DRY

no comments no links ["

I've gotten into the habit of keeping version numbers for every single app that I develop, be it a burgeoning desktop application or a simple script that I need to rename or move some files. Fact is, you really never know what's going to become of an application when you begin writing it. Even that tiny script may prove enormously useful in the Open Source community and suddenly thousands of people are using it but the code's embarrassingly hacked-together and you have no idea when you implemented what or why.

\n\n

So we have git and bzr and svn and mercurial and thousands of apps for the actual version control of the files themselves, but it's still useful to keep track numerically of when major changes were made to the app and approximately how far into the development cycle it is.

\n\n

And, at the end of the day, a website is as much an application as any desktop app is, and really should be versioned as such. If even to ensure that your Capistrano deployment pushed the correct version of your app onto your server, it's worth it to ultimately prevent what would otherwise be a two-hour goose-chase trying to figure out why none of the changes that you just committed manifested themselves in any capacity.

\n\n

So keeping track of your version number is really simple. As simple as:

\n", nil, "
\n
#!/app/helpers/application_helper.rb\n\nAPP_VERSION = "0.4.3"
\n
\n", nil, "

Which is simple enough and certainly gets done what you want, and doesn't interfere with the VERSION constant that Rails sets to determine its own version, and will allow you to print the version in any of your views or layouts by just calling that APP_VERSION constant.

\n\n

And that's all good and well and effective, but say you want something a little more flexible, if, say you needed to pull out a specific part of that version number without doing something like:

\n", nil, "
\n
#!/app/helpers/application_helper.rb\n\ndef get_minor_version\n  APP_VERSION.split(".")[1]\nend
\n
\n", nil, "

Which I guess, at the end of the day, really isn't a terrible solution, but it's a bit prettier to use a namespace here, namely, a module, like this:

\n", nil, "
\n
#!/app/helpers/application_helper.rb\n\nmodule APP_VERSION\n  MAJOR = 0\n  MINOR = 4\n  TINY = 3\n\n  def self.print\n    "\#{MAJOR}.\#{MINOR}.\#{TINY}"\n  end\nend
\n
\n", nil, "

Now you can get the whole application version by calling:

\n", nil, "
\n
#!/app/views/layouts/application.html.erb\n\nVersion: <%= APP_VERSION.print %>
\n
\n", nil, "

Or just the minor portion of the version by calling:

\n", nil, "
\n
#!/app/views/layouts/application.html.erb\n\nMinor Version: <%= APP_VERSION::MINOR %>
\n
\n", nil, "

:: sigh of relief :: Ahhhhhhhhhh. Much better. Pretty, functional, flexible. Everything we'd want a version module to be. Right?

\n\n

And while I've geared this article (with the filenames, and such), to be Rails-friendly, you can really use this in any Ruby application. And I do. This is the format that I use for versioning gems that I build, which is especially useful for Rakefiles, etc.

\n\n

So that's it. Go ahead, add version numbers to your Rails app. At the very least you'll find it satisfying to see your application version progress as you marvel at how far you've come from your \"Hello, world\" days.

\n\n

At most, your users will acknowledge that you're actively developing your website, and be more encouraged to participate in a growing community rather than a seemingly stagnant one.

\n"]

no comments

login to post comments, or register to post a comment

latest links

Help.GitHub - Multiple SSH keys The article from github help mirroring this process
ones zeros majors and minors ones zeros majors and minors: esoteric adventures in solipsism, by chris wanstrath
ActiveScaffold A Ruby on Rails plugin for dynamic, AJAX CRUD interfaces

login

register activate reset

feeds

articles/rss

topics

staff

editor

about

doblock focuses on ruby, rails, and all things that can help ruby and/or rails programmers hone their skills.

Techniques, tutorials, news, and even free open-source applications, doblock seeks to fill in the cracks of the ruby/rails blogosphere.

doblock v. 0.10.1 powered by Rails