public
Description: Canberra Ruby Crew Site #crc
Homepage: http://canberraruby.com
Clone URL: git://github.com/artpop/crc_site.git
Renamed and refactored next_meeting_date helper to meeting_date(date) so that 
any date can be passed and it now correctly shows the next month's meeting date 
if the meeting date for the chosen date has passed
schlick (author)
Thu Oct 16 05:39:33 -0700 2008
commit  1c5f88026ef9fb1a15e067e2e37daff8786f6df6
tree    84644cb8f1f26c22b0f803bd03e8b8cd1c5f049e
parent  d0aa182ccdf6d5fc35bc613a6a96562669631309
...
2
3
4
5
6
 
 
7
8
 
 
 
 
 
 
9
10
11
 
 
 
 
 
 
 
 
 
 
12
13
14
...
2
3
4
 
 
5
6
7
 
8
9
10
11
12
13
14
15
 
16
17
18
19
20
21
22
23
24
25
26
27
28
0
@@ -2,13 +2,27 @@ module PageHelper
0
   
0
   # TODO: should be made customisable
0
   MEETING_WDAY = 3        # Wednesday
0
-  
0
-  # returns the date for the next meeting
0
+
0
+  # returns the date for the next meeting from today and formats the output for display
0
   def next_meeting_date
0
-    last_day_of_month = Date.today.end_of_month
0
+    meeting_date(Date.today).to_s(:rfc822)
0
+  end
0
+  
0
+  # returns the date for the next meeting after a particular date
0
+  def meeting_date(date)
0
+    last_day_of_month = date.end_of_month
0
     total = last_day_of_month.wday + MEETING_WDAY + 1
0
     total -= 7 if total >= 7
0
-    return last_day_of_month - total.days
0
+    the_meeting_date = last_day_of_month - total.days
0
+    if the_meeting_date < date
0
+      meeting_date(date.next_month.beginning_of_month)
0
+    else
0
+      the_meeting_date
0
+    end
0
+  rescue
0
+    # TODO: what to return if a DateTime is not passed? should I not worry?
0
+    # TODO: maybe allow passing of month number of the current year as an integer
0
+    ""
0
   end
0
   
0
 end
...
22
23
24
25
 
26
27
28
...
22
23
24
 
25
26
27
28
0
@@ -22,7 +22,7 @@
0
   http://groups.google.com/group/canberra-ruby
0
 
0
 %h2 Meetings
0
-%p== We aim to have meetups on the last Wednesday of the month. According to the calendar that may well be #{next_meeting_date.to_s(:rfc822)} (check for announcements in the mailing list).
0
+%p== We aim to have meetups on the last Wednesday of the month. According to the calendar that may well be #{next_meeting_date} (check for announcements in the mailing list).
0
 
0
 %h2 Peeps
0
 #peeps
...
2
3
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
6
7
 
8
9
10
11
12
13
14
15
16
17
 
 
 
 
 
 
18
19
20
21
22
23
24
25
26
27
 
 
 
 
 
28
29
30
31
32
33
34
35
36
37
 
 
 
 
 
38
39
40
41
42
43
44
45
46
47
 
 
 
 
 
48
49
50
51
52
53
54
55
56
57
 
 
 
 
 
58
59
60
61
62
63
64
65
66
67
 
 
 
 
 
68
69
70
71
72
73
74
75
76
 
 
 
 
 
77
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
80
81
...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
23
24
 
 
 
 
 
 
 
 
 
25
26
27
28
29
30
31
 
 
 
 
 
 
 
 
 
32
33
34
35
36
37
 
 
 
 
 
 
 
 
 
38
39
40
41
42
43
 
 
 
 
 
 
 
 
 
44
45
46
47
48
49
 
 
 
 
 
 
 
 
 
50
51
52
53
54
55
 
 
 
 
 
 
 
 
 
56
57
58
59
60
61
 
 
 
 
 
 
 
 
62
63
64
65
66
67
 
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
0
@@ -2,80 +2,93 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
0
 
0
 describe PageHelper do
0
   include PageHelper
0
+
0
+  describe "next meeting date" do
0
+    before(:each) do
0
+      @mock_today = Date.new(2008,1,1)
0
+    end
0
+    it "should call meeting date helper" do
0
+      Date.stub!(:today).and_return(@mock_today)
0
+      should_receive(:meeting_date).with(@mock_today).and_return(@mock_today)
0
+      next_meeting_date
0
+    end
0
+    it "should format the output" do
0
+      stub!(:meeting_date).and_return(@mock_today)
0
+      @mock_today.should_receive(:to_s).with(:rfc822)
0
+      next_meeting_date
0
+    end
0
+  end
0
   
0
   # TODO: change the tests so that it is not tied to the particular day of the week
0
-  describe "next_meeting_date (Wednesday)" do
0
+  describe "meeting date" do
0
     
0
-    it "should return last Wednesday for month ending on a Wednesday" do
0
-      mock_today = Date.new(2008,12,1)
0
-      last_wednesday = mock_today.end_of_month
0
-      1.upto(31) { |d|
0
-        Date.stub!(:today).and_return(mock_today)
0
-        next_meeting_date.should eql(last_wednesday)      
0
-        mock_today += 1.day
0
-      }
0
-    end
0
+    describe "for current month has not passed" do
0
+      it "should return last Wednesday for month ending on a Wednesday" do
0
+        mock_today = Date.new(2008,12,1)
0
+        last_wednesday = mock_today.end_of_month
0
+        meeting_date(mock_today).should eql(last_wednesday)      
0
+      end
0
 
