Format time_select to nearest 15th minute

Posted on May 28, 2007
time_select is a nice little rails helper that generates dropdown lists for populating time fields, like this:


However, it can get a bit messy for users when they come to use the thing, displaying every minute from 1-60 like this:

You can dress it up nice though by using the :minute_step option. Setting it to 15 just lists the options 0, 15, 30 and 45 like this:
<%= time_select :gig, :doors_open, :minute_step => 15 %>

Better structured Selenium Tests

Posted on May 28, 2007
Selenium is a fantastic tool developed by ThoughtWorks for automated web acceptance tests.

If you're a web developer and you've never used this tool, go check it out immediately!

How I've been generally Selenium is by using the Selenium IDE Firefox Plugin to record my tests - and then copying the automatically generated C# into my test classes.

This is a really quick and easy approach, however the test code get's REALLY ugly REALLY fast.

Then I saw Erik Doernenburg's excellent presentation on Selenium best practices, and heard a great tip that's so simple and elegant I felt stupid for not thinking about it before.
Create a class for each of your web pages and use static methods to wrap the Selenium code. This allows you to create nice, human readable tests that are also far less brittle.
MainPage.cs (C#)
public class MainPage
{
    public static void Open()
    {
      Selenium.Open(@"/");
    }

    public static void SearchFor(string searchString)
    {
      // Ugly brittle code nicely wrapped up
      Selenium.Type("txtSearch", searchString);
      Selenium.Click("btnSubmit");
      Selenium.WaitForPageToOpen("30000")
    }
   
    public static int NumberOfSearchResults()
    {
      // This function uses regex to return the number of results on the page
      ..
    }
}
And then in your unit test class:
MainPageUnitTests.cs (C#)
   [Test]
   public void TestForSearch()
   {
     // Nice readable test logic
     MainPage.Open();
     MainPage.SearchFor("bananas");
     Assert.AreEqual(1, MainPage.NumberOfSearchResults(), "Wrong number of results");
   }

Global layouts in Rails

Posted on May 28, 2007
I'm just starting out with Rails, and making some ridiculous mistakes along the way.

One of my mistakes was to basically copy the same layout for each controller, not realising that there's such a thing as application.rhtml.

Application.rhtml allows you to create a global template for the whole site. The problem comes when you want to include controller specific bits on the page (like a side bar).

A trick I used (and I'm sure there's probably a million better ways of doing this) is to put a partial in the application.rhtml that you know will be used in the controllers. For example:
application.rhtml

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <title>Your application</title>
</head>
<body>
    <div id="left">
       <%= render :partial => 'sidebar' %><br/>
    </div>
    <div id="main">
    	<%= yield :layout %>
    </div>
</body>
</html>

Then in your controller view folders, include a _sidebar.rhtml partial like this:
 _sidebar.rhtml in views/user 

This is the User Controller Sidebar
<%= link_to "logout" %>
 _sidebar.rhtml in views/admin

This is the Admin Controller Sidebar
<%= link_to "Add User" %>
<%= link_to "Remove User" %>

This way you can have a global layout, but also unique side bar details.

Batch Insert of Records in .Net

Posted on May 27, 2007
So, I've got a DataTable behind the scenes practically packed with data (1000s of rows) - and I want to insert this into a table on sql server using a stored procedure. The wrong way:
//The dsSourceDataSet is a populated dataset with1 table with tonnes of rows
foreach (DataRow row in dsSourceDataSet[0].Rows)
{
  SqlCommand command = new SqlCommand("sp_my_proc", connection)
  command.Parameters.AddWithValue("@param1", row["field1"].Value);
  command.Parameters.AddWithValue("@param2", row["field2"].Value);
  //Yada yada
  command.ExecuteNonQuery();
}
I mean, let's face it - this works so I thought "Hey, that's good enough for me!" But man was it sloooow! Then my boss showed me the CORRECT way of doing things: The right way:
SqlCommand command = new SqlCommand("sp_my_proc", oConnSource);
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
SqlParameter param1 = dataAdapter.InsertCommand.Parameters.Add("@param1", SqlDbType.VarChar);
param1 .SourceColumn = "field1";
param1 .SourceVersion = DataRowVersion.Original;
dataAdapter.Update(dsSourceDataSet);
So what's happening? Instead of looping through the table like a crazy gooloot - everything gets crammed into the dataset and fired off to the database! And it is SOOO much faster! This process took a little over an hour before, now it takes a few minutes! Hooray!

Automatically load the Asp.net Web Server

Posted on May 27, 2007
When I'm debugging my Asp.net applications, I find myself just wanting a quick and easy way of starting the asp.net web server. You can do this from the command line like this:
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.exe" /path:[PATH OF YOUR WEB APP] /port:[WEB PORT]  /vpath:[/mywebapp]

You can also use the funky explorer plug-in designed by Robert McClaws here. This lets you run the webserver just by right clicking on a folder path.

Zlib:BufError

Posted on May 27, 2007
white out
So, I started installing Capistrano yesterday using:
gems install capistrano
Annoyingly, the net-ssh dependency failed with the hideous error message:
ERROR: While executing gem … (Zlib::BufError) buffer error
This seems to only happen on windows - and there doesn't seem to be any documentation on how to fix this. Thankfully, you can fix this problem by upgrading your RubyGems install using
gem update --system
I then installed Capistrano with no probs!