Sunday, June 28, 2009

Feedback on Meals & Recipes

‹prev | My Chain | next›

I start today with a note about yesterday's work: when URL encoding in Sinatra, use Rack::Utils.escape. It works and it will encode in the same way that Rack::Test would expect things to be encoded. Maybe an obvious suggestion, but with the myriad of URL encoding options available in the Ruby world, it seemed one worth noting.

I used Rack::Utils.escape in the meal feedback link:
%div
%a{:href => "/feedback?url=#{Rack::Utils.escape(@url)}&subject=#{Rack::Utils.escape("[Meal] " + @meal['title'])}"} Send us feedback on this meal
Yesterday, I used the subject and url query parameters in that link to pre-populate the feedback form. Today, I need to ensure that the url parameter, when submitted by the feedback form to the "send us email" action does something. Specifically, it ought to include the URL in the email that we receive.

Unfortunately, because the email body is only one attribute to the Pony.mail call, I have to specify the entire body of the email message. I can use RSpec's hash_including so that I do not have to specify all of the Pony.mail attributes, but I still need the entire message body:
    it "should include the URL (if supplied) in the email" do
message = <<"_EOM"
From: from
Email: email

Message

URL: http://example.org/
_EOM

Pony.
should_receive(:mail).
with(hash_including(:body => message))

post "/email",
:name => "from",
:email => "email",
:subject => "Subject",
:message => "Message",
:url => "http://example.org/"
end
I get that and the previous non-URL example passing with this email action:
post '/email' do
message = <<"_EOM"
From: #{params[:name]}
Email: #{params[:email]}

#{params[:message]}
_EOM

message << "\nURL: #{params[:url]}\n" if params[:url]

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

haml :email
end
With that, I am ready to work my way back out to the Cucumber scenario to mark it as complete:



I implement the last step, that the form's subject should be pre-filled, with:
Then /^I should see a subject of "([^\"]*)"$/ do |subject|
response.should have_selector("input",
:value => subject)
end
And now, I have the entire meal feedback scenario complete:
cstrom@jaynestown:~/repos/eee-code$ cucumber features -n \
-s "Give feedback to the authors on a yummy meal"
Sinatra::Test is deprecated; use Rack::Test instead.
Feature: Site

So that I may explore many wonderful recipes and see the meals in which they were served
As someone interested in cooking
I want to be able to easily explore this awesome site

Scenario: Give feedback to the authors on a yummy meal
Given a "Yummy" meal enjoyed on 2009-06-27
When I view the "Yummy" meal
And I click "Send us feedback on this meal"
Then I should see a feedback form
And I should see a subject of "[Meal] Yummy"

1 scenario
5 passed steps
After driving-by-example a similar feedback link in the recipe.haml template, I can mark the recipe-feedback scenario as complete as well:
cstrom@jaynestown:~/repos/eee-code$ cucumber features -n \
> -s "Send compliments to the chef on a delicious recipe"
Sinatra::Test is deprecated; use Rack::Test instead.
Feature: Site

So that I may explore many wonderful recipes and see the meals in which they were served
As someone interested in cooking
I want to be able to easily explore this awesome site

Scenario: Send compliments to the chef on a delicious recipe
Given a recipe for Curried Shrimp
When I view the recipe
And I click "Send us feedback on this recipe"
Then I should see a feedback form
And I should see a subject of "[Recipe] Curried Shrimp"

1 scenario
5 passed steps
Not bad—two scenarios done in one night. Up next: RSS Feeds.

No comments:

Post a Comment