<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Kirk Bushell - Home</title>
  <id>tag:www.kirkbushell.com,2010:mephisto/</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://www.kirkbushell.com/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://www.kirkbushell.com/" rel="alternate" type="text/html"/>
  <updated>2009-09-30T12:03:25Z</updated>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2009-09-30:2069</id>
    <published>2009-09-30T12:03:00Z</published>
    <updated>2009-09-30T12:03:25Z</updated>
    <category term="php"/>
    <category term="smarty"/>
    <category term="templating"/>
    <link href="http://www.kirkbushell.com/articles/phlite-a-light-weight-php-templating-engine" rel="alternate" type="text/html"/>
    <title>PHLite - A Light-Weight PHP Templating Engine!</title>
<content type="html">
            &lt;p&gt;After playing around with frameworks such as Ruby on Rails, CakePHP, CodeIgniter, and many others - I came to like the templating systems they used. They were quite fast, efficient, and importantly - very easy to use. However, with a current task I've been given, I've realised that there are some of us out there who aren't using these frameworks, dislike templating engines like Smarty, but need a good, robust templating system. Enter &lt;a href=&quot;http://github.com/torm3nt/phlite&quot; alt=&quot;PHLite - a light-weight PHP templating engine&quot;&gt;PHLite&lt;/a&gt; (pronounced flight).&lt;/p&gt;

&lt;p&gt;Would love to hear people's comments about this! &lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2009-07-14:1901</id>
    <published>2009-07-14T05:46:00Z</published>
    <updated>2009-07-14T05:46:23Z</updated>
    <category term="config"/>
    <category term="configuration"/>
    <category term="database"/>
    <category term="environment"/>
    <category term="rails"/>
    <link href="http://www.kirkbushell.com/articles/custom-database-yml-files-for-development-environments" rel="alternate" type="text/html"/>
    <title>Custom database.yml files for development environments</title>
<summary type="html">Working as part of a large team can mean one of two things: either you all have the exact same development environment, or you all have different setups with your own settings. Which one is most likely? In this article I'll highlight how to customize your rails application so that it will load your database configuration file rather than depending on the same one for everyone.</summary><content type="html">
            Working as part of a large team can mean one of two things: either you all have the exact same development environment, or you all have different setups with your own settings. Which one is most likely? In this article I'll highlight how to customize your rails application so that it will load your database configuration file rather than depending on the same one for everyone.
&lt;p&gt;In a previous development role we used to have everyone with their own database.yml file, based on the hostname. However, this was also loaded within the database.yml file itself. It would look for a custom database configuration file then load that in to itself, overloading any previously-defined configuration settings for dev/test. This was great, but was a little messy. An easier way to do this, is to define the configuration file that gets loaded within your environment.rb file:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;// this goes within the Rails::Initializer block
require 'socket'
database_file = ['config/', Socket.gethostname, '-database.yml'].join
config.database_configuration_file = database_file if File.exists? database_file&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Effectively what we're doing here is looking for a file within the config directory that matches something like: my-hostname-database.yml, and if it finds it - it will load this, otherwise it will retain the default config/database.yml.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2009-07-10:1886</id>
    <published>2009-07-10T11:03:00Z</published>
    <updated>2009-07-10T11:05:31Z</updated>
    <category term="ati"/>
    <category term="catalyst"/>
    <category term="jaunty"/>
    <category term="ubuntu"/>
    <link href="http://www.kirkbushell.com/articles/ati-catalyst-on-ubuntu-jaunty" rel="alternate" type="text/html"/>
    <title>ATI Catalyst on Ubuntu Jaunty</title>
<content type="html">
            A lesson in patience, time, and lots and lots of reading. I finally stumbled across a wiki tutorial for an old version of the ATI Catalyst drivers for Ubuntu 7, which worked great for Jaunty as well! It's a long tutorial but well worth it if you're having problems. So if you are,  &lt;a href=&quot;http://wiki.cchtml.com/index.php/Ubuntu_Gutsy_Installation_Guide#Method_2:_Install_the_Catalyst_Driver_Manually&quot;&gt;check out the Catalyst for Jaunty tutorial&lt;/a&gt;.
          </content>  </entry>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2009-05-28:1810</id>
    <published>2009-05-28T00:51:00Z</published>
    <updated>2009-05-28T00:52:39Z</updated>
    <link href="http://www.kirkbushell.com/articles/rails-2-3-2-on-ubuntu-jaunty-net-https-error" rel="alternate" type="text/html"/>
    <title>Rails 2.3.2 on Ubuntu Jaunty - net/https error</title>
