Monday, August 31, 2009

Another Mini Step in the Mini Calendar

‹prev | My Chain | next›

I continue working on the homepage mini-calendar tonight. Last night and tonight, I drive by example the following Haml template:
  - week = 0
- while ((sunday0 + week*7).mon <= day1.mon)
%tr{:class => "week#{week+1}"}
- (0..6).map{|d| sunday0 + d + week*7}.each do |date|
%td{:id => date.to_s}
= date.mday if date.mon == day1.mon
- week = week + 1
I cannot help thinking that I could have done this without the week accumulator, but this is what the examples drove me to:
cstrom@jaynestown:~/repos/eee-code$ spec ./spec/views/mini_calendar.haml_spec.rb -cfs

mini_calendar.haml
- should show a human readable month and year
- should show a human readable month and year for other months
- should not include previous month's dates
- should have the first of the month in the first week
- should have the last of the month
- should not include next month's dates
I will consider this the "get it done" phase. The "do it right" phase can wait for another day.

Next, I need to link to meals in this month. In RSpec speak:
describe "mini_calendar.haml" do
before(:each) do
assigns[:month] = "2009-08"
assigns[:meals_by_date] = {"2009-08-08" => "Meal Title"}
end

it "should link to meals this month" do
render("views/mini_calendar.haml")
response.should have_selector("td#2009-08-08 a",
:href => "/meals/2009/08/08",
:title => "Meal Title")
end

To make that example pass, I add another layer of conditionals, checking the @meals_by_date instance variable:
  - week = 0
- while ((sunday0 + week*7).mon <= day1.mon)
%tr{:class => "week#{week+1}"}
- (0..6).map{|d| sunday0 + d + week*7}.each do |date|
%td{:id => date.to_s}
- if date.mon == day1.mon
- if @meals_by_date.include?(date.to_s)
%a{:href => date.strftime("/meals/%Y/%m/%d"),
:title => @meals_by_date[date.to_s]}
= date.mday
- else
= date.mday

- week = week + 1
With that, I can mark one more step as complete. In my Cucumber scenario there should be 3 days in the month with meals:
Then /^there should be 3 links to meals$/ do
response.should have_selector("td a", :count => 3)
end
Running the Cucumber scenario, I do indeed have one more step done:



Tomorrow I need to add navigation between months. I think I may have missed an edge case here as well, so another Cucumber scenario is in order.

No comments:

Post a Comment