Cranking Code

Mark Holland

Whereupon I Receive an Email From Desert Island Discs

“Would you like to appear on the programme?” their email stated. “I’d love to.”, I replied. “I’ll take ‘Nimrod’, the overture from ‘Der fliegende Holländer’, ‘All Join Hands’ by Slade…”, and so on.

Of course that isn’t quite how it happened.

However I did receive a pleasant note for sending them a correction to the archive page of one of their castaways, the venerable actress Cicely Courtneidge.

Hi Mark

Thank you for your email to Desert Island Discs. I have changed the Albert Coates to Eric Coates. I think looking at the recording Albert might have been conducting but as you rightly point out Eric was the composer.

Thank you for your interest in our programme.

Best wishes

Desert Island Discs team.

How kind of them. It’s almost as if Roy Plomley had written it himself.

Running Padrino Rake Tasks in Production

An aide memoire for my own future reference.

In order to run Padrino Rake tasks in the production environment the ENV variable needs to be placed before the command. It took a fair bit of flailing about before discovering this.

For example:

1
PADRINO_ENV=production bundle exec padrino rake -T

Styling asp.net MVC Validation Summary as a Twitter Bootstrap Flash

The asp.net MVC validation summary is a bit plain. Let’s beautify it.

First grab the alert styles from the Twitter Bootstrap source and add the ‘.validation-summary-errors’ class selector where appropriate. I’ve used the “danger” (red) version of the flash.

1
2
3
4
5
6
7
8
9
10
   .validation-summary-errors,
  .alert-message
  {
     clear:both;
    position: relative;
    padding: 7px 15px;
    margin-bottom: 18px;
    color: #404040;
  
  etc...   

Just that alone will make things look miles better already.

However the Bootstrap flash has a close button on it in order to hide the message away which is kind of neat. Let’s use JavaScript to add that.

First we need to know if flash is present. If a full page postback has triggered the showing of the validation error message then we can just look for it in the DOM on document ready.

1
2
3
4
5
   var $validationSummaryErrors = $('.validation-summary-errors');
  
  if ($validationSummaryErrors.length !== 0) {
      addCloseButton($validationSummaryErrors);
  }

Simple.

But what if the validation error message was injected via client side code?

jquery.validate.unobtrusive.js, the Javascript code which performs client side model validation, runs on page load and has no input parameters so we can’t pass in a callback directly. Instead, I’m afraid, we need to add a function to the global object.

1
2
3
   window.onValidatorSummaryShown = function () {
      addCloseButton(this);
  };

And we’ll call it when the ‘.validation-summary-errors’ div is injected into the DOM.

1
2
3
    if (window.onValidatorSummaryShown) {
                window.onValidatorSummaryShown.call(container);
     }

And there you have it, a beautiful and arresting validation error message complete with working close button.

The code in full:

Introduction

Hello there.

This is a new blog where I intend to document any new things I learn within the realm of software development. Like jazz music, this is primarily for my own amusement but should anyone else benefit from this exercise then that’s good too.

I’ve got 13 years professional experience yet I believe the times we now find ourselves in to be the most exciting I’ve known. There’s so much interesting stuff flying about these days, it’s amazing.

Long ago I was in a corporate rut. Sure we were doing some pretty neat things with C++ and COM (that dates it!) but we were in our little silo and everybody else was in theirs. The global fraternity of developers that we now know was unimaginable until recently. We had the Borland email circular and that was about it. Now we’re all in touch on Twitter and collaborating with strangers on open source projects in Github. This rising tide lifts all boats.

My areas of interest are dotNet for business and Ruby for pleasure. Hopefully the Ruby can be inveigled into the business zone too moving forward. (I’ve already made a start on that process!)

What I really want to get a handle on in the coming months as the dark night draw in are:

  • Testing
  • Sass/Compass
  • Coffee Script

I’ve already converted a Ruby on Rails app to 3.1 and activated the asset pipeline so the Sass and Coffee Script shouldn’t require too much setting up in order to get started. Also this Octopress blog engine uses Sass so that’s something else to poke around with. I can really see how both technologies improve what has preceded them and I’m keen to get my hands on them.

As regards testing, whilst I’m totally sold on the idea I just haven’t got to grips with it yet. I read on Twitter a while back, ‘Test Driven Development is the “rock hard abs” of programming. Everybody wants it. Nobody wants to do the work to get it.’ I know that I’ve got to knuckle down and get it sorted. But alas it’s too easy to just write code and load up the resultant site in the browser.

Fingers crossed, but I should have a juicy little project on the side coming up. I’m considering using Padrino for it as I love the idea of a Ruby take on Django, sitting somewhere between the lightness of Sinatra and the all encompassingness of Rails and complete with the built in admin side. This would seem like a good opportunity to try to turn some of these notions into reality.

Hopefully putting all that out in public will be an incentive to crack on.