calendar_helper 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,14 @@
1
+ === 0.2.1 / 2007-07-07
2
+
3
+ * Added html output to the tests for visual confirmation or
4
+ for developing new stylesheets. Run 'rake' and look
5
+ in the 'test/output' directory.
6
+ * Adds a 'today' CSS class to the cell for the current day.
7
+ Can be turned of by sending option 'show_today => false'.
8
+ [Chris O'Sullivan]
9
+ * Added 'accessible' option to show extra fields around
10
+ days that are not in the current month. [Tom Armitage]
11
+
1
12
  === 0.2.0
2
13
 
3
14
  * Converted to hoe and a rubygem
@@ -9,5 +9,4 @@ generators/calendar_styles/templates/grey/style.css
9
9
  generators/calendar_styles/templates/red/style.css
10
10
  init.rb
11
11
  lib/calendar_helper.rb
12
- test/stylesheet_tester.html
13
12
  test/test_calendar_helper.rb
data/README.txt CHANGED
@@ -17,6 +17,9 @@ Dynamic enhancements for starting week on Monday and highlighting weekends
17
17
  Geoffrey Grosenbach -- http://nubyonrails.com
18
18
  Test suite and conversion to a Rails plugin
19
19
 
20
+ Tom Armitage -- http://infovore.org
21
+ Improvements to markup (abbreviations on day-headings, <caption>); introduction of :accessible option.
22
+
20
23
  Usage
21
24
  =====
22
25
 
data/Rakefile CHANGED
@@ -12,6 +12,7 @@ Hoe.new('calendar_helper', CalendarHelper::VERSION) do |p|
12
12
  p.description = "A simple method to create an HTML calendar for a single month. Can be styled with CSS. Usable with Ruby on Rails."
13
13
  p.url = "http://rubyforge.org/projects/seattlerb"
14
14
  p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
15
+ p.clean_globs = ['test/output']
15
16
  end
16
17
 
17
18
  # desc "Test task (actually runs specs)"
@@ -61,3 +61,6 @@
61
61
  .weekendDay {
62
62
  background-color: #ffffdd;
63
63
  }
64
+ .today{
65
+ background-color: #4682b4;
66
+ }
@@ -65,6 +65,10 @@
65
65
  .weekendDay {
66
66
  background-color: #787888;
67
67
  }
68
+ .today {
69
+ background-color: white;
70
+ color: black;
71
+ }
68
72
 
69
73
  Colors:
70
74
  dk: 787888
@@ -54,3 +54,7 @@
54
54
  color: white;
55
55
  background-color: black;
56
56
  }
57
+ .today {
58
+ background-color: #1e90ff;
59
+ color: white;
60
+ }
@@ -3,7 +3,7 @@ require 'date'
3
3
  # CalendarHelper allows you to draw a databound calendar with fine-grained CSS formatting
4
4
  module CalendarHelper
5
5
 
6
- VERSION = '0.2.0'
6
+ VERSION = '0.2.1'
7
7
 
8
8
  # Returns an HTML calendar. In its simplest form, this method generates a plain
9
9
  # calendar (which can then be customized using CSS) for a given month and year.
@@ -25,7 +25,16 @@ module CalendarHelper
25
25
  # Use (0..2) for the first three letters, (0..0) for the first, and
26
26
  # (0..-1) for the entire name.
27
27
  # :first_day_of_week => 0 # Renders calendar starting on Sunday. Use 1 for Monday, and so on.
28
- #
28
+ # :accessible => true # Turns on accessibility mode. This suffixes dates within the
29
+ # # calendar that are outside the range defined in the <caption> with
30
+ # # <span class="hidden"> MonthName</span>
31
+ # # Defaults to false.
32
+ # # You'll need to define an appropriate style in order to make this disappear.
33
+ # # Choose your own method of hiding content appropriately.
34
+ #
35
+ # :show_today => false # Highlights today on the calendar using the CSS class 'today'.
36
+ # # Defaults to true.
37
+ #
29
38
  # For more customization, you can pass a code block to this method, that will get one argument, a Date object,
