later_dude 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +9 -0
- data/VERSION +1 -1
- data/init.rb +0 -1
- data/lib/later_dude.rb +31 -39
- data/test/calendar_test.rb +21 -17
- data/test/test_helper.rb +2 -3
- metadata +2 -2
data/CHANGELOG
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
2010/03/07
|
2
|
+
----------
|
3
|
+
- Make LaterDude Rails 3-compatible
|
4
|
+
- Include Rails 2 compatibility layer
|
5
|
+
|
6
|
+
2009/11/18
|
7
|
+
----------
|
8
|
+
- Make it possible for subclasses to override default settings.
|
9
|
+
|
1
10
|
2009/10/30
|
2
11
|
----------
|
3
12
|
- Introduced new :yield_surrounding_days option to yield surrounding days of other months to passed block.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/init.rb
CHANGED
data/lib/later_dude.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
require '
|
2
|
-
require 'action_view'
|
1
|
+
require 'rails2_compat'
|
3
2
|
|
4
3
|
module LaterDude
|
5
4
|
module CalendarHelper
|
@@ -10,12 +9,15 @@ module LaterDude
|
|
10
9
|
|
11
10
|
# TODO: Maybe make output prettier?
|
12
11
|
class Calendar
|
12
|
+
include ActionView::Helpers::CaptureHelper
|
13
13
|
include ActionView::Helpers::TagHelper
|
14
14
|
include ActionView::Helpers::UrlHelper
|
15
15
|
|
16
|
+
attr_accessor :output_buffer
|
17
|
+
|
16
18
|
def initialize(year, month, options={}, &block)
|
17
19
|
@year, @month = year, month
|
18
|
-
@options = options.symbolize_keys.reverse_merge(
|
20
|
+
@options = options.symbolize_keys.reverse_merge(self.class.default_calendar_options)
|
19
21
|
|
20
22
|
# next_month and previous_month take precedence over next_and_previous_month
|
21
23
|
@options[:next_month] ||= @options[:next_and_previous_month]
|
@@ -26,22 +28,14 @@ module LaterDude
|
|
26
28
|
end
|
27
29
|
|
28
30
|
def to_html
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
#{show_month_names}
|
33
|
-
#{show_day_names}
|
34
|
-
</thead>
|
35
|
-
<tbody>
|
36
|
-
#{show_days}
|
37
|
-
</tbody>
|
38
|
-
</table>
|
39
|
-
EOF
|
31
|
+
content_tag(:table, :class => "#{@options[:calendar_class]}") do
|
32
|
+
content_tag(:tbody, show_days)+ content_tag(:thead, "#{show_month_names}#{show_day_names}".html_safe)
|
33
|
+
end
|
40
34
|
end
|
41
35
|
|
42
36
|
private
|
43
37
|
def show_days
|
44
|
-
"
|
38
|
+
content_tag(:tr, "#{show_previous_month}#{show_current_month}#{show_following_month}".html_safe)
|
45
39
|
end
|
46
40
|
|
47
41
|
def show_previous_month
|
@@ -49,13 +43,13 @@ module LaterDude
|
|
49
43
|
|
50
44
|
returning "" do |output|
|
51
45
|
beginning_of_week(@days.first).upto(@days.first - 1) { |d| output << show_day(d) }
|
52
|
-
end
|
46
|
+
end.html_safe
|
53
47
|
end
|
54
48
|
|
55
49
|
def show_current_month
|
56
50
|
returning "" do |output|
|
57
51
|
@days.first.upto(@days.last) { |d| output << show_day(d) }
|
58
|
-
end
|
52
|
+
end.html_safe
|
59
53
|
end
|
60
54
|
|
61
55
|
def show_following_month
|
@@ -63,7 +57,7 @@ module LaterDude
|
|
63
57
|
|
64
58
|
returning "" do |output|
|
65
59
|
(@days.last + 1).upto(beginning_of_week(@days.last + 1.week) - 1) { |d| output << show_day(d) }
|
66
|
-
end
|
60
|
+
end.html_safe
|
67
61
|
end
|
68
62
|
|
69
63
|
def show_day(day)
|
@@ -85,11 +79,12 @@ module LaterDude
|
|
85
79
|
content = day.day
|
86
80
|
end
|
87
81
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
82
|
+
content = content_tag(:td, content.to_s.html_safe, options)
|
83
|
+
|
84
|
+
# close table row at the end of a week and start a new one
|
85
|
+
# opening and closing tag for the first and last week are included in #show_days
|
86
|
+
content << "</tr><tr>".html_safe if day < @days.last && day.wday == last_day_of_week
|
87
|
+
content
|
93
88
|
end
|
94
89
|
|
95
90
|
def beginning_of_week(day)
|
@@ -101,9 +96,7 @@ module LaterDude
|
|
101
96
|
def show_month_names
|
102
97
|
return if @options[:hide_month_name]
|
103
98
|
|
104
|
-
|
105
|
-
#{previous_month}#{current_month}#{next_month}
|
106
|
-
</tr>)
|
99
|
+
content_tag(:tr, "#{previous_month}#{current_month}#{next_month}".html_safe, :class => 'month_names')
|
107
100
|
end
|
108
101
|
|
109
102
|
# @options[:previous_month] can either be a single value or an array containing two values. For a single value, the
|
@@ -133,14 +126,13 @@ module LaterDude
|
|
133
126
|
def show_month(month, format, options={})
|
134
127
|
options[:colspan] ||= 2
|
135
128
|
|
136
|
-
|
137
|
-
|
138
|
-
text = I18n.localize(month, :format => format.first.to_s)
|
129
|
+
content_tag(:th, :colspan => options[:colspan], :class => "#{options[:class]} #{Date::MONTHNAMES[month.month].downcase}") do
|
130
|
+
if format.kind_of?(Array) && format.size == 2
|
131
|
+
text = I18n.localize(month, :format => format.first.to_s).html_safe
|
139
132
|
format.last.respond_to?(:call) ? link_to(text, format.last.call(month)) : text
|
140
133
|
else
|
141
|
-
format.respond_to?(:call) ? format.call(month) : I18n.localize(month, :format => format.to_s)
|
134
|
+
format.respond_to?(:call) ? format.call(month) : I18n.localize(month, :format => format.to_s).html_safe
|
142
135
|
end
|
143
|
-
output << '</th>'
|
144
136
|
end
|
145
137
|
end
|
146
138
|
|
@@ -158,12 +150,10 @@ module LaterDude
|
|
158
150
|
|
159
151
|
def show_day_names
|
160
152
|
return if @options[:hide_day_names]
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
end
|
166
|
-
output << "</tr>"
|
153
|
+
content_tag(:tr, :class => 'day_names') do
|
154
|
+
apply_first_day_of_week(day_names).inject('') do |output, day|
|
155
|
+
output << content_tag(:th, include_day_abbreviation(day), :scope => 'col', :class => Date::DAYNAMES[day_names.index(day)].downcase)
|
156
|
+
end.html_safe
|
167
157
|
end
|
168
158
|
end
|
169
159
|
|
@@ -171,7 +161,7 @@ module LaterDude
|
|
171
161
|
def include_day_abbreviation(day)
|
172
162
|
return day if @options[:use_full_day_names]
|
173
163
|
|
174
|
-
|
164
|
+
content_tag(:abbr, day, :title => full_day_names[abbreviated_day_names.index(day)])
|
175
165
|
end
|
176
166
|
|
177
167
|
def apply_first_day_of_week(day_names)
|
@@ -209,4 +199,6 @@ module LaterDude
|
|
209
199
|
end
|
210
200
|
end
|
211
201
|
end
|
212
|
-
end
|
202
|
+
end
|
203
|
+
|
204
|
+
ActionView::Base.send(:include, LaterDude::CalendarHelper)
|
data/test/calendar_test.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
+
I18n.load_path += Dir[File.expand_path('fixtures/locales', File.dirname(__FILE__)) + '/*.yml']
|
4
|
+
|
3
5
|
# TODO: figure out why I have to reference Calendar via its module ...
|
4
6
|
class CalendarTest < ActiveSupport::TestCase
|
5
7
|
# some constants for increased readability
|
@@ -8,6 +10,10 @@ class CalendarTest < ActiveSupport::TestCase
|
|
8
10
|
FULL_MONTH_NAMES = I18n.translate(:'date.month_names')
|
9
11
|
ABBR_MONTH_NAMES = I18n.translate(:'date.abbr_month_names')
|
10
12
|
|
13
|
+
setup do
|
14
|
+
I18n.locale = 'en'
|
15
|
+
end
|
16
|
+
|
11
17
|
test "has default options" do
|
12
18
|
# TODO improve this so that every call gets a fresh copy of the default options
|
13
19
|
default_calendar_options = LaterDude::Calendar.send(:default_calendar_options)
|
@@ -28,12 +34,10 @@ class CalendarTest < ActiveSupport::TestCase
|
|
28
34
|
assert !default_calendar_options[:previous_month]
|
29
35
|
assert !default_calendar_options[:next_and_previous_month]
|
30
36
|
|
31
|
-
# some options use i18n ...
|
32
|
-
I18n.stubs(:translate).with(:'date.first_day_of_week', :default => "0").then.returns("1")
|
33
|
-
I18n.stubs(:translate).with(:'date.formats.calendar_header', :default => "%B").then.returns("%B %Y")
|
34
|
-
|
35
37
|
# default first day of week is Sunday (= 0) if no translation is set in locale
|
36
38
|
assert_equal 0, default_calendar_options[:first_day_of_week]
|
39
|
+
|
40
|
+
I18n.locale = 'de'
|
37
41
|
# with default first day of week set in locale
|
38
42
|
assert_equal 1, (LaterDude::Calendar.send(:default_calendar_options)[:first_day_of_week]) # have to do this so that we don't use the cached version
|
39
43
|
|
@@ -70,7 +74,7 @@ class CalendarTest < ActiveSupport::TestCase
|
|
70
74
|
end
|
71
75
|
|
72
76
|
test "uses next month for proc" do
|
73
|
-
assert_match %r(<a href="/events/2009/2">»</a>), LaterDude::Calendar.new(2009, 1, :next_month => lambda { |date|
|
77
|
+
assert_match %r(<a href="/events/2009/2">»</a>), LaterDude::Calendar.new(2009, 1, :next_month => lambda { |date| %(<a href="/events/#{date.year}/#{date.month}">»</a>) }).send(:next_month)
|
74
78
|
end
|
75
79
|
|
76
80
|
test "uses next month for array if first value is a string and second is a proc" do
|
@@ -90,7 +94,7 @@ class CalendarTest < ActiveSupport::TestCase
|
|
90
94
|
end
|
91
95
|
|
92
96
|
test "uses previous month for proc" do
|
93
|
-
assert_match %r(<a href="/events/2008/12">«</a>), LaterDude::Calendar.new(2009, 1, :previous_month => lambda { |date|
|
97
|
+
assert_match %r(<a href="/events/2008/12">«</a>), LaterDude::Calendar.new(2009, 1, :previous_month => lambda { |date| %(<a href="/events/#{date.year}/#{date.month}">«</a>) }).send(:previous_month)
|
94
98
|
end
|
95
99
|
|
96
100
|
test "uses previous month for array if first value is a string and second is a proc" do
|
@@ -122,7 +126,7 @@ class CalendarTest < ActiveSupport::TestCase
|
|
122
126
|
end
|
123
127
|
|
124
128
|
test "uses current month for proc" do
|
125
|
-
assert_match %r(<a href="/events/2009/1">Current</a>), LaterDude::Calendar.new(2009, 1, :current_month => lambda { |date|
|
129
|
+
assert_match %r(<a href="/events/2009/1">Current</a>), LaterDude::Calendar.new(2009, 1, :current_month => lambda { |date| %(<a href="/events/#{date.year}/#{date.month}">Current</a>) }).send(:current_month)
|
126
130
|
end
|
127
131
|
|
128
132
|
test "uses current month for array if first value is a string and second is a proc" do
|
@@ -135,8 +139,14 @@ class CalendarTest < ActiveSupport::TestCase
|
|
135
139
|
|
136
140
|
# helper methods
|
137
141
|
test "shows whether a given day is on a weekend or not" do
|
138
|
-
|
139
|
-
(1..5).each
|
142
|
+
# first week of March 2010 starts on Monday => :-)
|
143
|
+
(Date.civil(2010, 3, 1)..Date.civil(2010, 3, 5)).each do |day|
|
144
|
+
assert !LaterDude::Calendar.weekend?(day)
|
145
|
+
end
|
146
|
+
|
147
|
+
[Date.civil(2010, 3, 6), Date.civil(2010, 3, 7)].each do |day|
|
148
|
+
assert LaterDude::Calendar.weekend?(day)
|
149
|
+
end
|
140
150
|
end
|
141
151
|
|
142
152
|
test "includes day name abbreviation" do
|
@@ -194,12 +204,10 @@ class CalendarTest < ActiveSupport::TestCase
|
|
194
204
|
end
|
195
205
|
|
196
206
|
test "shows special days as designated by a block" do
|
197
|
-
CalendarTest.send(:include, ActionView::Helpers)
|
198
|
-
|
199
207
|
# all even days should be linked
|
200
208
|
special_days_proc = lambda do |day|
|
201
209
|
if day.day.even?
|
202
|
-
[
|
210
|
+
[%(<a href="/calendar/#{day.year}/#{day.month}/#{day.day}">#{day.day}</a>), { :class => 'specialDay' }]
|
203
211
|
else
|
204
212
|
day.day
|
205
213
|
end
|
@@ -218,8 +226,6 @@ class CalendarTest < ActiveSupport::TestCase
|
|
218
226
|
end
|
219
227
|
|
220
228
|
test "yields days of surrounding months :yield_surrounding_days is set to true" do
|
221
|
-
CalendarTest.send(:include, ActionView::Helpers)
|
222
|
-
|
223
229
|
# make it bold
|
224
230
|
special_days_proc = lambda { |day| "<b>#{day.day}</b>" }
|
225
231
|
|
@@ -234,8 +240,6 @@ class CalendarTest < ActiveSupport::TestCase
|
|
234
240
|
end
|
235
241
|
|
236
242
|
test "yields days of surrounding months :yield_surrounding_days isn't set to true" do
|
237
|
-
CalendarTest.send(:include, ActionView::Helpers)
|
238
|
-
|
239
243
|
# make it bold
|
240
244
|
special_days_proc = lambda { |day| "<b>#{day.day}</b>" }
|
241
245
|
|
@@ -252,4 +256,4 @@ class CalendarTest < ActiveSupport::TestCase
|
|
252
256
|
end
|
253
257
|
|
254
258
|
# TODO: Should I do "real" output testing despite the good coverage of output-related methods? Testing HTML is tedious ...
|
255
|
-
end
|
259
|
+
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: later_dude
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Clemens Kofler
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-03-07 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|