<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>The Chris O Show - Home</title>
  <id>tag:www.thechrisoshow.com,2008:mephisto/</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://www.thechrisoshow.com/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://www.thechrisoshow.com/" rel="alternate" type="text/html"/>
  <updated>2008-05-07T08:51:59Z</updated>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-05-07:67</id>
    <published>2008-05-07T08:51:00Z</published>
    <updated>2008-05-07T08:51:59Z</updated>
    <link href="http://www.thechrisoshow.com/2008/5/7/display-your-kml-on-a-google-map-now-using-the-google-app-engine" rel="alternate" type="text/html"/>
    <title>Display your KML on a Google Map (again) - now using Google App Engine</title>
<content type="html">
            &lt;p&gt;&lt;a href='http://en.wikipedia.org/wiki/Keyhole_Markup_Language'&gt;&lt;span class='caps'&gt;KML&lt;/span&gt;&lt;/a&gt; stands for Keyhole Markup Language and is used to create &lt;em&gt;geographically aware&lt;/em&gt; information.&lt;/p&gt;


	&lt;p&gt;It can be tricky to write your own &lt;span class='caps'&gt;KML&lt;/span&gt; by hand- in order to display kml on a Google Map you need to save the markup into a file on a web server that Google has access to.  Google then caches this file in it&#8217;s own mega-web, so it can take a while for any changes you make to filter through.&lt;/p&gt;


	&lt;p&gt;My little application fixes these issues by allowing you to paste the kml into a web form, hit a button, and &lt;span class='caps'&gt;WHAMMO&lt;/span&gt;, KML on a Google Map without any of the stupid stuff.&lt;/p&gt;


	&lt;p&gt;So, almost a year after creating this application with Ruby on Rails I&#8217;ve decided to port this little app to use &lt;a href='http://code.google.com/appengine/'&gt;Google App Engine&lt;/a&gt; &#8211; you shouldn&#8217;t notice any difference, but rest assured that it can now scale to support gabillions of visits.&lt;/p&gt;


	&lt;p&gt;&lt;em&gt;Because everyone&#8217;s gonna wanna use it&lt;/em&gt;&lt;/p&gt;


To try it out by going &lt;a href='http://www.thechrisoshow.com/display_kml'&gt;here&lt;/a&gt; and copy and paste the following &lt;span class='caps'&gt;KML&lt;/span&gt; into the box and click the button.
&lt;pre&gt;
&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;
&amp;lt;kml xmlns=&quot;http://earth.google.com/kml/2.2&quot;&amp;gt;
  &amp;lt;Placemark&amp;gt;
    &amp;lt;name&amp;gt;Invercargill Rocks!&amp;lt;/name&amp;gt;
    &amp;lt;description&amp;gt;Go there and see!&amp;lt;/description&amp;gt;
    &amp;lt;Point&amp;gt;
      &amp;lt;coordinates&amp;gt;168.34693908691406,-46.416322245553296,5&amp;lt;/coordinates&amp;gt;
    &amp;lt;/Point&amp;gt;
  &amp;lt;/Placemark&amp;gt;
&amp;lt;/kml&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;Display &lt;span class='caps'&gt;KML&lt;/span&gt; on a Google Map: &lt;a href='http://www.thechrisoshow.com/display_kml'&gt;http://www.thechrisoshow.com/display_kml&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;a href='http://code.google.com/apis/kml/documentation/kml_tut.html'&gt;&lt;span class='caps'&gt;KML&lt;/span&gt; Samples&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;a href='http://code.google.com/apis/kml/documentation/kml_21tutorial.html'&gt;&lt;span class='caps'&gt;KML&lt;/span&gt; Tutorial&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;a href='http://code.google.com/apis/kml/documentation/kmlreference.html'&gt;&lt;span class='caps'&gt;KML&lt;/span&gt; Reference&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;PS: Because I rewrote the code in Python it might&#8217;ve broken a few things &#8211; let me know in the comments if it doesn&#8217;t work for you.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-05-04:59</id>
    <published>2008-05-04T22:51:00Z</published>
    <updated>2008-05-04T22:52:16Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/5/4/five-tips-for-testing-rails" rel="alternate" type="text/html"/>
    <title>Five tips for testing Rails</title>
<content type="html">
            &lt;h3&gt;Tip One &#8211; Debugging tests&lt;/h3&gt;


	&lt;p&gt;I use RDebug in my apps a lot, but frequently I found myself wanting to debug my tests.  Here&#8217;s how you can do it:&lt;/p&gt;


If you haven&#8217;t done so already, install RDebug
&lt;pre&gt;
  $ gem install ruby-debug
&lt;/pre&gt;

Insert &lt;i&gt;debugger&lt;/i&gt; in your code where you&#8217;d like to breakpoint
&lt;pre class='ruby'&gt;
    def like_bananas?
      # Want to breakpoint here and see what the self.fruit attribute looks like
      debugger
      return self.fruit.likes.include?('bananas')
    end      
&lt;/pre&gt;
And then run the test from the command line using rdebug like this:
&lt;pre&gt;
    rdebug test/unit/bananas_test.rb
