Thursday, May 21, 2009

Meal Thumbnails

‹prev | My Chain | next›

I'm back inside some view specs today. Specifically, I need to add images to the meals-in-a-month view. The meal JSON includes an _attachments attribute that describes the image, so I add them to the before(:each) block:
        { "value" => [{ "_id"     => '2009-05-14',
"date" => '2009-05-14',
"title" => 'Meal 1',
"summary" => 'Meal 1 Summary',
"_attachments" => {"image1.jpg" => { }}
}]
},
The example then becomes:
  it "should include a thumbnail image of the meal" do
render("/views/meal_by_month.haml")
response.should have_selector("img",
:src => "/images/2009-05-14/image1.jpg",
:width => "200",
:height => "150")
end
Running the specification fails as expected:
cstrom@jaynestown:~/repos/eee-code$ spec ./spec/views/meal_by_month.haml_spec.rb
...F**

Pending:

meal_by_month.haml should include the menu items (Not Yet Implemented)
./spec/views/meal_by_month.haml_spec.rb:51

meal_by_month.haml should include recipe titles in the menu items (Not Yet Implemented)
./spec/views/meal_by_month.haml_spec.rb:53

1)
'meal_by_month.haml should include a thumbnail image of the meal' FAILED
expected following output to contain a <img height='150' width='200' src='/images/2009-05-14/image1.jpg'/> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<h1>Meals from 2009-05</h1>
<div class="meals">
<h2>
<span class="date">2009-05-14</span>
<span class="title">Meal 1</span>
</h2>
<p>Meal 1 Summary</p>
<h2>
<span class="date">2009-05-15</span>
<span class="title">Meal 2</span>
</h2>
<p>Meal 2 Summary</p>
</div>
</body></html>
./spec/views/meal_by_month.haml_spec.rb:45:

Finished in 0.012593 seconds

6 examples, 1 failure, 2 pending
Happily, I already have an image_link helper method for links to CouchDB images. I add it to the meal_by_month.haml template:
%h1= "Meals from #{@year}-#{@month}"

.meals
- @meals["rows"].each do |meal|
= image_link meal['value'][0]
%h2
%span.date= meal['value'][0]['date']
%span.title= meal['value'][0]['title']
%p= meal['value'][0]['summary']
Running the spec, I still get a failure:
1)
'meal_by_month.haml should include a thumbnail image of the meal' FAILED
expected following output to contain a <img height='150' width='200' src='/images/2009-05-14/image1.jpg'/> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<h1>Meals from 2009-05</h1>
<div class="meals">
<img src="/images/2009-05-14/image1.jpg"><h2>
<span class="date">2009-05-14</span>
<span class="title">Meal 1</span>
</h2>
<p>Meal 1 Summary</p>
<img src="/images/2009-05-15/image2.jpg"><h2>
<span class="date">2009-05-15</span>
<span class="title">Meal 2</span>
</h2>
<p>Meal 2 Summary</p>
</div>
</body></html>
./spec/views/meal_by_month.haml_spec.rb:46:
Ah, I do not have width and height attributes in the output. As currently defined, image_link does not accept an attributes hash. It should accept something like:
    it "should include image attributes" do
image_link(@doc, :alt => "foo").
should have_selector("img", :alt => "foo")
end
To make that example pass, I update the helper to accept an optional attributes hash and build attributes thusly:
    def image_link(doc, options={ })
#...
attrs = options.map{|kv| %Q|#{kv.first}="#{kv.last}"|}.join(" ")
%Q|<img #{attrs} src="/images/#{doc['_id']}/#{filename}"/>|
end
Updating the Haml template to supply the width and height attributes gets the example passing.

No comments:

Post a Comment