Showing posts with label webrat. Show all posts
Showing posts with label webrat. Show all posts

Thursday, June 25, 2009

Ponies and Filling out Forms with Webrat

‹prev | My Chain | next›

Following up on yesterday's simple form-by-example, today I get to fill out that form with Webrat.

Yay! Good clean fun.

In the site feedback Cucumber scenario, I am seeing the feedback form. Next up is "When I fill out the form with effusive praise", which I can define with:
When /^I fill out the form with effusive praise$/ do
fill_in "Name", :with => "Bob"
fill_in "Email", :with => "foo@example.org"
fill_in "Subject", :with => "Your site is awesome!"
fill_in "Message", :with => "The recipes are delicious and your use of CouchDB is impressive."
end
That's it! Webrat is just awesome.



This works because I have wisely wrapped my <label> tags around the input fields:
  <form action='email'>
<label>
Name
<input name='name' size='30' type='text' />
</label>

<label>
Email
<input name='email' size='30' type='text' />
</label>
<label>
Subject
<input name='subject' size='50' type='text' />
</label>
<label>
Message
<textarea cols='55' name='message' rows='8'></textarea>

</label>
<input name='Send Comments' type='submit' />
</form>
For the uninitiated, <label> tags not only cue Webrat which form fields to complete, but also activate the HTML form field when the label text is clicked—especially handy when clicking those tiny radio buttons or checkboxes.

I could also have added id attributes to the various form fields (e.g. <input id="email-input" type="text"…/>) and then done the label with a for attribute (e.g. <label for="email-input">Email</label>). That would have the same effect in most browsers, but would have the added benefit of working in Internet Explorer (wrapping <label> tags does not work in Internet Explorer), but why make life easier for Internet Explorer users?

With that step marked as complete, it is time to do something in response to submitting the feedback form. I drive-by-example a simple "Thanks for the feedback" page. Before I am really done, I need to actually do something with the feedback. In the various legacy versions of EEE Cooks, all that we do with the feedback is send an email. According to the Sinatra FAQ, that means Pony:
cstrom@jaynestown:~/repos/eee-code$ gem install pony
Building native extensions. This could take a while...
Successfully installed tmail-1.2.3.1
Successfully installed pony-0.3
2 gems installed
Pony is a great little gem—it does just enough to make sending email simple. The RSpec example that I use to drive sending email is:
    it "should send us an email" do
Pony.
should_receive(:mail).
with(hash_including(:subject => "Subject"))

post "/email",
:name => "Bob",
:subject => "Subject",
:message => "Feedback message."
end
The actual implementation:
post '/email' do
message = <<"_EOM"
From: #{params[:name]}
Email: #{params[:email]}

#{params[:message]}
_EOM

Pony.mail(:to => "us _at_ eeecooks.com".gsub(/\s*_at_\s*/, '@'),
:subject => params[:subject],
:body => message)

haml :email
end
That is a good stopping point for today. Tomorrow, I will work my way back out to the Cucumber scenario.