<content type="html">
            &lt;p&gt;While working with the new version of Rails, I got an error that looked like the following:&lt;/p&gt;

&lt;p&gt;/home/.../.gem/ruby/1.8/gems/rails-2.3.2/lib/initializer.rb:269:in `require_frameworks': no such file to load -- net/https (RuntimeError)&lt;/p&gt;

&lt;p&gt;This can be remedied by installing the libopenssl library: sudo aptitude install libopenssl-ruby&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2009-05-13:1790</id>
    <published>2009-05-13T00:23:00Z</published>
    <updated>2009-05-13T00:35:11Z</updated>
    <link href="http://www.kirkbushell.com/articles/new-site-design" rel="alternate" type="text/html"/>
    <title>New Site Design!</title>
<summary type="html">Thanks to &lt;a href=&quot;http://mephistoblog.com/&quot; title=&quot;Mephisto Blogging Software&quot;&gt;Mephisto&lt;/a&gt;, this has been an incredibly painless process. I've introduced features and categorised some of the content here much more easily than I would have thought. And so now I introduce to you - &lt;a href=&quot;http://www.kirkbushell.com&quot; title=&quot;Kirk Bushell&quot;&gt;kirkbushell.com&lt;/a&gt; v2.0!</summary><content type="html">
            Thanks to &lt;a href=&quot;http://mephistoblog.com/&quot; title=&quot;Mephisto Blogging Software&quot;&gt;Mephisto&lt;/a&gt;, this has been an incredibly painless process. I've introduced features and categorised some of the content here much more easily than I would have thought. And so now I introduce to you - &lt;a href=&quot;http://www.kirkbushell.com&quot; title=&quot;Kirk Bushell&quot;&gt;kirkbushell.com&lt;/a&gt; v2.0!
&lt;p&gt;Well it hasn't taken very long, thankfully. The longest process was the design aspect and getting it to a point that I wanted to keep it (I think I went through 4 or 5 iterations of the new design). Getting it into Mephisto was ridiculously easy, and I was even able to organise some of the posts and articles without having to touch the rails core, which I was very happy about.&lt;/p&gt;

&lt;p&gt;I initially wanted to do the site in HTML5, but figured I can slowly change the site over as I get time. It's certainly on my priority list as I'd like to come to grips with the new features of HTML5, but all in time. I still have various other projects that need completion before I can do that.&lt;/p&gt;

&lt;p&gt;Hope you all like the new design, and look forward to your comments and feedback!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2009-05-10:1781</id>
    <published>2009-05-10T07:05:00Z</published>
    <updated>2009-05-17T00:06:57Z</updated>
    <category term="Blog"/>
    <category term="array"/>
    <category term="function"/>
    <category term="php"/>
    <link href="http://www.kirkbushell.com/articles/retrieve-the-first-element-of-an-array" rel="alternate" type="text/html"/>
    <title>Retrieve the First Element of an Array</title>
<summary type="html">It's fairly general knowledge in PHP that if you want the first item of an array, you can use array_shift. However, this also removes the element from the array and this may not be the intended functionality of the developer. Introducing: array_first.</summary><content type="html">
            It's fairly general knowledge in PHP that if you want the first item of an array, you can use array_shift. However, this also removes the element from the array and this may not be the intended functionality of the developer. Introducing: array_first.
It's fairly general knowledge in PHP that if you want the first item of an array, you can use array_shift. However, this also removes the element from the array and this may not be the intended functionality of the developer. Introducing: array_first. array_first allows you to see the first element of an array, simply by passing the array. It can be a numerical or associative array, it just has to be an array.

&lt;pre&gt;&lt;code class=&quot;php&quot;&gt;function array_first($array)
{
  if (!is_array($array)) {
    throw new Exception('Argument is not an array.');
    return false;
  }
  $slice = array_slice($array, 0, 1);
  return $slice[0];
}&lt;/code&gt;&lt;/pre&gt;

&lt;i&gt;Updated the function to use array_slice() rather than finding the array keys, thanks &lt;a href=&quot;http://blog.joebeeson.com/&quot;&gt;Joe&lt;/a&gt;!&lt;/i&gt;
          </content>  </entry>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2009-04-14:1654</id>
    <published>2009-04-14T05:28:00Z</published>
    <updated>2009-05-12T06:35:23Z</updated>
    <category term="Blog"/>
    <category term="directory"/>
    <category term="function"/>
    <category term="php"/>
    <link href="http://www.kirkbushell.com/articles/directorize-function" rel="alternate" type="text/html"/>
    <title>directorize()  function</title>
<summary type="html">After having dealt with some directory-creation lately based on user input, I decided to safe-guard the creation of directories with the following function: directorize().</summary><content type="html">
            After having dealt with some directory-creation lately based on user input, I decided to safe-guard the creation of directories with the following function: directorize().
&lt;pre&gt;&lt;code class=&quot;php&quot;&gt;function directorize($dir) {
  return strtolower(preg_replace('/[^a-z0-9\-]/i', '', str_replace(' ', '-', $dir)));
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What this function does is replaces all instances of any character that is not a letter, number, or dash (we replace spaces with dashes for directory creation), with nothing, so an example such as:&lt;/p&gt;

&lt;p&gt;The Lazy Fox is Just plain lazy!&lt;/p&gt;

&lt;p&gt;Would return: the-lazy-fox-is-just-plain-lazy.&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2009-04-08:1650</id>
    <published>2009-04-08T01:12:00Z</published>
    <updated>2009-05-12T05:38:00Z</updated>
    <category term="Blog"/>
    <category term="planning"/>
    <link href="http://www.kirkbushell.com/articles/using-the-right-tool-for-the-job" rel="alternate" type="text/html"/>
    <title>Using the right tool for the job</title>
<summary type="html">Having now had some experience with a multitude of programming frameworks, including CakePHP, Codeigniter, Ruby on Rails, amongst various others, I'm beginning to see a pattern develop within the development community. This pattern involves the seductive nature of well-written frameworks pulling the developer in to it's curvaceous body, so much so that the developer becomes blinded by the possibilities before it.</summary><content type="html">
            Having now had some experience with a multitude of programming frameworks, including CakePHP, Codeigniter, Ruby on Rails, amongst various others, I'm beginning to see a pattern develop within the development community. This pattern involves the seductive nature of well-written frameworks pulling the developer in to it's curvaceous body, so much so that the developer becomes blinded by the possibilities before it.
&lt;p&gt;The main problem I see, is that with (especially good ones) frameworks, developers are moving towards these solutions for just about anything and everything, and for most parts of the web, this is fine. However, when developers become so clung to a framework whereby they think it is the solution for everything, stumbling blocks begin to materialise and the solutions become ugly.&lt;/p&gt;

&lt;p&gt;For the most part, frameworks provide developers with an easy interface to generate websites at breakneck speed, thereby allowing the development community to industrialize the development process (cheers Khaless for the term). What this means is that we're able to pump out solutions at speeds never thought possible, especially with frameworks such as Django, RoR, CakePHP and others. However, what does this mean for development in general?&lt;/p&gt;

&lt;p&gt;Using heavy-handed frameworks means that one thing is generally left behind - performance. The difference in performance between a site using a framework specifically tailored for it's product, and one that uses a generic framework which is built to cater for an endless number of options for websites, can be staggering. Of course, there is a tradeoff to this performance hit - development time. Development time is generally much more expensive than hardware (unless you're serving millions of page view per day), and so therefore (depending on the project) a sound and educated decision should be made, rather than simply jumping into your favourite development framework.&lt;/p&gt;

&lt;p&gt;One of my close colleagues uses a particular framework for just about everything, and although this framework in it's own right is a great product which allows people like myself to develop applications very quickly - it's also one of the heaviest frameworks available, meaning it's one of the slowest to serve pages. For example, even though development of a mainly static website could be easier to develop using it, what we're doing is using a full-stack solution to serve basically static pages. What does this mean? We're effectively cutting down the performance from 1000s of pages/second to approximately 50 or under. &lt;/p&gt;

&lt;p&gt;This brings me to my original point - use the right tool for the job! Big frameworks are great for spiking out and even developing the initial stages of products, so as to get it to the public in a quick and orderly fashion. However, as the needs for the product changes and popularity grows, the framework of choice can (but not always) become a real hurdle that could have been overcome earlier in the development process if the project was given it's due respect and investigated in depth for the right technologies and solutions to be used.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2009-03-12:876</id>
    <published>2009-03-12T12:16:00Z</published>
    <updated>2009-05-12T06:37:31Z</updated>
    <category term="Blog"/>
    <link href="http://www.kirkbushell.com/articles/working-on-a-new-project" rel="alternate" type="text/html"/>
    <title>Working on a new project</title>
<summary type="html">Always something new to do. This is something I've been wanting to do for quite some time, I'm just finally getting around to doing it as I seem to have a little bit of free time at the moment, which is always good for getting personal projects done.</summary><content type="html">
            Always something new to do. This is something I've been wanting to do for quite some time, I'm just finally getting around to doing it as I seem to have a little bit of free time at the moment, which is always good for getting personal projects done.
This is something that's been in the pipeline since at least 2002 (When I built droidsrpg.com, now down). Ever since those early days of browser-based game development, I had a vision - I knew I could create something quite cool, the idea was sitting in my head, festering away.&lt;br /&gt;
&lt;br /&gt;
Well, I've finally got off my butt and started making some real progress on this project. The game is still very early in the process, still mapping out features will work, where the game mechanics will take the player and so on, as well as a business model. This is my most ambitious project to date, but also the only one I've been interested in doing for over five years. I will be posting regular updates here as to my progress, as well as the lessons learned along the way. Adios!
          </content>  </entry>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2009-03-03:394</id>
    <published>2009-03-03T23:15:00Z</published>
    <updated>2009-05-12T06:40:52Z</updated>
    <category term="Blog"/>
    <category term="rails"/>
    <category term="themes"/>
    <link href="http://www.kirkbushell.com/articles/theme_support-plugin-not-that-evil" rel="alternate" type="text/html"/>
    <title>Theme_support plugin - not that evil</title>
<summary type="html">I had a bit of a stint recently developing some pseudo-theme functionality for a project here at &lt;a href=&quot;http://www.westfield.com.au/corporate&quot;&gt;Westfield&lt;/a&gt;. During that time, I've discovered a few &quot;gotchas&quot; relating to the view_paths variable within the controller.</summary><content type="html">
            I had a bit of a stint recently developing some pseudo-theme functionality for a project here at &lt;a href=&quot;http://www.westfield.com.au/corporate&quot;&gt;Westfield&lt;/a&gt;. During that time, I've discovered a few &quot;gotchas&quot; relating to the view_paths variable within the controller.
One of the most recent tasks I've had to complete at the office was to provide some sort of templating/theming system for one of our products. After a very small search, I stumbled upon the view_paths array and started fiddling, only to find I couldn't get layouts to work. I then moved onto a plugin called theme_support and had exactly the same problem. This caused days of grief (4, to be exact) without it ever occurring to me that maybe I'd have to set the layout.

Well this morning I went about trying just that and lo and behold - IT WORKS! This is an incredibly frustrating lesson, as a simple one-line piece of documentation within the plugin would state that a layout has to be specified in order for this to work. Now, I could be wrong, but I'm fairly certain rails by default looks for a layout file of the same name as the controller aka: layouts/controller.html.erb. Considering that it's not able to do this unless a layout is specified, leads me to believe that there may be a bug within the layout.rb file that prevents this search from working if no layout is specified, but the view_paths are changed.

In any case, I will be adding some comments to the theme_support plugin, as I think this is fairly fundamental and has been overlooked. Now that I have got it to work, theme_support is working as intended and I can get on with my life. Thanks to everyone on the rails-oceania Google Group for helping me out, especially Terry Heath.

Moral of the story - test every single part of code given in examples when working with a new plugin.
          </content>  </entry>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2008-12-08:23</id>
    <published>2008-12-08T23:30:00Z</published>
    <updated>2009-05-13T05:59:09Z</updated>
    <category term="Blog"/>
    <category term="array"/>
    <category term="function"/>
    <category term="php"/>
    <link href="http://www.kirkbushell.com/articles/array_collect-mapping-required-fields-to-a-php-array" rel="alternate" type="text/html"/>
    <title>array_collect, mapping required fields to a PHP array</title>
<content type="html">
            After delving around in ruby for a little and finding the magic that is the Array.collect method, I decided to write one for PHP, considering how much I use it in Ruby. Hopefully others will find some use in this function.

&lt;pre&gt;&lt;code class=&quot;php&quot;&gt;&amp;lt;?php
function array_collect($array, $params)
{
	$return = array();
	if (!is_array($params)) {
		$params = array($params);
	}
	
	foreach ($array as $record) {
		$rec_ret = array();
		foreach ($params as $search_term) {
			if (array_key_exists($search_term, $record)) {
				$rec_ret[$search_term] = $record[$search_term];
			}
		}
		
		if (count($rec_ret) &gt; 0) {
			$return[] = $rec_ret;
		}
	}
	
	return $return;
}
?&amp;gt;&lt;/code&gt;&lt;/pre&gt;

Basically, what this allows us to do is extract information from an array and have it stored in another array. For example, we may only want a few select fields as a subset of data, we could do this by doing:

&lt;pre&gt;&lt;code class=&quot;php&quot;&gt;
$records = array(array('a' =&gt; 'y', 'b' =&gt; 'z', 'c' =&gt; 'e'), array('a' =&gt; 'x', 'b' =&gt; 'w', 'c' =&gt; 'f'));
$subset1 = array_collect($records, 'a'); // $subset1 will be: array(array('a' =&gt; 'y'), array('a' =&gt; 'x'));
$subset2 = array_collect($records, array('a', 'c')); // $subset2 will be: array(array('a' =&gt; 'y', 'c' =&gt; 'e'), array('a' =&gt; 'x', 'c' =&gt; 'f'));
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2008-11-12:10</id>
    <published>2008-11-12T05:12:00Z</published>
    <updated>2009-05-13T23:13:39Z</updated>
    <category term="Blog"/>
    <category term="example"/>
    <category term="model"/>
    <category term="rspec"/>
    <category term="tests"/>
    <link href="http://www.kirkbushell.com/articles/rspec-model-example" rel="alternate" type="text/html"/>
    <title>An Rspec Model Example</title>
<summary type="html">Both in part due to the traffic coming to my blog via the subject line, and also my want to learn a bit more about RSpec model tests, I thought maybe it would be a good idea to throw together a more complicated model test, using RSpec. In this article, we'll look at the various methods you might have as part of... let's say... a vehicle model.</summary><content type="html">
            Both in part due to the traffic coming to my blog via the subject line, and also my want to learn a bit more about RSpec model tests, I thought maybe it would be a good idea to throw together a more complicated model test, using RSpec. In this article, we'll look at the various methods you might have as part of... let's say... a vehicle model.
&lt;p&gt;Before we get started, I'd just like to put a little disclaimer - this test will be done with database connectivity, meaning we will be hitting the database for any of the methods that will require it. This will ensure not only that our model works completely, but will also highlight any database issues we might have. Let's begin.&lt;/p&gt;

&lt;p&gt;For argument's sake, let's just say that our Vehicle table will contain a unique identifier, the name of the vehicle, a manufacturer's id, and a license plate. (id, name, manufacturer_id, license). This is what we'll use as the base fields for our tests.&lt;/p&gt;

&lt;p&gt;As stated previously, we're going to setup a model that will highlight the data one might find attributed to a vehicle. To ensure this article encapsulates a lot of what you would usually test in a model, we're also going to convolute it a little with silly extensions like a Doors association, just to stress a few points. Let's look at our base model.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class Vehicle &amp;lt; ActiveRecord::Base
  belongs_to :manufacturer
  has_many :doors, :wheels
  
  def self.vehicles_without_manufacturer
    Vehicle.find(:all, :conditions =&gt; {:manufacturer_id =&gt; nil} )
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here we can see that we have  Vehicle model, some basic associations and a custom method, which will retrieve all vehicles that do not have a set manufacturer. In the real world, obviously this wouldn't be the case, but we'll use this as a way of retrieving certain records. Let's look at the beginning RSpec tests.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
describe Vehicle do
  
  describe :vehicles_without_manufacturer
    fixtures :all

    before :each do
      @vehicle1 = Vehicle.create!( {:name =&gt; 'test vehicle 1', :license =&gt; 'abcdef'} )
      @vehicle2 = Vehicle.create!( {:name =&gt; 'test vehicle 2', :license =&gt; 'ghijkl'} )
      @vehicle3 = Vehicle.create!( {:name =&gt; 'test vehicle 2', :license =&gt; 'ghijkl', :manufacturer_id =&gt; 1} )

      @vehicles = Vehicle.vehicles_without_manufacturer
    end

    it &quot;should have only 2 vehicles that match the requirement&quot; do
      vehicles.length.should == 2
    end
    
    it &quot;should only contain the vehicles that have no manufacturer_id&quot; do
      vehicles.should == [@vehicle1, @vehicle2]
    end

  end

end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let's go over exactly what is going on here. First and foremost, we're grabbing all our fixtures for our project (these are generally located in test/fixtures, or spec/fixtures for rspec), and usually consists of an empty yml file, with the naming convention of [table].yml (where [table] is the name of the model representing your data). This ensures that each test we run, all data is cleared for that particular model, meaning we're starting from a clean slate each and every time.&lt;/p&gt;

&lt;p&gt;Next, we're setting up a way for us to pre-populate the database before each test. More than anything, this makes our tests easier to read, with each individual assert done as a separate test (in this example, ensuring our vehicle length is what it should be and that we're only fetching the matching vehicles).&lt;/p&gt;

&lt;p&gt;Now at this point you're probably wondering &quot;well, you're not really testing application-specific stuff, these are things already tested by ActiveRecord's own tests&quot;, and you'd be right. What we're really testing here, is to ensure that our database and associated data is setup correctly. Let's move onto something a little more application-specific.&lt;/p&gt;

&lt;p&gt;Let's add to our model the method below, which will give us the ability to assign the vehicle to a specified manufacturer, if it's not already assigned.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;def add_to_manufacturer(manufacturer)
  if self.manufacturer_id.nil?
    self.manufacturer = manufacturer
  end
end&lt;/code&gt;&lt;/pre&gt;

As you can see, we're only going to set the manufacturer if this particular vehicle record doesn't already have one assigned to it. Pretty straight-forward, right? Let's look at the test. What we're really looking to do, is mimic both conditions - one where a manufacturer_id is already set, and another where the condition is passed and we set the manufacturer, thereby covering all bases.

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;describe :add_to_manufacturer
  fixtures :all
  
  before :each do
    @vehicle1 = Vehicle.create!( {:name =&gt; 'test vehicle 1', :license =&gt; 'abcdef'} )
    @vehicle2 = Vehicle.create!( {:name =&gt; 'test vehicle 2', :license =&gt; 'ghijkl', :manufacturer_id =&gt; 2} )
  end
  
  it &quot;should add the manufacturer if it doesn't already exist&quot; do
    @vehicle1.add_to_manufacturer(Manufacturer.create!({field data}))
    @vehicle1.manufacturer.should_not be_nil
  end

  it &quot;should add the manufacturer if it doesn't already exist&quot; do
    @vehicle2.add_to_manufacturer(Manufacturer.create!({field data}))
    updated_vehicle = Vehicle.find_by_id(@vehicle2.id)
    updated_vehicle.manufacturer.should == @vehicle2.manufacturer
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The main aspect to point out here in our test addition, is the second &quot;should&quot;. Notably, we're ensuring that no changes are made to @vehicle2, because if it has changed - then we know that our code check has failed, and we need to have another look at our code.&lt;/p&gt;

&lt;p&gt;I hope this helps those of you looking for model examples - let me know if this was of any help, and if not - send me an e-slap and I'll write up another!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2008-11-04:5</id>
    <published>2008-11-04T04:17:00Z</published>
    <updated>2009-05-12T05:38:30Z</updated>
    <category term="Blog"/>
    <category term="controllers"/>
    <category term="rails"/>
    <category term="rspec"/>
    <category term="ruby"/>
    <category term="testing"/>
    <category term="unit"/>
    <link href="http://www.kirkbushell.com/articles/testing-using-rspec-part-2-the-controller" rel="alternate" type="text/html"/>
    <title>Testing using RSpec, Part 2 - the Controller</title>
<summary type="html">When first learning how to write tests for rails applications (or anything for that matter), using RSpec, probably my biggest stumbling block, was mocking and testing my controllers. There was just so much to take in it was hard to make sense of it all. But, little by little I made progress. In this article I'll go over writing some basic tests for your controller and offer some of the methods you can use to write your tests.</summary><content type="html">
            When first learning how to write tests for rails applications (or anything for that matter), using RSpec, probably my biggest stumbling block, was mocking and testing my controllers. There was just so much to take in it was hard to make sense of it all. But, little by little I made progress. In this article I'll go over writing some basic tests for your controller and offer some of the methods you can use to write your tests.
In order to make much sense of this, I'm going to do this article over three stages. The first stage will go over the controller and some simple methods one might find in a controller. The second stage will highlight some of the key aspects of testing our controllers and finally, the third stage - will see the article come together in a blur of RSpec, Controller and passing tests.

To start with, let's create a controller that will handle the various actions a user might do when handling their own account. I'm going to keep this controller fairly simple, as I want to focus on the testing, not the code itself, so please bear in mind that when writing your own controllers to ensure you follow the appropriate procedures to make your application secure and stable.

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class UsersController &amp;lt; ApplicationController
  def login( username, password )
    user = User.login( username, password )
    unless user.blank?
      session[:user] = user.id
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

When writing tests for our controllers, we want to ensure that our tests are as isolated as possible. Why? We only want to test the code we're working with and if we can, leave any dependencies of our controller outside in the cold. Reason for this is so that should we encounter any problems, either with our code or our tests - we know exactly where the problem is and how to fix it. As soon as you start introducing tests for code from outside the controller, you then begin to see an example of ambiguity as you now have to debug the code further to find the exact problem - and this should have been picked up in other tests, either in your models, or 3rd party libraries.etc. Add to this the fact that your tests get messy, and it's easy to see why this sort of &quot;behaviour&quot; is shunned against. Let's begin with some tests for the login method.

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;describe UsersController do
  describe :login do
    before :each do
      @params = {:username =&gt; 'username', :password =&gt; 'password'}
    end
    
    it &quot;should not log the user in if no user was found&quot; do
      User.should_receive(:login).with(params[:username], params[:password]).and_return(false)
      post :login, params
      session[:user].should be_nil
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

What we're doing is is firstly, stubbing out the login method on the user model (this could be anything you like, just make sure you stub out the correct method), but ensuring it receives the correct parameters and forcing it to return false. This in turn will unless user.blank? check fail, thereby not setting any session variable, which we check for with session[:user].should be_nil.

Now, if we're going to look at our coverage, you'll note that we've got some code that never gets executed with this test, so we'll have to write another. Let's extend this test with another one and make sure that code gets executed:

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;it &quot;should log the user in if the details are correct&quot; do
  User.should_receive(:login).with(params[:username], params[:password]).and_return(1)
  post :login, params
  session[:user].should eql(1)
end
&lt;/code&gt;&lt;/pre&gt;

As you can see here, we've changed the stub of the login method a little differently, ensuring that it responds with a numeric value, the user id. We then assert that the user key for the session array is equal to 1, and our test passes! Yay!

This is a fairly basic login method, so let's update this a little further with something a little more complex. First, let's update our test for the successful login:

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;it &quot;should log the user in if the details are correct&quot; do
  User.should_receive(:login).with(params[:username], params[:password]).and_return(1)
  post :login, params
  session[:user].should eql(1)
  response.should redirect_to(:controller =&gt; 'home', :action =&gt; 'index')
end
&lt;/code&gt;&lt;/pre&gt;

You can see here that we're now checking for some redirect functionality, which doesn't exist in our code - meaning our test will fail! So now we can go and update our code to ensure that the intended functionality is in place, and watch our test pass:

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class UsersController &amp;lt; ApplicationController
  def login( username, password )
    user = User.login( username, password )
    unless user.blank?
      session[:user] = user.id
      redirect_to( { :controller =&gt; 'home', :action =&gt; 'index' } )
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

And there you have it! Test-driven development at it's very primitive finest! We've written some basic login code, and written tests to ensure that all cases against that method are working as intended, whilst isolating our tests to just that method. In the next article, we're going to look at how we can test our views to ensure the expected content is being rendered to the browser.
          </content>  </entry>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2008-10-28:6</id>
    <published>2008-10-28T23:20:00Z</published>
    <updated>2009-05-12T05:38:42Z</updated>
    <category term="Blog"/>
    <category term="open"/>
    <category term="rails"/>
    <category term="ruby"/>
    <category term="source"/>
    <link href="http://www.kirkbushell.com/articles/teachmate-org-now-opensource" rel="alternate" type="text/html"/>
    <title>Teachmate.org, Now OpenSource</title>
<content type="html">
            Teachmate.org has open up it's doors and made the website's code opensource. Teachmate is about offering your knowledge to become and online teacher and/or to find people who would be willing to train you in a particular field, check it out: &lt;a href=&quot;http://www.teachmate.org&quot;&gt;Teachmate&lt;/a&gt;. As far as I'm aware, this is one of the few actual websites where they have made their code opensource - this can only be a good thing for the web community!

I found this through the &lt;a href=&quot;http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/5f9055a114be12e8?hl=en&quot;&gt;ruby on rails google group&lt;/a&gt;.
          </content>  </entry>
  <entry xml:base="http://www.kirkbushell.com/">
    <author>
      <name>Kirk Bushell</name>
    </author>
    <id>tag:www.kirkbushell.com,2008-10-27:3</id>
    <published>2008-10-27T23:33:00Z</published>
    <updated>2009-05-12T05:38:53Z</updated>
    <category term="Blog"/>
    <category term="rails"/>
    <category term="rspec"/>
    <category term="ruby"/>
    <category term="testing"/>
    <category term="unit"/>
    <link href="http://www.kirkbushell.com/articles/rspec-model-tests" rel="alternate" type="text/html"/>
    <title>Testing using RSpec, Part 1 - Models</title>
<summary type="html">This is part of a 4-part series which will go over the various tests you can write for your models, controllers, views and acceptance using &lt;a href=&quot;http://rspec.info/&quot;&gt;RSpec&lt;/a&gt;. My first article will begin with our application models and testing with and without database connectivity.</summary><content type="html">
            This is part of a 4-part series which will go over the various tests you can write for your models, controllers, views and acceptance using &lt;a href=&quot;http://rspec.info/&quot;&gt;RSpec&lt;/a&gt;. My first article will begin with our application models and testing with and without database connectivity.
Models for me have always been a bit of an iffy area of testing. Why? I've had many discussions, both online and with my fellow colleagues about whether or not models should be mocked out. I've heard good points in both directions, but I think it's better to test the models against the database (if required), to pick up early mistakes in your code. Leaving testing the models against the database in acceptance can take longer, and also means possible bugs may not be picked up &lt;b&gt;until&lt;/b&gt; you write your acceptance tests. In some development environments, this simply isn't possible. However, for this article we're going to cover a single model with two different forms of testing: with and without database connectivity. Consider the model below:

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
class User &amp;lt; ActiveRecord::Base
  def self.get_users_with_no_referrals
    User.find(:all, :conditions =&gt; {:referral_count =&gt; 0})
  end
end
&lt;/code&gt;&lt;/pre&gt;

Let's now test this, with database connectivity.

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
describe User do
  before :each do
    @user1 = User.create!( { :username =&gt; 'test', :referrals_count =&gt; 0 } )
    @user2 = User.create!( { :username =&gt; 'another test', :referrals_count =&gt; 1 } )
  end
  
  it &quot;should return all users with no referrals&quot; do
    users = User.get_users_with_no_referrals
    users.length.should == 1
  end
end
&lt;/code&gt;&lt;/pre&gt;

Let's look at this same example, but we'll mock out the database calls so as to avoid any talking to our database.

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
require 'ostruct'

describe User do
  before :each do
    @returned_users = [ OpenStruct.new( { :username =&gt; 'test', :referrals_count =&gt; 1 } ) ]
    User.stub!(:get_users_with_no_referrals).and_return( @returned_users )
  end
  
  it &quot;should return all users with no referrals&quot; do
    users = User.get_users_with_no_referrals
    users.length.should == 1
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Have a look closely here - I've purposely made a code mistake in order to demonstrate why I think it's better to test against the database with our models. Have a look at the @returned_users line - you'll notice that the referrals_count is set to 1. Now, due to the fact that we're actually mocking this out, our test will pass - and we'll be none the wiser. Had we checked this against the database, our test would fail - as no data would be in the database that would match our request.&lt;/p&gt;

&lt;p&gt;There are advantages and disadvantages to doing both ways and I think more than anything it comes to developer discretion as to which method should best be used - some developers (like myself) simply prefer one way or the other - neither is particularly  right or wrong, but one way does enforce &quot;proper&quot; unit testing, whereas the other enforces (in my opinion) a better test.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Update: Thanks to Alexandre for spotting this. Be sure to require 'ostruct' when using OpenStruct. I tend to use this heavily in all my projects, so forgot to include it in the article.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;In my next article, I look at using RSpec for testing our controllers. &lt;a href=&quot;http://www.kirkbushell.com/articles/testing-using-rspec-part-2-the-controller&quot;&gt;Testing using RSpec, Part 2 - the Controller&lt;/a&gt;&lt;/p&gt;
          </content>  </entry>
</feed>