&lt;/pre&gt;

	&lt;p&gt;You could even put the &lt;i&gt;debugger&lt;/i&gt; reference in your test code as well.&lt;/p&gt;


	&lt;h3&gt;Tip Two &#8211; Gently ease yourself into &lt;span class='caps'&gt;BDD&lt;/span&gt; with shoulda&lt;/h3&gt;


	&lt;p&gt;Everybody&#8217;s talking about Behaviour Driven Development these days &#8211; and the popular &lt;span class='caps'&gt;BDD&lt;/span&gt; tool for Rails is the mighty RSpec.&lt;/p&gt;


	&lt;p&gt;But, it can seem like quite a bit of a jump to get into RSpec.  Maybe you&#8217;ve got a well established application with a plethora of Test::Unit tests that you can&#8217;t bear to look at.  Or maybe you just wanna try out the whole &lt;span class='caps'&gt;BDD&lt;/span&gt; approach for a while and see how it goes without really committing.&lt;/p&gt;


	&lt;p&gt;Well never fear &#8211; there&#8217;s a really easy way of easing yourself into &lt;span class='caps'&gt;BDD&lt;/span&gt; and it&#8217;s called &lt;a href='http://thoughtbot.com/projects/shoulda'&gt;shoulda&lt;/a&gt;.  This is a great &lt;span class='caps'&gt;BDD&lt;/span&gt; tool made by the crazy freakin&#8217; geniuses (genii?) over at &lt;a href='http://thoughtbot.com/'&gt;thoughtbot&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Basically, shoulda is a &lt;span class='caps'&gt;BDD&lt;/span&gt; framework built &lt;i&gt;on top of&lt;/i&gt; Test::Unit.  It uses all the Test::Unit assertions that we know and love &#8211; adds some exotic delicous syntactic &lt;span class='caps'&gt;BDD&lt;/span&gt; sugar &#8211; and throws some wonderful helper macros in as freebies..&lt;/p&gt;


	&lt;p&gt;The thoughtbot folks rabbit on about it over here: &lt;a href='http://thoughtbot.com/projects/shoulda'&gt;http://thoughtbot.com/projects/shoulda&lt;/a&gt;.&lt;/p&gt;


An example test unit class might look like this:
&lt;pre class='ruby'&gt;
class FruitBowlTests &amp;lt; Test::Unit::TestCase

  def setup
    @fruit_bowl = get_fruit_bowl
  end

  # Check out this BDD coolness
  should &quot;be best before date&quot; do
    assert @fruit_bowl.fresh?
  end

  # But you can still include old skool tests like this
  def test_fruit_bowl_has_three_fruit
    assert_equal 3, @fruit_bowl.fruit.size
  end

end
&lt;/pre&gt;

	&lt;p&gt;So you can try out &lt;span class='caps'&gt;BDD&lt;/span&gt; without messing up your current tests &#8211; hooray!  Shoulda actually provides way more than this though &#8211; but don&#8217;t take my word for it, gem install shoulda today!&lt;/p&gt;


PS: Not only that, but the shoulda guys include a script that modifies your Test::Unit tests and changes them into shoulda style tests.  Run it like this:
&lt;pre&gt;
 $ ./vendor/plugins/shoulda/bin/convert_to_should_syntax [path to test file]
&lt;/pre&gt;

	&lt;h3&gt;Tip Three &#8211; &lt;span class='caps'&gt;DRY&lt;/span&gt; up your tests with define method&lt;/h3&gt;


Ever find yourself writing the same tests over and over again but with slightly different modifications &#8211; like this:
&lt;pre class='ruby'&gt;
  def test_banana_should_be_nil_if_fruit_bowl_rotten
    @fruit_bowl = FruitBowl.new(:rotten =&amp;gt; true)
    assert_nil @fruit_bowl.banana
  end

  def test_apple_should_be_nil_if_fruit_bowl_rotten
    @fruit_bowl = FruitBowl.new(:rotten =&amp;gt; true)
    assert_nil @fruit_bowl.apple
  end

  def test_pear_should_be_nil_if_fruit_bowl_rotten
    @fruit_bowl = FruitBowl.new(:rotten =&amp;gt; true)
    assert_nil @fruit_bowl.pear
  end
&lt;/pre&gt;

	&lt;p&gt;Using the wonderful Ruby methods &lt;i&gt;define_method&lt;/i&gt; and &lt;i&gt;send&lt;/i&gt; you can dry this up really easily like thus:&lt;/p&gt;


&lt;pre class='ruby'&gt;
# Put the different bits into an array
[:banana, :apple, :pear].each do |fruit|
  # Iterate over the array and create a test method for each object.  Remember to prefix the method with 'test' when using Test::Unit
    define_method &quot;test_#{fruit}_should_be_nil_if_fruit_bowl_rotten&quot; do
      @fruit_bowl = FruitBowl.new(:rotten =&amp;gt; true)
      # And use the send method to check the correct attribute
         assert_nil @fruit_bowl.send(fruit)
    end
  end

&lt;/pre&gt; 

	&lt;h3&gt;Tip Four &#8211; Test the objects behaviour&lt;/h3&gt;


	&lt;p&gt;Here&#8217;s a tip that should revolutionise your testing methodology forever&#8230;&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;Make sure you test the correct behaviour for an object&lt;/b&gt;&lt;/p&gt;


	&lt;p&gt;What this means is that when testing an object, don&#8217;t create a test that looks outside of its scope.&lt;/p&gt;


	&lt;p&gt;For example:&lt;/p&gt;


	&lt;p&gt;Up until very recently I used to be a big fixtures man.  It wasn&#8217;t uncommon for me to write functional tests like this:&lt;/p&gt;


&lt;pre class='ruby'&gt;
    def test_transfers_heading_should_be_on_page
      get :show, :id =&amp;gt; festivals(:festival_with_transfers_heading)    
      assert_select 'h3#heading', &quot;Transfers&quot; 
    end
&lt;/pre&gt;

	&lt;p&gt;Which was to test that the h3 with the id of &#8216;heading&#8217; displayed the correct text, in this case &#8220;transfers&#8221;.&lt;/p&gt;


	&lt;p&gt;But deep down inside I was relying on this test to do more than what it should be doing.  Whenever this test was run it had to load the festival from the database, and show the correct template.  However, the scope of the test though was just to test that the correct information was shown in the view, anything else was irrelevant to this test.&lt;/p&gt;


A better test could look like this:
&lt;pre class='ruby'&gt;
    def test_transfers_heading_should_be_on_page
      festival = Festival.new(:heading =&amp;gt; 'Transfers')
      Festival.stubs(:find).returns(festival)
      get :show, :id =&amp;gt; 1
      assert_select 'h3#heading', &quot;Transfers&quot; 
    end
