Permalink
Browse files

Merge master from p8 at e36c64a

  • Loading branch information...
pupeno committed Jan 17, 2012
2 parents cb091d7 + e36c64a commit bcd7b90ef55129536c089b551b5eb7783afbf022
View
@@ -1,3 +1,5 @@
+.DS_Store
+pkg
.idea
*.gem
.bundle
View
@@ -153,7 +153,7 @@ and going to http://localhos:8808
== Contributors
-Petrik de Heus, Andrew C. Greenberg, Jason Cheong-Kee-You, J. Pablo Fernández.
+Petrik de Heus, Sean Dague, F. Kocherga, John Duff, Andrew C. Greenberg, Jason Cheong-Kee-You, J. Pablo Fernández.
Original Work Copyright (c) 2008 Petrik de Heus, released under the MIT license.
Fork revisions Copyright (c) 2010 Andrew C. Greenberg, released under the MIT license.
View
@@ -1,2 +1 @@
-# Include hook code here
require 'table_builder'
View
@@ -1 +0,0 @@
-# Install hook code here
@@ -9,15 +9,15 @@ module CalendarHelper
# 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)
+ def calendar_for(objects, *args)
raise ArgumentError, "Missing block" unless block_given?
options = args.last.is_a?(Hash) ? args.pop : {}
html_options = options[:html]
builder = options[:builder] || CalendarBuilder
calendar = options[:calendar] || Calendar
-
- content = with_output_buffer{block.call(builder.new(objects||[], self, calendar, options))}
- self.content_tag(:table, content, html_options, false)
+ content_tag(:table, nil, html_options) do
+ yield builder.new(objects || [], self, calendar, options)
+ end
end
class CalendarBuilder < TableHelper::TableBuilder
@@ -26,74 +26,81 @@ def initialize(objects, template, calendar, options)
@calendar = calendar.new(options)
@today = options[:today] || Time.now
@row_header = options[:row_header] || false
- end
-
- def day(*args,&block)
- raise ArgumentError, "Missing block" unless block_given?
- options = options_from_hash(args)
- day_method = options.delete(:day_method) || :date
- id_pattern = options.delete(:id)
- activity_class = options.delete(:activity)
- output = ""
+ end
+
+ def day(*args)
+ raise ArgumentError, "Missing block" unless block_given?
+ options = options_from_hash(args)
+ day_method = options.delete(:day_method) || :date
+ id_pattern = options.delete(:id)
+ tbody do
@calendar.objects_for_days(@objects, day_method).to_a.sort{|a1, a2| a1.first <=> a2.first }.each do |o|
- key, array = o
- 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>'
- output << '</tr>' if (day.wday == @calendar.last_weekday)
+ key, array = o
+ day, objects = array
+ concat(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)
+ row_header_options[:class] ||= ""
+ row_header_options[:class] << " row_header"
+ concat(tag(:td, row_header_options, true))
+ yield(day, nil)
+ concat("</td>")
+ end
+ concat(tag(:td, td_options(day, id_pattern), true))
+ yield(day, objects)
+ concat('</td>')
+ concat('</tr>') if(day.wday == @calendar.last_weekday)
end
- @template.content_tag(:tbody, output.html_safe, {}, false)
+ end
end
private
def objects_for_days
@calendar.objects_for_days(@objects)
end
-
- def td_options(day, id_pattern, activity_class)
+
+ def td_options(day, id_pattern)
options = {}
- if(day.strftime("%Y-%m-%d") == @today.strftime("%Y-%m-%d"))
- options[:class] = 'today'
- elsif(day.month != @calendar.month)
- options[:class] = 'notmonth'
- elsif(day.wday == 0 or day.wday == 6)
- options[:class] = 'weekend'
- elsif activity_class
- options[:class] = activity_class
- end
- if id_pattern
- options[:id] = day.strftime(id_pattern)
- end
-
+ css_classes = []
+ css_classes << 'today' if day.strftime("%Y-%m-%d") == @today.strftime("%Y-%m-%d")
+ css_classes << 'notmonth' if day.month != @calendar.month
+ css_classes << 'weekend' if day.wday == 0 or day.wday == 6
+ css_classes << 'future' if day > @today.to_date
+ options[:class] = css_classes.join(' ') unless css_classes.empty?
+ options[:id] = day.strftime(id_pattern) if id_pattern
options
end
end
class Calendar
attr_accessor :first_weekday, :last_weekday, :month
+
+ # :first lets you set the first day to start the calendar on (default is the first day of the given :month and :year).
+ # :first => :today will use Date.today
+ # :last lets you set the last day of the calendar (default is the last day of the given :month and :year).
+ # :last => :thirty will show 30 days from :first
+ # :last => :week will show one week
def initialize(options={})
- @year = options[:year] || Time.now.year
- @month = options[:month] || Time.now.month
- @first_day_of_week = options[:first_day_of_week] || 0
- @first_weekday = first_day_of_week(@first_day_of_week)
- @last_weekday = last_day_of_week(@first_day_of_week)
- @first = Date.civil(@year, @month, 1)
- @last = Date.civil(@year, @month, -1)
+ @year = options[:year] || Time.now.year
+ @month = options[:month] || Time.now.month
+ @first_day_of_week = options[:first_day_of_week] || 0
+ @first_weekday = first_day_of_week(@first_day_of_week)
+ @last_weekday = last_day_of_week(@first_day_of_week)
+
+ @first = options[:first]==:today ? Date.today : options[:first] || Date.civil(@year, @month, 1)
+
+ if options[:last] == :thirty_days || options[:last] == :thirty
+ @last = @first + 30
+ elsif options[:last] == :one_week || options[:last] == :week
+ @last = @first
+ else
+ @last = options[:last] || Date.civil(@year, @month, -1)
+ end
+
end
-
+
def each_day
first_day.upto(last_day) do |day|
yield(day)
@@ -104,18 +111,18 @@ def last_day
last = @last
while(last.wday % 7 != @last_weekday % 7)
last = last.next
- end
+ end
last
end
-
+
def first_day
first = @first - 6
while(first.wday % 7 != (@first_weekday) % 7)
first = first.next
end
first
- end
-
+ end
+
def objects_for_days(objects, day_method)
unless @objects_for_days
@objects_for_days = {}
@@ -129,34 +136,34 @@ def objects_for_days(objects, day_method)
end
@objects_for_days
end
-
+
def days
unless @days
@days = []
- each_day{|day| @days << day}
+ each_day{|day| @days << day}
end
- @days
- end
-
+ @days
+ end
+
def mjdays
unless @mjdays
@mdays = []
- each_day{|day| @days << day}
+ each_day{|day| @days << day}
end
- @days
- end
-
+ @days
+ end
+
def first_day_of_week(day)
day
end
-
+
def last_day_of_week(day)
if day > 0
day - 1
else
6
end
- end
+ end
end
end
@@ -5,9 +5,10 @@ def table_for(objects, *args, &block)
options = args.last.is_a?(Hash) ? args.pop : {}
html_options = options[:html]
builder = options[:builder] || TableBuilder
-
- content = with_output_buffer{block.call(builder.new(objects||[], self, options))}
- self.content_tag(:table, content, html_options, false)
+
+ content_tag(:table, html_options) do
+ yield builder.new(objects || [], self, options)
+ end
end
class TableBuilder
@@ -23,10 +24,11 @@ def head(*args, &block)
@template.content_tag(:thead, nil, options_from_hash(args), true, &block)
else
@num_of_columns = args.size
- content = args.collect do |c|
- @template.content_tag(:th,c)
- end.join("\n").html_safe
- @template.content_tag(:thead, @template.content_tag(:tr,content,{},false))
+ content_tag(:thead,
+ content_tag(:tr,
+ args.collect { |c| content_tag(:th, c.html_safe)}.join('').html_safe
+ )
+ )
end
end
@@ -50,8 +52,12 @@ def body(*args, &block)
def body_r(*args, &block)
raise ArgumentError, "Missing block" unless block_given?
options = options_from_hash(args)
- tds = @objects.collect do |thing|
- @template.with_output_buffer{block.call(thing)}
+ tbody do
+ @objects.each { |c|
+ concat(tag(:tr, options, true))
+ yield(c)
+ concat('</tr>'.html_safe)
+ }
end
content = tds.collect do |td|
@template.content_tag(:tr,td, {}, false)
@@ -89,9 +95,26 @@ def options_from_hash(args)
args.last.is_a?(Hash) ? args.pop : {}
end
- # def content_tag(tag, content, *args, &block)
- # options = options_from_hash(args)
- # @template.content_tag(tag, content, options, &block)
- # end
+ def concat(tag)
+ @template.safe_concat(tag)
+ ""
+ end
+
+ def content_tag(tag, content, *args)
+ options = options_from_hash(args)
+ @template.content_tag(tag, content, options)
+ end
+
+ def tbody
+ concat('<tbody>')
+ yield
+ concat('</tbody>')
+ end
+
+ def tr options
+ concat(tag(:tr, options, true))
+ yield
+ concat('</tr>')
+ end
end
-end
+end
@@ -1,4 +0,0 @@
-# desc "Explaining what the task does"
-# task :table_builder do
-# # Task goes here
-# end
Oops, something went wrong.

0 comments on commit bcd7b90

Please sign in to comment.