Viewing entries in
Work

3 Comments

Easy version management for Rails apps using VersionMaster and Capistrano

Working on the new version of subdelay.com I came up with a couple of plugins for Rails that although functionality-wise are dirt simple turned out to be rather useful. Therefore I have decided to open source them on GitHub and also do a little writeup here on the blog. VersionMaster

Sub Delay Version Number

First up is VersionMaster. I needed a simple way to manage a version number (eg. "v1.0.8" etc) for the site, in order to keep track of minor and major updates as I changed things around and added new features. More importantly, I wanted to have this happen automatically whenever I deployed a new release using Capistrano. I first took a look at toland's app_version but it didn't quite do what I needed so I came up with my own little implementation.

Using VersionMaster

Install the plugin using

script/plugin install git://github.com/balvig/version_master.git

The file "version.yml" will be copied into the config directory:

You can edit this file to manually set the version number or use the rake tasks below:

You can also use the built-in Capistrano recipe to update versions when deploying (which is how I use it):

You can then use the constant APP_VERSION in your views or elsewhere to display the current version.

Get VersionMaster from GitHub.

3 Comments

4 Comments

Applying DRY principles from Rails to Flex + Cairngorm

I am currently in the process of re-building www.subdelay.com in order to accommodate some of the changes I mentioned earlier and also to make the site more maintainable and easier to expand upon than the current version. The original site was created in Flash 7, meaning that everything was done in "good old" Actionscript 2. The second incarnation of Actionscript never really had a good structure for keeping code organized and this paired with the fact that I at the time to be honest didn't really know what I was doing and was picking up things as I went has resulted in a...shall we say less than pretty codebase, that wouldn't be much fun to try and clean up and re-use at this point.

Hence the decision to completely rewrite the site, and in an effort to keep things a bit systematic I have been using Flex for the task this time around. Also, while the original site uses PHP as its backend, with all the Ruby work I have been doing lately it only seemed natural to have the new version run on Ruby on Rails.

I am however a newcomer to Flex and have been relying on the book Flex on Railsfor support. The book does a fairly good job of explaining how to plug a Flex frontend into a Rails app, but there is one thing that has been bothering me a bit about the whole process.

Although the book touches upon the virtues of keeping things "DRY" on the Rails side of things by employing "convention over configuration" and making good use of the refactoring techniques that seem so natural when using Ruby, from reading the book it seems that these principles don't apply when writing the Actionscript 3-based Flex code.

In the case of subdelay.com I am using the Cairngorm framework, and from reading through the examples in the book, it seems that the way to do things is to:

  • Create a "delegate" for every resource you have (for subdelay.com this might be "Artists"," Tracks", "Albums", "Cart" etc).
  • Create an "event" for every user gesture/application event ("PlayTrackEvent","EmptyCartEvent","LoadAllAlbumsEvent","LoadAllArtistsEvent", etc)
  • Create a corresponding "command" for each event that tells the program how to react whenever an event occurs (thus we have "PlayTrackCommand", "EmptyCartCommand" and so on)

Now, each of these are represented by a separate file with a separate class definition, and to me this just seems like an unnecessary amount of similar code divided among an unnecessarily large number of files that you have to keep track off for something that seems like it should be much simpler.

The part where it gets particularly weird for me is when creating the delegates. You are basically creating a class for each resource you want to access on the Rails side, but the code in these files end up being almost completely identical since all they do is expose the typical REST-based methods ("index","show","create","destroy","update") for different Rails controllers with only a few variations for the controllers that have additional non-standard methods defined (such as the method "recent" in my case, for loading only the most recent albums). Using subdelay.com as an example you then end up with a file structure that looks something like this:

So many delegates..

This just didn't seem right to me, so I went ahead and had a go at replacing all the individual delegate classes with one generic "RestfulDelegate" class, which works with any Rails-based resource I care to throw at it and contains the 5 basic REST methods as well as a 6th generic method that allow the use of any custom methods as needed.

When calling out to your delegates from a Cairngorm ICommand you would usually write something like

var delegate:AlbumsDelegate = new AlbumsDelegate(this);

and have AlbumsDelegate hardcoded to use the "albums" service. Instead, when using RestfulDelegate you simply write:

var delegate:RestfulDelegate = new RestfulDelegate(this,'albums');

Passing in "albums" as the second argument ensures that the right service is selected (configured in Services.mxml) and therefore only one class is required for all resources exposed in the Rails app, and I don't have to create a new file for every new resource I add. Having the generic "send" method also allows me to deal with situations where I have added custom methods to a controller. So for example the "recent" method found in the AlbumsController can be called in the following way:

public function execute(e:CairngormEvent):void { var delegate:RestfulDelegate = new RestfulDelegate(this,'albums'); delegate.send('recent',{limit:5}); }

I guess I could even have gone as far as to replace all the other methods with this one method to even further reduce code, but for the time being I kinda like the idea of having the basic bread/butter methods hard coded.

It's all very basic but I am wondering, is there something I am missing here? Am I doing something that will get me in trouble later?? :o I realize there might be some sense in having a separate delegate for each resource if you are planning on replacing the Rails backend for that resource with an external API or something like that at a later stage, but apart from that I don't really see the advantage of having to deal with all those files and classes.

I am currently trying something similar to reduce the amount of code repetition required for events and commands, but haven't yet figured out the best way to go about it. Will let that be the subject of a future post I guess!

4 Comments

2 Comments

Redesigning Balvig.com

Hey! As I am sure you have noticed by the time you read this, things around here have changed quite a bit. I have spent the last couple of weeks reorganizing Balvig.com and the result is what you see before you. The main reason for this change is actually related to a change In Real Life that happened a few weeks back.

Since April I have begun working part time for my current company in order to allocate more time to doing my own projects. It is not that I don't enjoy the work at Export Japan, but the fact that working 5 days a week from 9am-6pm was preventing me from spending time on things I used to love doing (such as music, creating new web sites/applications etc) had been bothering me for a while and thus after having several talks with my boss last year I finally made the decision to go part time.

With this change I realized I needed a platform to write about these personal projects and this blog seemed like the perfect place to do it. However, the blog in its previous incarnation wasn't really suited for this, so last week I finally found time to think up a better format resulting in the following changes to the site:

  • The posts on the blog have been split into two major sections, which (for want of better names) have been dubbed "Work" and "Play" respectively.
  • The "Work" section covers anything related to, well...work! Not the best name for it perhaps, since the border between work/play isn't crystal clear, but I am sticking with this naming scheme until I can think of something better. In this section I hope to write about my music, programming and perhaps even some of the projects I work on for Export Japan. You may consider this a collection of my "output" to the world if you will. So far I only have one project in there (Sub Delay) but expect to see more added as I get around to it.
  • The "Play" section is where I post all the other stuff that goes on in everyday life that is not strictly related to work/projects. Trips, food, random articles about life in Japan, in other words the "inputs" I receive on a daily basis (continuing the fairly weak analogy from above :P) In the sidebar of this section you can also see my Flickr pictures, find out what music I am listening to (Kenneth Bager right now actually), read my Twitter feed, and so on.
  • The "Home" top page shows posts from both sections.
  • I have moved the whole blog from www.balvig.com/blog to www.balvig.com and gotten rid of that boring (and unnecessary) splash page that used to reside at the top of site. Please update any bookmarks/rss-feeds (although you should be redirected automatically)

On a final note: After having been "catching up" for almost two years and still not actually reaching the present, I realize it is time to give up this endeavour and start focusing on The Now. Thus, I am officially putting an end to this series as of today!

2 Comments