&lt;/pre&gt;

	&lt;p&gt;The database doesn&#8217;t get hit because it doesn&#8217;t need to get hit.  All we&#8217;re testing here is that the correct heading is displayed on the page &#8211; that the controller fulfilled its obligation by marshalling the data from the model to the view.&lt;/p&gt;


	&lt;h3&gt;Tip Five &#8211; Expectations are tests too you know!&lt;/h3&gt;


	&lt;p&gt;Mocks Rock &#8211; there I said it!&lt;/p&gt;


	&lt;p&gt;But, you can get so obsessed with stubbing out all manner of stuff for your assertions that you can mess out on all the nice inbuilt assertions built into mock objects &#8211; expectations.&lt;/p&gt;


For example,  you might have an ActiveRecord observer that does some stuff after a model is saved like this:
&lt;pre class='ruby'&gt;
  class FruitObserver &amp;lt; Observer

   def after_create(fruit)
     # Add to all the inventory lists
     InventoryList.find(:all).each do |i|
       i.stock.push fruit
     end    
   end
  end
&lt;/pre&gt;

You could use a mocha expectation to test this without using a single assert_blah like this:
&lt;pre class='ruby'&gt;
  def test_when_creating_banana_observer_is_called
    banana = Fruit.new(:name =&amp;gt; &quot;banana&quot;)
    InventoryList.any_instance.expects(:push).with(banana)
    banana.save
  end
&lt;/pre&gt;

	&lt;p&gt;You don&#8217;t need to have assert_blah in all your tests &#8211; this is a perfectly acceptable test because it includes the expectation     InventoryList.any_instance.expects(:push).with(banana).&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-04-18:65</id>
    <published>2008-04-18T14:51:00Z</published>
    <updated>2008-04-18T14:52:08Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/4/18/bug-in-mephisto-when-adding-comments" rel="alternate" type="text/html"/>
    <title>Is your Mephisto Blog not accepting new comments?</title>
<content type="html">
            &lt;p&gt;This blog uses &lt;a href='http://mephistoblog.com'&gt;Mephisto&lt;/a&gt; &#8211; which is pretty cool and works a treat &#8211;  but I realised to my horror the other day that the commenting system appeared to have stopped working.&lt;/p&gt;


	&lt;p&gt;It drove me bananas!  Why would it work one day, but not the next!  Why would it work on my local box but not on production?!?&lt;/p&gt;


	&lt;p&gt;Turns out there&#8217;s a known bug with Mephisto when used with Apache that prevents additional comments from being added to an article that already has a comment.&lt;/p&gt;


	&lt;p&gt;You can fix this by adding the following line to the bottom of your .htaccess file:&lt;/p&gt;


&lt;pre&gt;
  DirectorySlash Off 
&lt;/pre&gt;

	&lt;p&gt;And voila &#8211; comments will start working again.&lt;/p&gt;


	&lt;p&gt;Thanks to &lt;a href='http://blog.gilluminate.com/2007/2/12/mephisto-comments-breaking-in-apache/'&gt;Jason Gill&lt;/a&gt; for this tip.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-04-13:56</id>
    <published>2008-04-13T18:32:00Z</published>
    <updated>2008-04-17T23:09:00Z</updated>
    <link href="http://www.thechrisoshow.com/2008/4/13/five-rails-tips" rel="alternate" type="text/html"/>
    <title>Five tips for contributing to Rails</title>
<content type="html">
            &lt;div class='note'&gt;
Note: these tips assume a basic understanding of how to contribute to Rails.  

If you&#8217;ve never contributed before I heartily recommend you check out Josh Sussers presentation &lt;a href='http://blog.hasmanythrough.com/2007/5/24/laying-tracks-slides'&gt;Laying tracks&lt;/a&gt; or Ryan Bates excellent &lt;a href='http://railscasts.com/episodes/50'&gt;railscast&lt;/a&gt; on the topic.
&lt;/div&gt;

	&lt;h3&gt;Tip One &#8211; Test ActiveRecord tests with different database types&lt;/h3&gt;


	&lt;p&gt;When creating a patch for ActiveRecord you need to make sure your patch works with different databases.  I learned this the hard way when I submitted a patch that worked perfectly in mysql and broke in every other database type.&lt;/p&gt;


If you have the database management system set up correctly and also added the required databases (activerecord_unittest and activerecord_unittest2) then you can run the following rake task in the rails/activerecord folder:
&lt;pre&gt;
  $ rake test_[database type]
&lt;/pre&gt;
Where [database type] is the database system you&#8217;re looking to run the tests on.

For example:
&lt;pre&gt;
  $ rake test_mysql
  $ rake test_sqlite
  $ rake test_postgresql
&lt;/pre&gt;
You can also use this rake task to run a specific ActiveRecord test.

For example, to run the base_test.rb tests using mysql:
&lt;pre&gt;
  $ rake test_mysql TEST=test/cases/base_test.rb
&lt;/pre&gt;

	&lt;h3&gt;Tip Two &#8211; Run ActionPack tests in TextMate&lt;/h3&gt;


	&lt;p&gt;When I&#8217;m working on a Rails application I get pretty addicted to running tests in TextMate with Command-R.  I found when working on Rails patches that it wasn&#8217;t quite so tidy.&lt;/p&gt;


	&lt;p&gt;&lt;img src='http://www.thechrisoshow.com/assets/2008/4/17/addresses_render_tes-2.jpg' alt='' /&gt;&lt;/p&gt;


	&lt;p&gt;ActionPack tests typically have require &#8216;abstract_unit&#8217; at the beginning of the file.&lt;/p&gt;


At the top of the test file add:
&lt;pre class='ruby'&gt;
$: &amp;lt;&amp;lt; &quot;..&quot; 
&lt;/pre&gt;

	&lt;p&gt;An explanation of what these strange hieroglyphs mean:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;$:&lt;/strong&gt; is the search path (you can think of it as being an array of directory path names)&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;&#8221;..&#8221;&lt;/strong&gt; the parent folder &#8211; you could have &#8217;../tests/blahblah&#8217; etc if you wanted to&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;&amp;lt;&amp;lt;&lt;/strong&gt; pushes an element onto an array&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;And now it finds &#8216;abstract_unit&#8217; in the parent folder, allowing the tests to run within TextMate &#8211; hooray!
