farski-calendar_for 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +94 -0
- data/VERSION.yml +4 -0
- data/lib/calendar_for.rb +104 -0
- data/test/helper_test.rb +9 -0
- data/test/test_helper.rb +9 -0
- metadata +57 -0
data/README.rdoc
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
= CalendarFor
|
2
|
+
|
3
|
+
The calendar_for helper method generates standard calendars, in the form of HTML tables. Each day of the calendars being
|
4
|
+
output is a TD element; the content template for each cell is the block being passed to the helper; partials are the
|
5
|
+
easiest to deal with. Each yielded template is given access to a Time object for midnight on the day it's rendering.
|
6
|
+
|
7
|
+
All cells are created with a valid Time object. i.e. if you're making a calendar for Oct. 2008, the first day is a
|
8
|
+
Wednesday. Any cells that come before that in the table (e.g. Sunday, Monday, Tuesday) will use the end of September to
|
9
|
+
render the template. If you need those cells to be empty, they are classed in the HTML and you should just knock them
|
10
|
+
out with styles.
|
11
|
+
|
12
|
+
Usage
|
13
|
+
===========
|
14
|
+
|
15
|
+
The most basic implementation of the helper makes a calendar for all of the current month.
|
16
|
+
|
17
|
+
<%= calendar_for { |day| content_tag(:h3, day.mday } %>
|
18
|
+
|
19
|
+
This will generate a table with its first row being a TR element for each weekday's full name (e.g. Sunday,
|
20
|
+
Monday...Saturday), and then rows for each week of the month, where each cell contains an H3 element with the day of the
|
21
|
+
month (1,2...31).
|
22
|
+
|
23
|
+
Options
|
24
|
+
===========
|
25
|
+
|
26
|
+
The first argument is the date to be considered. It can be a Time or Date, a Chronic-parsable string, or a hash ({ :year
|
27
|
+
=> 2009 [, :month => 1 [, :day => 15]] })
|
28
|
+
|
29
|
+
Other options include:
|
30
|
+
:week_start is the day of the week the calendars should start on, default is Sunday.
|
31
|
+
accepts 0 – 6 (Sunday – Saturday), or a String or Symbol of the weekday name ("Sunday", :monday)
|
32
|
+
:timeframe is the scope of the calendars being produces. Default is :month
|
33
|
+
:day creates a single cell table for the supplied date
|
34
|
+
:week
|
35
|
+
:month
|
36
|
+
:quarter creates three tables for the months of the quarter the date is in (e.g if supplied date is Feb 2,
|
37
|
+
shows Jan. Feb, Mar)
|
38
|
+
:year goes back to January of the year for the supplied date and shows all months
|
39
|
+
Integer starts with the month of the supplied date and shows the next i-1 months
|
40
|
+
:weekday_headers false suppressed the row of TRs containing the weekday names
|
41
|
+
Array (length = 7) replaces the default weekday names (always starts with Sunday)
|
42
|
+
:html_classes replaces the default html class names for special cells (today, days outside the month being shown that
|
43
|
+
appear on the calendar)
|
44
|
+
:today
|
45
|
+
:out_of_timeframe
|
46
|
+
|
47
|
+
Examples
|
48
|
+
===========
|
49
|
+
|
50
|
+
Example 1
|
51
|
+
|
52
|
+
<%= calendar_for( { :year => 2010, :month => 5 },
|
53
|
+
:week_start => :monday,
|
54
|
+
:timeframe => :quarter,
|
55
|
+
:weekday_headers => ['S','M','T','W','T','F','S']
|
56
|
+
:html_classes => { :out_of_timeframe => 'blank' }
|
57
|
+
) { |day| render :partial => 'day', :locals => { :day => day } } %>
|
58
|
+
|
59
|
+
# _day.html.erb
|
60
|
+
|
61
|
+
<%= content_tag(:h5, link_to(day.mday, { :controller => 'calendar', :action => 'show', :year => day.year, :month =>
|
62
|
+
day.month, :day => day.mday })
|
63
|
+
|
64
|
+
|
65
|
+
Example 2
|
66
|
+
|
67
|
+
<%= calendar_for(Time.now, :weekday_start => :monday, :timeframe => :week) { render :partial => 'day', :locals => {
|
68
|
+
:day => day } }
|
69
|
+
|
70
|
+
# _day.html.erb
|
71
|
+
|
72
|
+
<%= link_to(day.mday) %>
|
73
|
+
<ol><%= render :partial => 'event', :collection => @events.select { |e| e.starts_at.midnight == day.midnight } %></ol>
|
74
|
+
|
75
|
+
|
76
|
+
# _event.html.erb
|
77
|
+
<%= content_tag(:li, event.summary) %>
|
78
|
+
|
79
|
+
Sample output
|
80
|
+
===========
|
81
|
+
|
82
|
+
# for <%= calendar_for({ :year => 2008, :month => 10 }, :week_start => :monday, :timeframe => :week) { |day|
|
83
|
+
content_tag(:p, day.mday) } %>
|
84
|
+
|
85
|
+
<table
|
86
|
+
class="calendar"><tr><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th
|
87
|
+
><th>Sunday</th></tr><tr><td class="null"><p>29</p></td><td
|
88
|
+
class="today"><p>30</p></td><td><p>1</p></td><td><p>2</p></td><td><p>3</p></td><td><p>4</p></td><td><p>5</p></td></tr>
|
89
|
+
</table>
|
90
|
+
|
91
|
+
COPYRIGHT
|
92
|
+
=========
|
93
|
+
|
94
|
+
Copyright (c) 2008 Chris Kalafarski. See LICENSE for details.
|
data/VERSION.yml
ADDED
data/lib/calendar_for.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
module CalendarHelper
|
2
|
+
# Creates a calendar table for a given date within a specified timeframe.
|
3
|
+
# Generic usage:
|
4
|
+
# <%= calendar_for { |day| render :partial => 'day', :locals => { :day => day } } %>
|
5
|
+
# This will default to the current time in a month timeframe.
|
6
|
+
# The date can be specified as a Date or Time object, as a string to be parsed by Chronic, or a hash like { :year => 2001, :month => 6 }
|
7
|
+
# Options:
|
8
|
+
# :week_start, the first day of the week, as an integer (0=Sunday, 6=Saturday), string ("Friday"), or symbol (:monday); default is Sunday.
|
9
|
+
# :timeframe, default is :month. others are, :year (starting with january), :quarter, :week, Integer (e.g. 12 would be specified month and the next 11)
|
10
|
+
# :html_classes, a hash to set custom class attributes for special TD tags, :today when the current day is displayed (default "today"),
|
11
|
+
# and :out_of_timeframe, eg when June 1 is a Friday and the week_start is Monday, the first four days shown are out_of_timeframe.
|
12
|
+
# The class of tables (calendars) can be set with :calendar
|
13
|
+
# :weekday_headers, to suppress a row of <th>Monday</th>... set to false, to replace standard "Monday", "Tuesday", supply an array
|
14
|
+
# Complex use:
|
15
|
+
# calendar_for({ :year => 1962 }, :week_start => :monday, :timeframe => 24, :html_classes => { :today => 'current' }, :weekday_headers => false) { |day| content_tag(:h1, day.day) }
|
16
|
+
def calendar_for(date_or_time_object_or_parameters = Time.now, *args, &block)
|
17
|
+
raise ArgumentError, "Missing block" unless block_given?
|
18
|
+
|
19
|
+
options = args.extract_options!
|
20
|
+
|
21
|
+
options[:html_classes] ||= Hash.new
|
22
|
+
options[:html_classes][:calendar] ||= "calendar"
|
23
|
+
options[:html_classes][:today] ||= "today"
|
24
|
+
options[:html_classes][:out_of_timeframe] ||= "null"
|
25
|
+
|
26
|
+
weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
|
27
|
+
options[:week_start] ||= 0
|
28
|
+
case options[:week_start]
|
29
|
+
when Integer
|
30
|
+
week_start = options[:week_start]
|
31
|
+
when String, Symbol
|
32
|
+
week_start = weekdays.index(options[:week_start].to_s.capitalize)
|
33
|
+
end
|
34
|
+
unless options[:weekday_headers] == false
|
35
|
+
weekdays = options[:weekday_headers] if (options[:weekday_headers].is_a?(Array) && options[:weekday_headers].size == 7 )
|
36
|
+
table_headers = Array.new
|
37
|
+
week_start.times { weekdays << weekdays.shift }
|
38
|
+
weekdays.each { |w| table_headers << content_tag(:th, w) }
|
39
|
+
table_headers_row = content_tag(:tr, table_headers.join)
|
40
|
+
end
|
41
|
+
|
42
|
+
case date_or_time_object_or_parameters
|
43
|
+
when Date, Time
|
44
|
+
timeframe_starts_on = date_or_time_object_or_parameters
|
45
|
+
when String
|
46
|
+
timeframe_starts_on = Chronic.parse(date_or_time_object_or_parameters)
|
47
|
+
when Hash
|
48
|
+
date_params = date_or_time_object_or_parameters
|
49
|
+
timeframe_starts_on = Time.local(date_params[:year], date_params[:month], date_params[:day])
|
50
|
+
end
|
51
|
+
|
52
|
+
case options[:timeframe]
|
53
|
+
when :day
|
54
|
+
months, weeks, days = 1, 1, 1
|
55
|
+
calendar_starts_on = timeframe_starts_on.midnight
|
56
|
+
when :week
|
57
|
+
calendar_starts_on = timeframe_starts_on.midnight.monday.advance(:days => (week_start-1))
|
58
|
+
months, weeks = 1, 1
|
59
|
+
when :quarter
|
60
|
+
months = 4
|
61
|
+
calendar_starts_on = timeframe_starts_on.midnight.beginning_of_quarter.monday.advance(:days => (week_start-1))
|
62
|
+
timeframe_starts_on = timeframe_starts_on.beginning_of_quarter
|
63
|
+
when :year
|
64
|
+
months = 12
|
65
|
+
calendar_starts_on = timeframe_starts_on.midnight.beginning_of_year.monday.advance(:days => (week_start-1))
|
66
|
+
timeframe_starts_on = timeframe_starts_on.beginning_of_year
|
67
|
+
when Integer
|
68
|
+
months = options[:timeframe]
|
69
|
+
calendar_starts_on = timeframe_starts_on.midnight.beginning_of_month.monday.advance(:days => (week_start-1))
|
70
|
+
else
|
71
|
+
months = 1
|
72
|
+
calendar_starts_on = timeframe_starts_on.midnight.beginning_of_month.monday.advance(:days => (week_start-1))
|
73
|
+
end
|
74
|
+
|
75
|
+
tables = Array.new
|
76
|
+
months.times do |m|
|
77
|
+
table_rows = Array.new
|
78
|
+
table_rows << table_headers_row if table_headers_row
|
79
|
+
weeks ||= ((timeframe_starts_on.end_of_month - calendar_starts_on)/(7.days)).ceil
|
80
|
+
weeks.times do |w|
|
81
|
+
table_cells = Array.new
|
82
|
+
(days || 7).times do |d|
|
83
|
+
current_date = calendar_starts_on.advance(:days => (w*7+d))
|
84
|
+
if current_date.midnight == Time.now.midnight
|
85
|
+
html_class = options[:html_classes][:today]
|
86
|
+
elsif current_date.month != timeframe_starts_on.month
|
87
|
+
html_class = options[:html_classes][:out_of_timeframe]
|
88
|
+
end
|
89
|
+
table_cells << content_tag(:td, yield(current_date), :class => html_class)
|
90
|
+
end
|
91
|
+
table_rows << content_tag(:tr, table_cells.join)
|
92
|
+
end
|
93
|
+
weeks = nil
|
94
|
+
html_rel = timeframe_starts_on.strftime('%Y%m%d')
|
95
|
+
timeframe_starts_on = timeframe_starts_on.advance(:months => 1)
|
96
|
+
calendar_starts_on = timeframe_starts_on.midnight.beginning_of_month.monday.advance(:days => (week_start-1))
|
97
|
+
tables << content_tag(:table, table_rows.join, :class => options[:html_classes][:calendar], :rel => html_rel)
|
98
|
+
end
|
99
|
+
|
100
|
+
return tables
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
ActionView::Base.send :include, CalendarHelper
|
data/test/helper_test.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class CalendarForTest < Test::Unit::TestCase
|
4
|
+
# no clue how to test this...
|
5
|
+
|
6
|
+
# should "probably rename this file and start testing for real" do
|
7
|
+
# flunk "hey buddy, you should probably rename this file and start testing for real"
|
8
|
+
# end
|
9
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: farski-calendar_for
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Kalafarski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-17 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: TODO
|
17
|
+
email: chris@farski.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- VERSION.yml
|
27
|
+
- lib/calendar_for.rb
|
28
|
+
- test/helper_test.rb
|
29
|
+
- test/test_helper.rb
|
30
|
+
has_rdoc: false
|
31
|
+
homepage: http://github.com/farski/calendar_for
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.2.0
|
53
|
+
signing_key:
|
54
|
+
specification_version: 2
|
55
|
+
summary: TODO
|
56
|
+
test_files: []
|
57
|
+
|