<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>validates_presence_of :purpose : Tag rails, everything about rails</title>
    <link>http://lenaherrmann.net</link>
    <language>en_US</language>
    <ttl>40</ttl>
    <description>Lena Herrmann</description>
    <item>
      <title>Comatose bug when creating pages in migrations</title>
      <description>&lt;p&gt;&lt;a href="http://comatose.rubyforge.org/"&gt;Comatose&lt;/a&gt; is a micro CMS, easy to embed in your Rails app, ideal when the customer wants to update her FAQ once in a while. I work on a few projects where comatose is integrated, and it's been the second time now that one specific problem has given headaches to me and my colleagues. Time to document the solution.&lt;/p&gt;


&lt;p&gt;Comatose has a hierarchical structure. All the pages are children of the comatose root page, which is usually not in use. This root page is created by the migration AddComatoseSupport. When we add comatose to a project, we usually create another migration, where some child pages are created and populated (because the customer usually already knows what pages she wants). This can look like this:&lt;/p&gt;


&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;root = &lt;span class="co"&gt;ComatosePage&lt;/span&gt;.find_by_title(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;Home Page&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;)
faq = &lt;span class="co"&gt;ComatosePage&lt;/span&gt;.create(&lt;span class="sy"&gt;:title&lt;/span&gt; =&amp;gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;FAQ&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="sy"&gt;:author&lt;/span&gt; =&amp;gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;System&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="sy"&gt;:body&lt;/span&gt; =&amp;gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Text goes here.&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="sy"&gt;:parent_id&lt;/span&gt; =&amp;gt; root.id&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;All will be fine if you run the AddComatoseSupport migration first, and then the other one. If you run both of them in one go, all will look correctly first, too. But then - try to access the application and you get "a nil object when you didn't expect it!" A look in the database will tell you that your root page and your FAQ page are both there - but the slugs (needed to access the pages) are nil. Why is that?&lt;/p&gt;

&lt;p&gt;The answer: The class ComatosePage gets overwritten in the AddComatoseSupport migration, to make sure the comatose root page can be created (they are invalid without parent_id). But if you have another migration following this one, where you create a couple of comatose pages, they still are instances of that overwritten class. Especially the before/after filters from the original ComatosePage aren't evaluated. In those filters the slug fields are meant to be populated, so they are nil.&lt;/p&gt;

&lt;p&gt;First soluton: Define a class ComatoseRootPage instead, so you won't override anything.&lt;/p&gt;

&lt;p&gt;Second and much easier solution: Remove the class redefinition altogether, for the root page just do
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="co"&gt;ComatosePage&lt;/span&gt;.new(...).save(&lt;span class="pc"&gt;false&lt;/span&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
 &lt;a href="http://github.com/lenalena/comatose/commit/4005cf44f11165393b18b3fed2837a9afb57814a"&gt;See on github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Unfortunately the comatose project is not updated very often, so this bug is still unsolved in the generated migrations.&lt;/p&gt;</description>
      <pubDate>Tue, 27 Oct 2009 22:27:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0f740e51-8617-4048-a94b-90528f912f58</guid>
      <comments>http://lenaherrmann.net/2009/10/27/comatose-bug-when-creating-pages-in-migrations#comments</comments>
      <category>rails</category>
      <category>comatose</category>
      <category>bug</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=4</trackback:ping>
      <link>http://lenaherrmann.net/2009/10/27/comatose-bug-when-creating-pages-in-migrations</link>
    </item>
    <item>
      <title>Snippets for cucumber</title>
      <description>&lt;p&gt;In every project I use cucumber (that means, always) there are a few things that I keep reinventing, or I spend a lot of time searching for them in other projects. Here are some of them. They aren't all written by me, but maybe my coworkers like to use this as a reference, too?&lt;/p&gt;


&lt;p&gt;When you upgrade to the current cucumber (0.4.2) from an older version some steps will fail, because cucumber doesn't examine the whole html anymore. You can still check the raw html with these steps:&lt;/p&gt;


&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="co"&gt;Then&lt;/span&gt; &lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;^I should see &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]*)&amp;quot; in the source$&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt; |text|
  response.body.should match(&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;text&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt;)
&lt;span class="r"&gt;end&lt;/span&gt;

&lt;span class="co"&gt;Then&lt;/span&gt; &lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;^I should not see &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]*)&amp;quot; in the source$&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt; |text|
  response.body.should_not match(&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;text&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt;)
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Want to test sorting in one step?&lt;/p&gt;