30
39
  # and return a values for the individual table cells. The block can return an array, [cell_text, cell_attrs],
31
40
  # cell_text being the text that is displayed and cell_attrs a hash containing the attributes for the <td> tag
@@ -66,7 +75,9 @@ module CalendarHelper
66
75
  :day_name_class => 'dayName',
67
76
  :day_class => 'day',
68
77
  :abbrev => (0..2),
69
- :first_day_of_week => 0
78
+ :first_day_of_week => 0,
79
+ :accessible => false,
80
+ :show_today => true
70
81
  }
71
82
  options = defaults.merge options
72
83
 
@@ -82,19 +93,30 @@ module CalendarHelper
82
93
  end
83
94
 
84
95
  cal = %(<table class="#{options[:table_class]}" border="0" cellspacing="0" cellpadding="0">)
85
- cal << %(<thead><tr class="#{options[:month_name_class]}"><th colspan="7">#{Date::MONTHNAMES[options[:month]]}</th></tr><tr class="#{options[:day_name_class]}">)
86
- day_names.each {|d| cal << "<th>#{d[options[:abbrev]]}</th>"}
96
+ cal << %(<caption class="#{options[:month_name_class]}"></caption><thead><th colspan="7">#{Date::MONTHNAMES[options[:month]]}</th></tr><tr class="#{options[:day_name_class]}">)
97
+ day_names.each do |d|
98
+ unless d[options[:abbrev]].eql? d
99
+ cal << "<th scope='col'><abbr title='#{d}'>#{d[options[:abbrev]]}</abbr></th>"
100
+ else
101
+ cal << "<th scope='col'>#{d[options[:abbrev]]}</th>"
102
+ end
103
+ end
87
104
  cal << "</tr></thead><tbody><tr>"
88
105
  beginning_of_week(first, first_weekday).upto(first - 1) do |d|
89
106
  cal << %(<td class="#{options[:other_month_class]})
90
107
  cal << " weekendDay" if weekend?(d)
91
- cal << %(">#{d.day}</td>)
108
+ if options[:accessible]
109
+ cal << %(">#{d.day}<span class="hidden"> #{Date::MONTHNAMES[d.month]}</span></td>)
110
+ else
111
+ cal << %(">#{d.day}</td>)
112
+ end
92
113
  end unless first.wday == first_weekday
93
114
  first.upto(last) do |cur|
94
115
  cell_text, cell_attrs = block.call(cur)
95
116
  cell_text ||= cur.mday
96
117
  cell_attrs ||= {:class => options[:day_class]}
97
118
  cell_attrs[:class] += " weekendDay" if [0, 6].include?(cur.wday)
119
+ cell_attrs[:class] += " today" if (cur == Date.today) and options[:show_today]
98
120
  cell_attrs = cell_attrs.map {|k, v| %(#{k}="#{v}") }.join(" ")
99
121
  cal << "<td #{cell_attrs}>#{cell_text}</td>"
100
122
  cal << "</tr><tr>" if cur.wday == last_weekday
@@ -102,7 +124,11 @@ module CalendarHelper
102
124
  (last + 1).upto(beginning_of_week(last + 7, first_weekday) - 1) do |d|
103
125
  cal << %(<td class="#{options[:other_month_class]})
104
126
  cal << " weekendDay" if weekend?(d)
105
- cal << %(">#{d.day}</td>)
127
+ if options[:accessible]
128
+ cal << %(">#{d.day}<span class='hidden'> #{Date::MONTHNAMES[d.mon]}</span></td>)
129
+ else
130
+ cal << %(">#{d.day}</td>)
131
+ end
106
132
  end unless last.wday == last_weekday
107
133
  cal << "</tr></tbody></table>"
108
134
  end
@@ -1,16 +1,21 @@
1
1
  require 'test/unit'
2
+ require 'fileutils'
2
3
  require File.expand_path(File.dirname(__FILE__) + "/../lib/calendar_helper")
3
4
 
4
5
  class CalendarHelperTest < Test::Unit::TestCase
5
6
 
6
7
  include CalendarHelper
7
-
8
-
8
+
9
+ def test_with_output
10
+ output = "<h2>Past Month</h2>" + calendar_with_defaults
11
+ output << "<h2>Current Month</h2>" + calendar_for_this_month
12
+ write_sample "sample.html", output
13
+ end
14
+
9
15
  def test_simple
10
16
  assert_match %r{August}, calendar_with_defaults
11
17
  end
12
18
 
13
-
14
19
  def test_required_fields
15
20
  # Year and month are required
16
21
  assert_raises(ArgumentError) {
@@ -21,20 +26,20 @@ class CalendarHelperTest < Test::Unit::TestCase
21
26
  }
22
27
  assert_raises(ArgumentError) {
23
28
  calendar :month => 1
24
- }
29
+ }
25
30
  end
26
31
 
27
32
  def test_default_css_classes
28
33
  # :other_month_class is not implemented yet
29
- { :table_class => "calendar",
30
- :month_name_class => "monthName",
31
- :day_name_class => "dayName",
32
- :day_class => "day" }.each do |key, value|
34
+ { :table_class => "calendar",
35
+ :month_name_class => "monthName",
36
+ :day_name_class => "dayName",
37
+ :day_class => "day"
38
+ }.each do |key, value|
33
39
  assert_correct_css_class_for_default value
34
40
  end
35
41
  end
36
42
 
37
-
38
43
  def test_custom_css_classes
39
44
  # Uses the key name as the CSS class name
40
45
  # :other_month_class is not implemented yet
@@ -43,14 +48,12 @@ class CalendarHelperTest < Test::Unit::TestCase
43
48
  end
44
49
  end
45
50
 
46
-
47
51
  def test_abbrev
48
52
  assert_match %r{>Mon<}, calendar_with_defaults(:abbrev => (0..2))
49
53
  assert_match %r{>M<}, calendar_with_defaults(:abbrev => (0..0))
50
54
  assert_match %r{>Monday<}, calendar_with_defaults(:abbrev => (0..-1))
51
55
  end
52
56
 
53
-
54
57
  def test_block
55
58
  # Even days are special
56
59
  assert_match %r{class="special_day">2<}, calendar(:year => 2006, :month => 8) { |d|
@@ -60,14 +63,24 @@ class CalendarHelperTest < Test::Unit::TestCase
60
63
  }
61
64
  end
62
65
 
63
-
64
66
  def test_first_day_of_week
65
- assert_match %r{<tr class="dayName">\s*<th>Sun}, calendar_with_defaults
66
- assert_match %r{<tr class="dayName">\s*<th>Mon}, calendar_with_defaults(:first_day_of_week => 1)
67
+ assert_match %r{<tr class="dayName">\s*<th scope='col'><abbr title='Sunday'>Sun}, calendar_with_defaults
68
+ # testing that if the abbrev and contracted version are the same, there should be no abbreviation.
69
+ assert_match %r{<tr class="dayName">\s*<th scope='col'>Sunday}, calendar_with_defaults(:abbrev => (0..8))
70
+ assert_match %r{<tr class="dayName">\s*<th scope='col'><abbr title='Monday'>Mon}, calendar_with_defaults(:first_day_of_week => 1)
67
71
  end
68
72
 
69
- private
73
+ def test_today_is_in_calendar
74
+ todays_day = Date.today.day
75
+ assert_match %r{class="day.+today">#{todays_day}<}, calendar_for_this_month
76
+ end
70
77
 
78
+ def test_should_not_show_today
79
+ todays_day = Date.today.day
80
+ assert_no_match %r{today}, calendar_for_this_month(:show_today => false)
81
+ end
82
+
83
+ private
71
84
 
72
85
  def assert_correct_css_class_for_key(css_class, key)
73
86
  assert_match %r{class="#{css_class}"}, calendar_with_defaults(key => css_class)
@@ -82,4 +95,18 @@ private
82
95
  calendar options
83
96
  end
84
97
 
98
+ def calendar_for_this_month(options={})
99
+ options = { :year => Time.now.year, :month => Time.now.month}.merge options
100
+ calendar options
101
+ end
102
+
103
+ def write_sample(filename, content)
104
+ FileUtils.mkdir_p "test/output"
105
+ File.open("test/output/#{filename}", 'w') do |f|
106
+ f.write %(<html><head><title>Stylesheet Tester</title><link href="../../generators/calendar_styles/templates/grey/style.css" media="screen" rel="Stylesheet" type="text/css" /></head><body>)
107
+ f.write content
108
+ f.write %(</body></html>)
109
+ end
110
+ end
111
+
85
112
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: calendar_helper
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.0
7
- date: 2007-06-24 00:00:00 -07:00
6
+ version: 0.2.1
7
+ date: 2007-07-07 00:00:00 -07:00
8
8
  summary: Generates a configurable, CSS-tagged HTML calendar.
9
9
  require_paths:
10
10
  - lib
@@ -40,7 +40,6 @@ files:
40
40
  - generators/calendar_styles/templates/red/style.css
41
41
  - init.rb
42
42
  - lib/calendar_helper.rb
43
- - test/stylesheet_tester.html
44
43
  - test/test_calendar_helper.rb
45
44
  test_files:
46
45
  - test/test_calendar_helper.rb
@@ -1,32 +0,0 @@
1
- <html>
2
- <head>
3
- <title>Stylesheet Tester</title>
4
- <link href="../generators/calendar_styles/templates/grey/style.css" media="screen" rel="Stylesheet" type="text/css" />
5
- <style>
6
- body, tr, td {
7
- font-family: Verdana;
8
- }
9
- </style>
10
- </head>
11
- <body style="background-color: #eeeeee">
12
-
13
- <table class="calendar" border="0" cellspacing="0" cellpadding="0">
14
- <thead>
15
- <tr class="monthName">
16
- <th colspan="7">April</th>
17
- </tr>
18
- <tr class="dayName">
19
- <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> <th>Fri</th> <th>Sat</th> <th>Sun</th> </tr>
20
- </thead>
21
- <tbody>
22
- <tr> <td class="otherMonth">27</td> <td class="otherMonth">28</td> <td class="otherMonth">29</td> <td class="otherMonth">30</td> <td class="otherMonth">31</td> <td class="day weekendDay">1</td> <td class="day weekendDay">2</td> </tr>
23
- <tr> <td class="day">3</td> <td class="day">4</td> <td class="day">5</td> <td class="day">6</td> <td class="day">7</td> <td class="day weekendDay">8</td> <td class="day weekendDay">9</td> </tr>
24
- <tr> <td class="day">10</td> <td class="day">11</td> <td class="day">12</td> <td class="day">13</td> <td class="day">14</td> <td class="day weekendDay">15</td> <td class="day weekendDay">16</td> </tr>
25
- <tr> <td class="day">17</td> <td class="day">18</td> <td class="day">19</td> <td class="day">20</td> <td class="day">21</td> <td class="day weekendDay">22</td> <td class="day weekendDay">23</td> </tr>
26
- <tr> <td class="day">24</td> <td class="day">25</td> <td class="day">26</td> <td class="specialDay">27</td> <td class="day">28</td> <td class="day weekendDay">29</td> <td class="day weekendDay">30</td> </tr>
27
- <tr> </tr>
28
- </tbody>
29
- </table>
30
-
31
- </body>
32
- </html>