0
-    it "should return last Wednesday for month ending on a Thursday" do
0
-      mock_today = Date.new(2008,7,1)
0
-      last_wednesday = mock_today.end_of_month - 1.day
0
-      1.upto(31) { |d|
0
-        Date.stub!(:today).and_return(mock_today)
0
-        next_meeting_date.should eql(last_wednesday)      
0
-        mock_today += 1.day
0
-      }
0
-    end
0
+      it "should return last Wednesday for month ending on a Thursday" do
0
+        mock_today = Date.new(2008,7,1)
0
+        last_wednesday = mock_today.end_of_month - 1.day
0
+        meeting_date(mock_today).should eql(last_wednesday)      
0
+      end
0
 
0
-    it "should return last Wednesday for month ending on a Friday" do
0
-      mock_today = Date.new(2008,10,1)
0
-      last_wednesday = mock_today.end_of_month - 2.days
0
-      1.upto(31) { |d|
0
-        Date.stub!(:today).and_return(mock_today)
0
-        next_meeting_date.should eql(last_wednesday)      
0
-        mock_today += 1.day
0
-      }
0
-    end
0
+      it "should return last Wednesday for month ending on a Friday" do
0
+        mock_today = Date.new(2008,10,1)
0
+        last_wednesday = mock_today.end_of_month - 2.days
0
+        meeting_date(mock_today).should eql(last_wednesday)      
0
+      end
0
 
0
-    it "should return last Wednesday for month ending on a Saturday" do
0
-      mock_today = Date.new(2008,5,1)
0
-      last_wednesday = mock_today.end_of_month - 3.days
0
-      1.upto(30) { |d|
0
-        Date.stub!(:today).and_return(mock_today)
0
-        next_meeting_date.should eql(last_wednesday)      
0
-        mock_today += 1.day
0
-      }
0
-    end
0
+      it "should return last Wednesday for month ending on a Saturday" do
0
+        mock_today = Date.new(2008,5,1)
0
+        last_wednesday = mock_today.end_of_month - 3.days
0
+        meeting_date(mock_today).should eql(last_wednesday)      
0
+      end
0
 
0
-    it "should return last Wednesday for month ending on a Sunday" do
0
-      mock_today = Date.new(2008,8,1)
0
-      last_wednesday = mock_today.end_of_month - 4.days
0
-      1.upto(31) { |d|
0
-        Date.stub!(:today).and_return(mock_today)
0
-        next_meeting_date.should eql(last_wednesday)      
0
-        mock_today += 1.day
0
-      }
0
-    end
0
+      it "should return last Wednesday for month ending on a Sunday" do
0
+        mock_today = Date.new(2008,8,1)
0
+        last_wednesday = mock_today.end_of_month - 4.days
0
+        meeting_date(mock_today).should eql(last_wednesday)      
0
+      end
0
        
0
-    it "should return last Wednesday for month ending on a Monday" do
0
-      mock_today = Date.new(2008,6,1)
0
-      last_wednesday = mock_today.end_of_month - 5.days
0
-      1.upto(30) { |d|
0
-        Date.stub!(:today).and_return(mock_today)
0
-        next_meeting_date.should eql(last_wednesday)      
0
-        mock_today += 1.day
0
-      }
0
-    end
0
+      it "should return last Wednesday for month ending on a Monday" do
0
+        mock_today = Date.new(2008,6,1)
0
+        last_wednesday = mock_today.end_of_month - 5.days
0
+        meeting_date(mock_today).should eql(last_wednesday)      
0
+      end
0
 
0
-    it "should return last Wednesday for month ending on a Tuesday" do
0
-      mock_today = Date.new(2008,9,1)
0
-      last_wednesday = mock_today.end_of_month - 6.days
0
-      1.upto(30) { |d|      
0
-        Date.stub!(:today).and_return(mock_today)
0
-        next_meeting_date.should eql(last_wednesday)
0
-        mock_today += 1.day     
0
-      }
0
+      it "should return last Wednesday for month ending on a Tuesday" do
0
+        mock_today = Date.new(2008,9,1)
0
+        last_wednesday = mock_today.end_of_month - 6.days
0
+        meeting_date(mock_today).should eql(last_wednesday)      
0
+      end
0
     end
0
-          
0
+    
0
+    describe "for current month is today" do
0
+      it "should return today as the last Wednesday for the month" do
0
+        mock_today = Date.new(2008,3,26)        # last Wednesday of the month
0
+        last_wednesday = mock_today
0
+        meeting_date(mock_today).should eql(last_wednesday)
0
+      end
0
+    end
0
+    
0
+    describe "for current month has passed" do
0
+      it "should return last Wednesday for next month" do
0
+        mock_today = Date.new(2008,9,25)        # day after the last wednesday
0
+        last_wednesday = Date.new(2008,10,29)   # last wednesday of next month
0
+        meeting_date(mock_today).should eql(last_wednesday)      
0
+      end
0
+    end
0
+    
0
+    describe "is passed an invalid date" do
0
+      it "should return empty string" do
0
+        mock_today = "Today"
0
+        meeting_date(mock_today).should eql("")
0
+      end
0
+    end
0
+    
0
   end
0
 
0
 end

Comments