Skip to content

Commit

Permalink
Row headers in the calendar.
Browse files Browse the repository at this point in the history
  • Loading branch information
pupeno committed Jan 17, 2012
1 parent 02a4f47 commit 5e001e6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.rdoc
Expand Up @@ -116,7 +116,24 @@ If you want to add id's to your td tag you can pass a pattern:

<%= t.day(:id => 'day_%d') do |day, tasks| %>

To have a header at the begining of each row:

<%= calendar_for(@tasks, :row_header => true) do |t| %>

and then in your block you get nil as the list of objects and the first day of thet upcoming week. For example:

<%= calendar_for(@tasks) do |t| %>
<%= t.day do |day, tasks| %>
<% if tasks.nil? %>
<%= day.cweek %>
<% else %>
<%= day.day %>
<% tasks.each do |task| %>
<%= h(task.name) %>
<% end %>
<% end %>
<% end %>
<% end %>
== Install

Inside your Gemfile:
Expand Down
19 changes: 18 additions & 1 deletion lib/table_builder/calendar_helper.rb
@@ -1,5 +1,13 @@
module CalendarHelper

# Generates a calendar (as a table) for an array of objects placing each of them on the corresponding date.
#
# TODO: fully document this method, the current documentation is far from done.
#
# @param [Hash] options extra options
# :row_header if true, each row will have an extra cell at the beginning, as a row header. A typical usage would be
# to output week numbers. When the block is called, it will get the date that would normally be passed to the
# first day of the week (to give you some context) and a nil list of objects (and that's how you recognize it as
# a header, because empty days get an empty array, not nil).
def calendar_for(objects, *args, &block)
raise ArgumentError, "Missing block" unless block_given?
options = args.last.is_a?(Hash) ? args.pop : {}
Expand All @@ -16,6 +24,7 @@ def initialize(objects, template, calendar, options)
super(objects, template, options)
@calendar = calendar.new(options)
@today = options[:today] || Time.now
@row_header = options[:row_header] || false
end

def day(*args,&block)
Expand All @@ -30,6 +39,14 @@ def day(*args,&block)
day, objects = array

output << @template.tag(:tr,options,true) if (day.wday == @calendar.first_weekday)
if @row_header && day.wday == @calendar.first_weekday
row_header_options = td_options(day, id_pattern, (objects.empty? ? nil: activity_class))
row_header_options[:class] ||= ""
row_header_options[:class] << " row_header"
output << @template.tag(:td, row_header_options, true)
output << @template.with_output_buffer{block.call(day, nil)}
output << '</td>'
end
output << @template.tag(:td,td_options(day, id_pattern, (objects.empty? ? nil: activity_class)), true)
output << @template.with_output_buffer{block.call(day, objects)}
output << '</td>'
Expand Down

0 comments on commit 5e001e6

Please sign in to comment.