&lt;img src='http://www.thechrisoshow.com/assets/2008/4/17/addresses_render_tes-1.jpg' alt='' /&gt;&lt;/p&gt;


Obviously, if abstract_unit isn&#8217;t in the parent folder, then you should adjust this to search in the right folder for abstract_unit.  For example:
&lt;pre class='ruby'&gt;
$: &amp;lt;&amp;lt; &quot;../tests/helpers&quot; 
&lt;/pre&gt;

&lt;div class='note'&gt;
Note: Make sure you remove the extra line before creating your patch
&lt;/div&gt;

	&lt;h3&gt;Tip Three &#8211; ActiveRecord script/console&lt;/h3&gt;


	&lt;p&gt;Anyone who&#8217;s spent anytime working on an ActiveRecord Rails patch will realise that the included test models can get pretty complicated.  That&#8217;s why I like to create a special &lt;span class='caps'&gt;IRB&lt;/span&gt; console for playing around with the relationships.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Download this file: &lt;a href='http://www.thechrisoshow.com/assets/2008/4/13/script_console_for_rails.zip'&gt;script_console_for_rails.zip&lt;/a&gt; and unzip it into your rails/activerecord/ folder.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;
	&lt;li&gt;Still in the activerecord folder run:&lt;/li&gt;
	&lt;/ul&gt;


&lt;pre&gt;
    $ script/console
&lt;/pre&gt;

	&lt;ul&gt;
	&lt;li&gt;This opens a special &lt;span class='caps'&gt;IRB&lt;/span&gt; console that includes all the Rails test models and fixtures &#8211; allowing for cool stuff like this:&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;&lt;img src='http://www.thechrisoshow.com/assets/2008/4/13/Terminal_—_ruby_—_80×24.jpg' alt='' /&gt;&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;You can open it with different database types like this:&lt;/li&gt;
	&lt;/ul&gt;


&lt;pre&gt;
    $ script/console sqlite3
    $ script/console postgresql
    etc
&lt;/pre&gt;

	&lt;h3&gt;Tip Four &#8211; Go through old unloved patches and fix them up&lt;/h3&gt;


	&lt;p&gt;Stuck for ideas on how you can contribute to Rails?  One great way is by going through old Rails patches in Trac and bring them up to speed.&lt;/p&gt;


Some patches work fine, but haven&#8217;t been accepted for a number of reasons:
	&lt;ul&gt;
	&lt;li&gt;They need documentation&lt;/li&gt;
		&lt;li&gt;They need tests&lt;/li&gt;
		&lt;li&gt;The code that the patch applies to has been changed&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;This &lt;a href='http://dev.rubyonrails.org/report/11'&gt;link&lt;/a&gt; provides a list of patches that are untested, undocumented and incomplete.&lt;/p&gt;


	&lt;p&gt;Or you could just browse through the old open bugs &lt;a href='http://dev.rubyonrails.org/report/1'&gt;here&lt;/a&gt; for patches that work but need some attention.&lt;/p&gt;


	&lt;h3&gt;Tip Five &#8211; Validate other peoples patches&lt;/h3&gt;


	&lt;p&gt;Last year, the Rails Core team made a decision that patches need to be verified by three other contributors before being considered for acceptance.  With this arrangement, the Core team now no longer have to waste their time examining half-finished patches, and the quality of patches are higher.&lt;/p&gt;


	&lt;p&gt;Verifying a patch is more than just agreeing with the idea, it requires a bit of effort.&lt;/p&gt;


Here’s the steps you need to take to verify a patch:
	&lt;ul&gt;
	&lt;li&gt;Find a patch that needs verifying.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;This bit&#8217;s easy.  Just waltz on into #rails-contrib on freenode and ask &#8220;anyone want me to +1 their patch?&#8221; and prepare to be bombarded with requests.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Agree with what the patch does&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;This may seem obvious, but there&#8217;s no need to verify a patch if you don&#8217;t believe that the patch belongs in Rails.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Look at the source code&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Give the code an eyeball in trac before downloading it.  Make sure that the patch includes appropriate tests and documentation, and the code doesn&#8217;t have any glaring peculiarities.  If it&#8217;s lacking in one of these areas post a comment on the ticket so that the contributor can make the necessary fixes.&lt;/p&gt;


	&lt;p&gt;If however you&#8217;re happy with how the patch looks then it&#8217;s time to take it for a test drive.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Update your own Rails repository&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;This is so that when you install the patch you can be sure that it patches cleanly.  If it doesn&#8217;t then post a comment on the trac ticket letting the contributor know so they can fix it.&lt;/p&gt;


If you&#8217;re using subversion then run
&lt;pre&gt;
    svn update
&lt;/pre&gt;

	&lt;ul&gt;
	&lt;li&gt;Download the patch&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Sometimes trac tickets have multiple patch files, so make sure you&#8217;ve got the right one(s).&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Install the patch&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Now install the patch on your own repository using a command like:&lt;/p&gt;


&lt;pre&gt;
    $ patch -p0 -i [patch_file.diff]
&lt;/pre&gt;
(Replace [patch_file.diff] with the file path of the patch you want to apply.)

If the patch doesn&#8217;t apply cleanly you&#8217;ll get a message like:
&lt;pre&gt;
    Hunk #2 FAILED at 738.
&lt;/pre&gt;
This is a sign that the code base has changed since the patch was created.  The contributor will need to update the patch.  Make sure you post a comment on the ticket with the details.

	&lt;ul&gt;
	&lt;li&gt;Run the tests to make sure they all pass&lt;/li&gt;
	&lt;/ul&gt;


&lt;pre&gt;
    $ rake test
