<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>adams.co.tt blog - tagged with ruby</title>
  <id>http://adams.co.tt/</id>
  <updated>2012-04-19</updated>
  <author>
    <name></name>
  </author>
  <entry>
    <title>Rendering partials in toto</title>
    <link rel="alternate" href="http://adams.co.tt/blog/2012/04/19/rendering-partials-in-toto/"/>
    <id>http://adams.co.tt/blog/2012/04/19/rendering-partials-in-toto/</id>
    <published>2012-04-19</published>
    <updated>2012-04-19</updated>
    <author>
      <name></name>
    </author>
    <content type="html">&lt;p&gt;&lt;a href="https://github.com/cloudhead/toto"&gt;toto&lt;/a&gt; is a fantastic lightweight blogging engine. In fact, at the time of writing, this very blog is running on it. By looking at &lt;a href="https://github.com/cloudhead/dorothy"&gt;dorothy&lt;/a&gt; you can see how it uses a layout and templates to render content. However, by default, it does not provide support for template partials.&lt;/p&gt;

&lt;p&gt;However, that&amp;rsquo;s easily solved with a little monkey patching:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module Toto
  class Site
    class Context
      def to_partial(page)
        to_html page, @config
      end
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you can include partials in your templates like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;%= render 'sidebar', :partial %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, I&amp;rsquo;m not a fan of monkey patching for a number reasons, not least of which is the fact that a good monkey patch is often a missed opportunity to improve an open source project. With that in mind, &lt;a href="https://github.com/cloudhead/toto/pull/121"&gt;here&lt;/a&gt; is the pull request to have this functionality added to toto itself.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Duplicate Active Model validation errors</title>
    <link rel="alternate" href="http://adams.co.tt/blog/2012/04/12/duplicate-active-model-validation-errors/"/>
    <id>http://adams.co.tt/blog/2012/04/12/duplicate-active-model-validation-errors/</id>
    <published>2012-04-12</published>
    <updated>2012-04-12</updated>
    <author>
      <name></name>
    </author>
    <content type="html">&lt;p&gt;When attempting to test a model using Active Model validations, I came across a rather odd problem.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'active_model'

class Person
  include ActiveModel::Validations
  validates_length_of :name, :maximum =&amp;gt; 40, :message =&amp;gt; 'name too long'
  attr_reader :name
  def initialize(attrs)
    @name = attrs[:name]
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I was seeing duplicate &lt;code&gt;'name too long'&lt;/code&gt; error messages in the errors hash.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;describe Person do
  describe 'validation' do
    it 'should be invalid if name is too long' do
      person = Person.new(:name =&amp;gt; 'a' * 41)
      person.should_not be_valid
      person.errors[:name].should == ['name too long']
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The above spec failed with the following message:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Failures:

  1) Person validation should be invalid if name is too long
     Failure/Error: person.errors[:name].should == ['name too long']
       expected: ["name too long"]
            got: ["name too long", "name too long"] (using ==)
     # ./spec/person_spec.rb:17
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Some further digging revealed that the &lt;code&gt;Person&lt;/code&gt; was being loaded twice by the &lt;code&gt;require&lt;/code&gt; method. &lt;code&gt;require&lt;/code&gt; was being called twice with two different paths, which both resolved to the file containing &lt;code&gt;Person&lt;/code&gt;. Removing the extra call to &lt;code&gt;require&lt;/code&gt; fixed the issue. This seems to be a problem particular to ruby 1.8.7.&lt;/p&gt;

&lt;p&gt;The runnable code examples reproducing this issue can be found &lt;a href="https://github.com/adscott/validations-spike"&gt;here&lt;/a&gt;.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Introducing mailcrate</title>
    <link rel="alternate" href="http://adams.co.tt/blog/2012/03/12/introducing-mailcrate/"/>
    <id>http://adams.co.tt/blog/2012/03/12/introducing-mailcrate/</id>
    <published>2012-03-12</published>
    <updated>2012-03-12</updated>
    <author>
      <name></name>
    </author>
    <content type="html">&lt;p&gt;Often when I&amp;rsquo;m working near the top of the &lt;a href="http://blog.goneopen.com/2010/08/test-automation-pyramid-review/"&gt;test automation pyramid&lt;/a&gt;, I find myself needing to test interactions with outside services such as SMTP servers.&lt;/p&gt;

&lt;p&gt;On projects in the past, I&amp;rsquo;ve been able to leverage java tools such as &lt;a href="http://www.icegreen.com/greenmail/"&gt;greenmail&lt;/a&gt;, which lets me have logic like this.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class MailerTest {

  private Greenmail greenmail

  @Before
  public void startGreenmail() {
    greenMail = new GreenMail();
    greenMail.start();
  }

  @After
  public void stopGreenmail() {
    greenMail.stop();
  }

  @Test
  public void testYourSendingCode() throws Exception {    
    new Mailer().send("to@localhost.com", "body");

    assertEquals("body", GreenMailUtil.getBody(greenMail.getReceivedMessages()[0]));
  }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This library is particularly useful as it allows interrogation of sent mails directly from the test. However, I have had trouble finding an equivalent gem for ruby development. However, there is the fantastic command line tool, &lt;a href="https://github.com/mmower/mailtrap"&gt;mailtrap&lt;/a&gt;. Mailtrap is written in ruby and:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Mailtrap waits on your chosen port for a client to connect and talks &lt;em&gt;just enough&lt;/em&gt; SMTP protocol for ActionMailer to successfully deliver its message.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Unfortunately, mailtrap does not play nicely with automated tests. It writes out captured emails to the filesystem, and there is no easy way to start, stop or interrogate a server in memory. However, mailtrap and greenmail have acted as the inspiration for &lt;a href="https://github.com/adscott/mailcrate"&gt;mailcrate&lt;/a&gt;. Mailcrate is designed to be run from your test code and allows interrogation of sent mails from the test itself. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'mailcrate'

describe Mailer do

  before do
    @mailcrate = Mailcrate.new(2525)
    @mailcrate.start
  end

  after do
    @mailcrate.stop
  end

  it 'should use Mailcrate to send mails' do
    mail = Mailer.welcome_email('a@b.com')
    mail.deliver

    @mailcrate.mails[0][:from].should == 'from@example.com'
    @mailcrate.mails[0][:to_list].should include 'a@b.com'
    @mailcrate.mails[0][:body].should include 'Full of awesomeness.'
  end

end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Mailcrate has been published as a gem and can be used by running:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem install mailcrate
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Mailcrate is still rather immature. Any feedback would be welcomed, both here on this blog or its &lt;a href="https://github.com/adscott/mailcrate"&gt;github&lt;/a&gt; page.&lt;/p&gt;
</content>
  </entry>
</feed>