&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="co"&gt;Then&lt;/span&gt; &lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;^I should see &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]*)&amp;quot; before &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]*)&amp;quot;$&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt; |first, second|
  response.body.should match(&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;first&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="k"&gt;.*&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;second&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="mod"&gt;im&lt;/span&gt;&lt;/span&gt;)
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Sometimes you want to click a link in a specific div:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="co"&gt;When&lt;/span&gt; &lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;^I buy &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]*)&amp;quot;$&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt; |title|
  product = &lt;span class="co"&gt;Product&lt;/span&gt;.find_by_title(title)
  field_by_xpath(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;//div[@id='buy_product_dialog_product_&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;product.id&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="k"&gt;']//input&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;).click
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Or you want to test what happens when you delete a specific object:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="co"&gt;When&lt;/span&gt; &lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;^I press &amp;quot;Delete&amp;quot; for comment &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]*)&amp;quot;$&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt; |comment|
  tag = &lt;span class="co"&gt;Comment&lt;/span&gt;.find_by_title(comment)
  field_by_xpath(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;//form[@id='delete_comment_&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;comment.id&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="k"&gt;']//input[@type='submit']&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;).click
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;With this you can check for a string that's within a tag with a specific id:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="co"&gt;Then&lt;/span&gt; &lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;^I should see &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]+)&amp;quot; in &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]+)&amp;quot;$&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt; |content, css_id|
  response.body.should have_tag(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;#&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;css_id&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, content)
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Use this regex to check for something within a given tag:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="co"&gt;Then&lt;/span&gt; &lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;^I should see &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]*)&amp;quot; within a (&lt;/span&gt;&lt;span class="ch"&gt;\w&lt;/span&gt;&lt;span class="k"&gt;+) tag$&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt; |text, tag|
  response.body.should match(&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;&amp;lt;&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;tag&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="k"&gt;[^&amp;gt;]*&amp;gt;[^&amp;lt;]*&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;text&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="k"&gt;[^&amp;lt;]*&amp;lt;&lt;/span&gt;&lt;span class="ch"&gt;\/&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;tag&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="k"&gt;&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt;)
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Cucumber 0.4.2 auto-generates webrat_steps.rb including a step to save the current page and open it in the browser. Just in case not everyone knows this step by now:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="co"&gt;Then&lt;/span&gt; &lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;^show me the page$&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt;
  save_and_open_page
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
</description>
      <pubDate>Sun, 25 Oct 2009 21:53:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:f0a52106-3a2f-4e60-b2cf-94b3ac484f5e</guid>
      <comments>http://lenaherrmann.net/2009/10/25/snippets-for-cucumber#comments</comments>
      <category>rails</category>
      <category>xpath</category>
      <category>cucumber</category>
      <category>testing</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=3</trackback:ping>
      <link>http://lenaherrmann.net/2009/10/25/snippets-for-cucumber</link>
    </item>
    <item>
      <title>Things I learned recently</title>
      <description>&lt;p&gt;Doing client work for &lt;a href="http://upstream-agile.com"&gt;Upstream&lt;/a&gt;, I recently had the pleasure of pair programming with fabulous &lt;a href="http://freelancing-gods.com"&gt;Pat Allan&lt;/a&gt; for a month. Here are a few of the things I learned.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    Pair Programming is a good thing (not that I haven't known that before). Here is &lt;a href="http://www.pathf.com/blogs/2009/09/pair-programming-york-times/"&gt;yet another good article about pairing&lt;/a&gt; if you aren't convinced yet. Next to improved concentration, keeping each other from getting stuck in stuff, I like the sharing of best practices, Textmate short cuts, syntax tricks etc.
  &lt;/li&gt;
  
  &lt;li&gt;
    Instead of writing "puts" in, after and before blocks that don't act as expected, use ruby-debug. It's really easy, and gives you so much more information about your situation.
    Just do:
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;notextile&gt;&lt;span class="CodeRay"&gt;sudo gem install ruby-debug&lt;/span&gt;&lt;/notextile&gt;&lt;/pre&gt;&lt;/div&gt;      
And in your code, at the place you want to examine:
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;notextile&gt;&lt;span class="CodeRay"&gt;require &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;ruby-debug&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
debugger&lt;/span&gt;&lt;/notextile&gt;&lt;/pre&gt;&lt;/div&gt;
    Then restart script/server with the --debug option. It stops when the line where you put 'debugger' is reached. You can directly call variables to explore their values. The basic commands are:
    &lt;ul class="second-level"&gt;
      &lt;li&gt;n - next&lt;/li&gt;
      &lt;li&gt;s - step into method&lt;/li&gt;
      &lt;li&gt;c - continue to breakpoint&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  


  &lt;li&gt;
    Read the source code of the gems you're using. If you are not sure how to do something with the gem, the actual code is often more informative than documentation. Lachie Cox &lt;a href="http://smartbomb.com.au/2008/02/i-love-me-some-source"&gt;describes in his blog&lt;/a&gt; how to set up a Textmate shortcut with which you can access all your gems without any typing. 
  &lt;/li&gt;

  &lt;li&gt;
    &lt;a href="http://www.rubular.com"&gt;Rubular&lt;/a&gt; is a nice app to test regular expressions in your browser.
  &lt;/li&gt;

  &lt;li&gt; 
    I have never used RCov before. Of course it doesn't mean too much, but having a test coverage of 97,7% feels good, I can tell you. A Code to Test Ratio of 1:2.7 is not bad either.
    &lt;br/&gt;
    The RCov gem has moved to &lt;a href="http://github.com/relevance/rcov"&gt;Relevance&lt;/a&gt;, &lt;a href="http://www.claytonlz.com/index.php/2009/04/how-to-setup-rspec-cucumber-webrat-rcov-and-autotest-on-leopard"&gt;read here&lt;/a&gt; how to set it up for an RSpec/Cucumber environment. 
  &lt;/li&gt;

  &lt;li&gt;
    And finally, I got convinced that it's good to have a blog ...
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here we are!&lt;/p&gt;

</description>
      <pubDate>Sat, 24 Oct 2009 21:12:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0934e5ce-393c-4b62-96f3-e14fe762114f</guid>
      <comments>http://lenaherrmann.net/2009/10/24/things-i-learned-recently#comments</comments>
      <category>ruby</category>
      <category>rails</category>
      <category>debugging</category>
      <category>pairing</category>
      <category>textmate</category>
      <category>testing</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=1</trackback:ping>
      <link>http://lenaherrmann.net/2009/10/24/things-i-learned-recently</link>
    </item>
  </channel>
</rss>