&lt;/pre&gt;

	&lt;ul&gt;
	&lt;li&gt;Try out the patch with a test Rails application.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;I like to keep my Rails repository in the vendor folder of a dummy application that for testing patches.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Verify&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;If the patch works, and there are no obvious side effects, then you can +1 the trac ticket.  Simply post a comment on the ticket with something along the lines of:&lt;/p&gt;


&lt;pre&gt;
    +1 tests pass
&lt;/pre&gt;

	&lt;p&gt;If you find that you&#8217;re the third person to verify a patch, then add the &lt;strong&gt;verified&lt;/strong&gt; keyword.  This signals to the core team that this patch is ready for examination.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Revert the patch&lt;/li&gt;
	&lt;/ul&gt;


Run something like
&lt;pre&gt;
    svn revert . -R
&lt;/pre&gt;

	&lt;p&gt;To ensure that the patch is no longer applied to your repository.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-04-11:57</id>
    <published>2008-04-11T09:13:00Z</published>
    <updated>2008-04-11T09:13:34Z</updated>
    <link href="http://www.thechrisoshow.com/2008/4/11/having-an-issue-installing-git-using-macports" rel="alternate" type="text/html"/>
    <title>Wee issue trying to install git using MacPorts</title>
<content type="html">
            &lt;p&gt;The other day I wanted to install the hot new &lt;span class='caps'&gt;SCM&lt;/span&gt; git &#8211; and an easy way to do this is using MacPorts.&lt;/p&gt;


So  I opened up the terminal and banged out:
&lt;pre&gt;&lt;code&gt;
sudo port install git-core
&lt;/code&gt;&lt;/pre&gt;

It chugged away installing nicely until it tried to install rsync as a dependency:
&lt;pre&gt;&lt;code&gt;
&amp;lt;snip&amp;gt;
---&amp;gt;  Attempting to fetch rsync-2.6.9.tar.gz from http://svn.macports.org/repository/macports/downloads/rsync
Error: Target org.macports.fetch returned: fetch failed
Error: Status 1 encountered during processing.
&lt;/code&gt;&lt;/pre&gt;

I fixed this by running the selfupdate command for MacPorts
&lt;pre&gt;&lt;code&gt;
sudo port selfupdate
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;After that the git installation worked fine.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-03-21:52</id>
    <published>2008-03-21T09:36:00Z</published>
    <updated>2008-03-21T17:46:06Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/3/21/where-s-my-blasted-gems" rel="alternate" type="text/html"/>
    <title>Where's my blasted gems?</title>
<content type="html">
            If you&#8217;re anything like me, and you keep forgetting whereabouts your gems are installed, use the following command:
&lt;pre&gt;&lt;code&gt;
gem environment
&lt;/code&gt;&lt;/pre&gt;

This will pop up a handy list like this:
&lt;pre&gt;&lt;code&gt;
RubyGems Environment:
  - VERSION: 0.9.4 (0.9.4)
  - INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.8
  - GEM PATH:
     - /usr/local/lib/ruby/gems/1.8
  - REMOTE SOURCES:
     - http://gems.rubyforge.org
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Your gems are nicely tucked away within your &lt;span class='caps'&gt;GEM PATH&lt;/span&gt; directory&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-02-13:49</id>
    <published>2008-02-13T15:11:00Z</published>
    <updated>2008-02-13T15:12:41Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/2/13/yajb-excel-and-jlambda" rel="alternate" type="text/html"/>
    <title>Add a picture to an excel file using YAJB and railsxls</title>
<content type="html">
            &lt;p&gt;&lt;img src='http://www.thechrisoshow.com/assets/2008/2/13/excel_export.jpg' alt='' /&gt;&lt;/p&gt;


	&lt;p&gt;Ever find yourself wanting to generate an excel spreadsheet that includes logos or images?&lt;/p&gt;


	&lt;p&gt;If you want to generate xslx spreadsheets it&#8217;s not too bad, because you can embed the image binary in the markup &#8211; but for older excel formats it can get a little trickier.&lt;/p&gt;


	&lt;p&gt;Enter &lt;a href='https://rubyforge.org/projects/railsxls/'&gt;railsxls&lt;/a&gt; &#8211; an excel plugin for Rails!&lt;/p&gt;


	&lt;p&gt;&lt;a href='https://rubyforge.org/projects/railsxls/'&gt;railsxls&lt;/a&gt; is basically a wrapper around the funky &lt;a href='http://poi.apache.org/'&gt;&lt;span class='caps'&gt;POI&lt;/span&gt;&lt;/a&gt; library, so anything you can do with &lt;a href='http://poi.apache.org/'&gt;&lt;span class='caps'&gt;POI&lt;/span&gt;&lt;/a&gt; you can do with &lt;a href='https://rubyforge.org/projects/railsxls/'&gt;railsxls&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;&#8220;But Chris, how can a Rails plugin use a Java library?!?&#8221;&lt;/p&gt;


	&lt;p&gt;Well I&#8217;m glad you asked &#8211; by using Yet Another Java Branch (or &lt;a href='http://www.cmt.phys.kyushu-u.ac.jp/~M.Sakurai/cgi-bin/fw/wiki.cgi?page=YAJB'&gt;&lt;span class='caps'&gt;YAJB&lt;/span&gt;&lt;/a&gt; for short)  This exciting little gem let&#8217;s you calls out to Java libraries using Ruby code.&lt;/p&gt;


	&lt;p&gt;&lt;em&gt;Niiiice.&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;&#8220;Ok, so I can use the &lt;span class='caps'&gt;POI&lt;/span&gt; library commands to insert pictures onto an excel spreadsheet?&#8221;&lt;/p&gt;


	&lt;p&gt;Well&#8230;kinda&#8230;here&#8217;s how!&lt;/p&gt;


	&lt;p&gt;First install railsxls and yajb (details &lt;a href='http://wiki.rubyonrails.org/rails/pages/Rails+XLS+Plugin'&gt;here&lt;/a&gt;)&lt;/p&gt;


	&lt;p&gt;The &lt;span class='caps'&gt;POI&lt;/span&gt; code that handles addPictures uses byte arrays, which &lt;span class='caps'&gt;YAJB&lt;/span&gt; has a hard time transporting between Java and Ruby.  (Well, it did for me anyway)&lt;/p&gt;


