diff --git a/lib/has_calendar.rb b/lib/has_calendar.rb index 15e52b7..56cfdae 100644 --- a/lib/has_calendar.rb +++ b/lib/has_calendar.rb @@ -12,19 +12,19 @@ def calendar(options={}, &block) :caption_format => :default, :id => "calendar" }.merge(options) - + cmd = 'cal ' cmd << '-m ' unless RUBY_PLATFORM =~ /darwin/ - + # execute the command output = IO.popen("#{cmd} #{options[:month]} #{options[:year]}") {|f| f.read} - + # get calendar lines io = StringIO.new(output) lines = io.readlines lines = lines[2, lines.size-2] lines.map!(&:chomp) - + # strip spaces from each day and group them days = lines.inject([]) do |holder, line| 0.step(line.size, 3) do |n| @@ -32,34 +32,37 @@ def calendar(options={}, &block) end holder end - + # group all records if data is provided if options[:events] records = options[:events].inject({}) do |memo, record| - unless record.send(options[:field]).nil? - stamp = record.send(options[:field]).to_date.to_s(:number) + field = record.send(options[:field]) + + if field + stamp = field.to_date.to_s(:number) memo[stamp] ||= [] memo[stamp] << record - end - memo - end + end + + memo + end end - + # building the calendar contents = content_tag(:table, :id => options[:id], :class => 'calendar') do # first, get the header today = Date.today date = today.beginning_of_week date = date - 1.day if RUBY_PLATFORM =~ /darwin/ - + caption = content_tag(:caption, l(Date.new(options[:year], options[:month]), :format => options[:caption_format])) - + head = content_tag(:thead) do content_tag(:tr) do (0..6).collect { |i| content_tag(:th, l(date + i.days, :format => options[:header_format])) } * "" end end - + # then get the body rows = "" days.in_groups_of(7, "") do |group| @@ -69,23 +72,23 @@ def calendar(options={}, &block) group.inject("") do |cols, day| col_options = {:class => 'day'} events = "" - + unless day.blank? date = Date.new(options[:year], options[:month], day.to_i) col_options[:class] << ' today' if today == date col_options[:class] << ' weekend' if [0,6].include?(date.wday) end - + if block_given? && !day.blank? if options[:events] events = capture(date, records[date.to_s(:number)], &block) else events = capture(date, &block) end - + col_options[:class] << ' events' unless events.blank? end - + cols << content_tag(:td, col_options) do day = options[:today] if options[:today] && date == today span = content_tag(:span, day) @@ -94,12 +97,12 @@ def calendar(options={}, &block) end end end - + caption + head + rows end - - concat(contents) if block_given? - + + concat(contents) if block_given? + contents end end