So instead you have to use a jlambda, which let&#8217;s you run chunks of Java using ruby&#8230;like this:
&lt;pre class='ruby'&gt;
  # Here's the java code I want to run
  java_code = &quot;System.out.println(message);&quot; 

  # And here's the jlambda that sets it up
  # Note the first param &quot;String message&quot; which let's you determine what param to pass in
  lambda_to_run = jlambda(&quot;String message&quot;, java_code)

  # Now we're actually CALLING the code and passing in a string
  lambda_to_run.call &quot;bananas!&quot; 
&lt;/pre&gt; 

	&lt;p&gt;So, that&#8217;s a jlambda!&lt;/p&gt;


Now, with this new found knowledge, let&#8217;s call the appropriate &lt;span class='caps'&gt;POI&lt;/span&gt; code to create a picture.  (I put the java code in the here_doc)
&lt;pre class='ruby'&gt;

  # First off, let's use YAJB calls to import some java libraries
  jimport &quot;java.io.*&quot; 
  jimport &quot;org.apache.poi.hssf.usermodel.*&quot; 

  # Now create the new HSSFWorkbook option
  workbook = jnew :HSSFWorkbook

  java_code =&amp;lt;&amp;lt;JAVA
  java.io.File file = new java.io.File(&quot;#{File.join(RAILS_ROOT, 'tmp',&quot;rails.png&quot;)}&quot; );
  java.io.FileInputStream fis = new java.io.FileInputStream(file);

  byte[] bytes = new byte[(int) file.length()];
  fis.read(bytes);
  return new Integer(workbook.addPicture(bytes, org.apache.poi.hssf.usermodel.HSSFPicture.PICTURE_TYPE_PNG));
  JAVA

  # Create a worksheet in the workbook - an important step!
  sheet = workbook.createSheet(&quot;new sheet&quot;) 
  row = sheet.createRow(0)

  # This patriarch business is used for writing images and autoshapes
  patriarch = sheet.createDrawingPatriarch

  # Anchors are used for positioning
  anchor = jnew :HSSFClientAnchor, 0,0,0,255,2,2,4,7;
  anchor.setAnchorType( 2 );

  # Work your magic jlambda!  (Pass in the workbook as a parameter)
  p2 = jlambda(&quot;org.apache.poi.hssf.usermodel.HSSFWorkbook workbook&quot;, java_code)
  image_index = p2.call workbook

  # An image index is returned and this is used by the createPicture function
  patriarch.createPicture anchor, image_index
&lt;/pre&gt;

The code doesn&#8217;t cover saving the workbook, but you could do something like this:
&lt;pre class='ruby'&gt;
  temp = Tempfile.new('railsworksheet-', File.join(RAILS_ROOT, 'tmp') )
  out = jnew :FileOutputStream, temp.path
  workbook.write(out)
  out.close
  File.open(temp.path, 'rb') { |file| file.read }
&lt;/pre&gt;

	&lt;p&gt;Now go erm&#8230;insert images!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-01-14:48</id>
    <published>2008-01-14T11:25:00Z</published>
    <updated>2008-01-14T11:26:06Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/1/14/assert_select-the-order-of-a-drop-down-in-ruby-on-rails" rel="alternate" type="text/html"/>
    <title>assert_select the order of a drop-down in Ruby on Rails</title>
<content type="html">
            &lt;p&gt;To write a functional test to determine the correct order of a dropdown do the following:&lt;/p&gt;


&lt;pre class='ruby'&gt;
    option = css_select(&quot;select#id_of_dropdown option&quot;)[n]
    assert_equal &quot;Value To Compare&quot;, option['value'].to_s
&lt;/pre&gt;

	&lt;p&gt;And replace id_of_dropdown with the dom id for the drop down you&#8217;re looking for, replace n with the nth number, and replace &#8216;Value To Compare&#8217; with the value you&#8217;re looking for.&lt;/p&gt;


So for example, if you have a dropdown with an id of &#8216;fruit&#8217; and you want to make sure that the third option is &#8216;banana&#8217; use this:
&lt;pre class='ruby'&gt;
    option = css_select(&quot;select#fruit&quot;)[3]
    assert_equal &quot;banana&quot;, option['value'].to_s
&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-01-09:47</id>
    <published>2008-01-09T13:04:00Z</published>
    <updated>2008-01-09T13:04:47Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/1/9/grabbing-the-model-object-using-the-controller-name" rel="alternate" type="text/html"/>
    <title>Ruby on Rails: Grabbing the model object using the controller name</title>
<content type="html">
            &lt;p&gt;I found myself in a situation where I was creating basically the same method in many of my controllers, the only difference being the name of the model object.  For example:&lt;/p&gt;


&lt;pre class='ruby'&gt;
class ArticlesController

 # .... cut 

 def set_recommended(is_recommended)
    @article.recommended = is_recommended
    @article.save    
    respond_to do |format|
      format.js { render :nothing =&amp;gt; true }
    end    
 end

class MoviesController
 # .... cut 

 def set_recommended(is_recommended)
    @movie.recommended = is_recommended
    @movie.save    
    respond_to do |format|
      format.js { render :nothing =&amp;gt; true }
    end    
 end

end
&lt;/pre&gt;  

	&lt;p&gt;So, I want to refactor this code, and create one method for this in the application.rb file &#8211; but how to refer to the @movie and @article objects?&lt;/p&gt;


	&lt;p&gt;Simply by using the &lt;strong&gt;instance_variable_get&lt;/strong&gt; method!  You simply pass in a string with the name of the method, and it returns the instance variable.&lt;/p&gt;


Here&#8217;s how the new method looked within the application.rb file. 
&lt;pre class='ruby'&gt;
class ApplicationController
 # .... cut 

  def set_recommended(is_recommended)
    object = instance_variable_get(&quot;@#{self.controller_class_name.chomp(&quot;Controller&quot;).singularize.underscore}&quot;)
    object.recommended = is_recommended
    object.save    
    respond_to do |format|
      format.js { render :nothing =&amp;gt; true }
    end        
  end

end
&lt;/pre&gt;

	&lt;p&gt;Of course, this only works for controllers and model names that follow the singular, plural convention (i.e. model name is article and controller name is ArticlesController)&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2008-01-03:45</id>
    <published>2008-01-03T21:58:00Z</published>
    <updated>2008-01-03T21:58:45Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2008/1/3/default-columns-in-fixtures" rel="alternate" type="text/html"/>
    <title>Default columns in fixtures</title>
<content type="html">
            &lt;p&gt;Ever find yourself writing out the same old column names in fixtures just so your tests will work?  Ever wonder if there&#8217;s a better way?  Well there is!&lt;/p&gt;


	&lt;p&gt;Fixtures, now with &lt;span class='caps'&gt;NEW&lt;/span&gt; default columns!&lt;/p&gt;


	&lt;p&gt;All you need to do is bung a method in your test/test_helper.rb file that looks like this:&lt;/p&gt;


&lt;pre class='ruby'&gt;
  def default_columns
    columns = &amp;lt;&amp;lt;END_OF_STRING
    created_at: 1 Jan 2007
    created_by: 1
    updated_at: 1 Jan 2007
    updated_by: 1
    lock_version: 1
  END_OF_STRING     
  end
&lt;/pre&gt;

	&lt;p&gt;Now, in each fixture that requires the default columns put this at the top:&lt;/p&gt;


&lt;pre class='ruby'&gt;
&amp;lt;% require &quot;#{RAILS_ROOT}/test/test_helper.rb&quot; %&amp;gt;                    
&lt;/pre&gt;

And then for each fixture bung in this inline code:
&lt;pre class='ruby'&gt;
one:
  id: 1   
  name: MyString
&amp;lt;%= default_columns %&amp;gt; 
&lt;/pre&gt;

	&lt;p&gt;Voila!  No more repeating yourself with stupid columns!  Hooray!&lt;/p&gt;


	&lt;p&gt;Note: spacing is important in yaml files &#8211; so make sure you put the &amp;lt;%= default_columns %&amp;gt; in an appropriate place&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2007-09-07:42</id>
    <published>2007-09-07T12:27:00Z</published>
    <updated>2007-09-07T13:08:56Z</updated>
    <link href="http://www.thechrisoshow.com/2007/9/7/use-javascript-reflection-to-get-obfuscated-google-map-object-members" rel="alternate" type="text/html"/>
    <title>Using Javascript reflection to get obfuscated Google Map object members</title>
<content type="html">
            &lt;p&gt;If you&#8217;ve had much to do with Google Maps, then you&#8217;ve probably come across the &lt;a href='http://www.google.com/apis/maps/documentation/reference.html#GMarker'&gt;GMarker&lt;/a&gt; object.  You use it when making your own custom markers on a Google Map.&lt;/p&gt;


You typically use it like this:
&lt;pre class='javascript'&gt;
var stdIcon.image = &quot;/images/myMarker.png&quot;;
stdIcon.iconSize = new GSize(20, 20);
stdIcon.iconAnchor = new GPoint(16, 16);
stdIcon.infoWindowAnchor = new GPoint(25, 7);   
var  marker = new GMarker(point, {icon:stdIcon});
map.addOverlay(marker);
&lt;/pre&gt;

	&lt;p&gt;Once the marker&#8217;s been created and placed on the map, then there didn&#8217;t seem to be anyway to get the &lt;i&gt;image element of the marker&lt;/i&gt; so I could do some funky stuff with it.&lt;/p&gt;


	&lt;p&gt;So, I opened up FireBug and had a look at the GMarkers internals:
&lt;img src='http://www.thechrisoshow.com/assets/2007/9/7/gmarker_props.PNG' alt='' /&gt;&lt;/p&gt;


	&lt;p&gt;The property I had been looking for was cleverly obfuscated with the boring name of Mj.
&#8220;Aha!&#8221; I thought, &#8220;Gotcha Google!  Can&#8217;t hide it away from me!&#8221; and proceeded to write code that referenced this highly secretive Mj property.&lt;/p&gt;


	&lt;p&gt;A month went by though, and I noticed that my code no longer worked &#8211; the Mj property had been renamed to Zj, breaking all my code!  Teach me for referencing an Obfuscated property I guess.&lt;/p&gt;


	&lt;p&gt;So, I got around it using Javascript reflection.&lt;/p&gt;


&lt;pre class='javascript'&gt;
function getMarkerImage(marker)
{
    for (var memberName in marker)
    {
        var memberObj = marker[memberName];

        if ((memberObj) &#38;&#38; (memberObj.tagName == &quot;IMG&quot;))
        {
            return memberObj;            
        }

    }
}
&lt;/pre&gt;

	&lt;p&gt;This method finds the first property of the GMarker class that has a tagName == &#8220;IMG&#8221;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2007-09-02:41</id>
    <published>2007-09-02T13:49:00Z</published>
    <updated>2007-09-02T13:50:11Z</updated>
    <link href="http://www.thechrisoshow.com/2007/9/2/find-latitude-and-longitude-for-an-address" rel="alternate" type="text/html"/>
    <title>Find latitude and longitude for an address</title>
<content type="html">
            &lt;p&gt;If you&#8217;re looking for the lat/long of an address, try my free &lt;a href='http://www.thechrisoshow.com/display_kml/geocoder'&gt;geocoder application&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;This app uses the &lt;a href='http://www.google.com/apis/maps/documentation/#Geocoding_Examples'&gt;Google Geocoding api&lt;/a&gt; to convert any worldwide address into its lat longs.&lt;/p&gt;


	&lt;p&gt;Just type an address/city/postcode into the text box, and click the button.  A Google push pin will appear on the map showing the details.  Simple huh!&lt;/p&gt;


	&lt;p&gt;&lt;img src='http://www.thechrisoshow.com/assets/2007/9/2/invercargill.png' alt='' /&gt;&lt;/p&gt;


	&lt;p&gt;Find it here: &lt;a href='http://www.thechrisoshow.com/display_kml/geocoder'&gt;http://www.thechrisoshow.com/display_kml/geocoder&lt;/a&gt;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2007-08-27:40</id>
    <published>2007-08-27T22:34:00Z</published>
    <updated>2007-09-07T13:28:02Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2007/8/27/quickly-add-google-driving-directions-to-your-rails-application" rel="alternate" type="text/html"/>
    <title>Quickly add Google Driving Directions to your rails application</title>
<content type="html">
            &lt;p&gt;Do you want driving directions on your website but don&#8217;t want to mess around with the Google Maps api?&lt;/p&gt;


	&lt;p&gt;Well, you can easily implement Google driving directions by calling the url:
http://maps.google.com/maps?saddr=&lt;i&gt;start_address&lt;/i&gt;&#38;daddr=&lt;i&gt;dest_address&lt;/i&gt;&#38;hl=en&lt;/p&gt;


So, in Rails you could do something like this:
&lt;pre class='ruby'&gt;
&amp;lt;%= button_to_function &quot;Get Driving Directions&quot;,  &quot;window.open(\&quot;http://maps.google.com/maps?saddr=#{URI.escape(source_address)}&#38;daddr=#{URI.escape(destination_address)}&#38;hl=en\&quot;)&quot; %&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;Where source_address and destination_address are string variables containing things like:&lt;/p&gt;


	&lt;p&gt;London -&amp;gt; Manchester&lt;br /&gt;
&lt;span class='caps'&gt;SE5 0LL&lt;/span&gt; -&amp;gt; &lt;span class='caps'&gt;NW6 6RJ&lt;/span&gt;&lt;br /&gt;
221b Baker St -&amp;gt; 10 Downing Street&lt;br /&gt;&lt;/p&gt;


	&lt;p&gt;This method handles all the geocoding for ya &#8211; and is way simpler than messing with the GDirections api object.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2007-08-24:36</id>
    <published>2007-08-24T23:02:00Z</published>
    <updated>2007-09-08T08:47:33Z</updated>
    <category term="Ruby on Rails"/>
    <link href="http://www.thechrisoshow.com/2007/8/24/how-to-dynamically-serve-a-kml-file-with-ruby-on-rails" rel="alternate" type="text/html"/>
    <title>How to dynamically serve a KML file with Ruby on Rails</title>
<content type="html">
            &lt;p&gt;Serving a &lt;span class='caps'&gt;KML&lt;/span&gt; file in Ruby on Rails is way easier than doing it in asp.net.&lt;/p&gt;


	&lt;p&gt;Here&#8217;s how I do it for my little &lt;a href='http://www.thechrisoshow.com/display_kml'&gt;display kml on a google map&lt;/a&gt; application.&lt;/p&gt;


First of all, you want to trick Google into thinking that the kml file is just a regular file with the extension &#8220;kml&#8221;.  I do this by using a custom route in routes.rb that looks a little something like this:
&lt;pre class='ruby'&gt;
map.connect ':controller/:action/:ignore_this_bit/:uuid.:format 
#The :ignore_this_bit is optional if you want to get around the google  
#caching issue detailed below
&lt;/pre&gt;

So that means you could serve the file with a url like this:
&lt;code&gt;
http://www.my_domain.com/my_controller/get_kml/'random_number'/11.kml 
&lt;/code&gt;
If you put a random number in there then you&#8217;ll get around Googles 4 minute cache.

Next you create a controller method that will spit out the &lt;span class='caps'&gt;KML&lt;/span&gt; and render it to the browser using the send_data function like the example detailed below. (I load my kml data using a &lt;span class='caps'&gt;UUID&lt;/span&gt; by the way &#8211; but you can do it any way you want):
&lt;pre class='ruby'&gt;
  def get_kml
    @kml_data = KmlData.find_by_uuid(params[:uuid])
    if @kml_data.kml     
        send_data @kml_data.kml
    else
      render :text =&amp;gt; 'Kml is empty'
    end
  end
&lt;/pre&gt;

	&lt;p&gt;The @kml_data.kml variable contains the raw xml formatted kml.&lt;/p&gt;


	&lt;p&gt;The real guts of the function is the send_data function.  This is rubys way of sending a file to the browser, which is how google expects to view the file.&lt;/p&gt;


	&lt;p&gt;And that&#8217;s it!  Piece of cake!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.thechrisoshow.com/">
    <author>
      <name>chriso</name>
    </author>
    <id>tag:www.thechrisoshow.com,2007-08-24:18</id>
    <published>2007-08-24T08:37:00Z</published>
    <updated>2007-09-07T13:37:54Z</updated>
    <link href="http://www.thechrisoshow.com/2007/8/24/for-loops-in-sql-server-2000" rel="alternate" type="text/html"/>
    <title>For Loop in SQL Server 2000</title>
<content type="html">
            &lt;p&gt;Here&#8217;s a &lt;strong&gt;just-so-crazy-it-might-work&lt;/strong&gt; way of doing a for loop in sql server.&lt;/p&gt;


	&lt;p&gt;This comes in real handy when making fake data for testing purposes.&lt;/p&gt;


&lt;pre class='sql'&gt;
DECLARE @count INT
DECLARE @command_to_loop NVARCHAR(1000)
-- This example runs from 1 to 10
SET @count = 1
WHILE(@count &amp;lt;= 10)
BEGIN
    -- This is just my crazy example
    -- Replace @command_to_loop with whatever you want to execute loop
    SET @command_to_loop = 'insert into customers (name) values 
                       (''Billy Bob' + cast(@count as varchar(3)) + ''')'
    print @command_to_loop
    EXEC sp_executesql @command_to_loop
    SET @count = @count + 1
END
&lt;/pre&gt;
          </content>  </entry>
</feed>
