From 3802a1e2d24e76b94758e4abfc0fe28dc0b6a884 Mon Sep 17 00:00:00 2001 From: "brunocostalopes@gmail.com" Date: Thu, 11 Jul 2013 22:12:25 +0100 Subject: [PATCH 1/5] Option to show week numbers (:show_week_numbers => true). Weeks column header title can be set using :weeks_column_name --- lib/later_dude/calendar.rb | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/later_dude/calendar.rb b/lib/later_dude/calendar.rb index 7277898..b4d613f 100644 --- a/lib/later_dude/calendar.rb +++ b/lib/later_dude/calendar.rb @@ -39,7 +39,7 @@ def initialize(year, month, options={}, &block) def to_html(&block) @block = block if block_given? content_tag(:table, :class => "#{@options[:calendar_class]}") do - content_tag(:thead, "#{show_month_names}#{show_day_names}".html_safe) + content_tag(:tbody, show_days) + content_tag(:thead, "#{show_week_headers}#{show_month_names}#{show_day_names}".html_safe) + content_tag(:tbody, show_days) end end @@ -53,7 +53,7 @@ def last_rendered_date private def show_days - content_tag(:tr, "#{show_previous_month}#{show_current_month}#{show_following_month}".html_safe) + content_tag(:tr, "#{show_week_number}#{show_previous_month}#{show_current_month}#{show_following_month}".html_safe) end def show_previous_month @@ -99,12 +99,26 @@ def show_day(day) content = content_tag(:td, content.to_s.html_safe, options) - # close table row at the end of a week and start a new one - # opening and closing tag for the first and last week are included in #show_days - content << "".html_safe if day < @days.last && day.wday == last_day_of_week + if day < @days.last && day.wday == last_day_of_week + # close table row at the end of a week and start a new one + # opening and closing tag for the first and last week are included in #show_days + content << "".html_safe + # add week number at the beginning of next row + content << show_week_number(day.next) + end content end + # if the day argument is not passed then we are showing the first week + def show_week_number(day = @days.first) + return if !@options[:show_week_numbers] + + # this considers only weeks starting on sunday or monday + # todo: week number calculation for all other scenarios + week = @options[:first_day_of_week] == 0 ? (day+1).cweek : day.cweek + content_tag(:th, week, :scope => "row", :class => "week_number") + end + def beginning_of_week(day) diff = day.wday - first_day_of_week diff += 7 if first_day_of_week > day.wday # hackish ;-) @@ -117,6 +131,17 @@ def show_month_names content_tag(:tr, "#{previous_month}#{current_month}#{next_month}".html_safe, :class => 'month_names') end + def show_week_headers + rows = 3 + rows -= 1 if @options[:hide_month_name] + rows -= 1 if @options[:hide_day_names] + + content_tag(:tr, :class => "week_header") do + content_tag(:th, @options[:weeks_column_name].html_safe, :scope => "col", :rowspan => rows) + + content_tag(:th, "", :colspan => "7") + end + end + # @options[:previous_month] can either be a single value or an array containing two values. For a single value, the # value can either be a strftime compatible string or a proc. # For an array, the first value is considered to be a strftime compatible string and the second is considered to be @@ -207,6 +232,8 @@ def default_calendar_options :first_day_of_week => I18n.translate(:'date.first_day_of_week', :default => "0").to_i, :hide_day_names => false, :hide_month_name => false, + :show_week_numbers => false, + :weeks_column_name => "Week", :use_full_day_names => false, :current_month => I18n.translate(:'date.formats.calendar_header', :default => "%B"), :next_month => false, From 2e939da79a8b7e1bf1387d10dc65b76983bc07b8 Mon Sep 17 00:00:00 2001 From: "brunocostalopes@gmail.com" Date: Thu, 11 Jul 2013 22:32:11 +0100 Subject: [PATCH 2/5] week header bug: hide when week numbers are not shown on the calendar --- lib/later_dude/calendar.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/later_dude/calendar.rb b/lib/later_dude/calendar.rb index b4d613f..2a1e11c 100644 --- a/lib/later_dude/calendar.rb +++ b/lib/later_dude/calendar.rb @@ -132,6 +132,7 @@ def show_month_names end def show_week_headers + return if !@options[:show_week_numbers] rows = 3 rows -= 1 if @options[:hide_month_name] rows -= 1 if @options[:hide_day_names] From 3fde25505bc770d740371d4eb7808a9fe1b29872 Mon Sep 17 00:00:00 2001 From: "brunocostalopes@gmail.com" Date: Sun, 14 Jul 2013 21:40:56 +0100 Subject: [PATCH 3/5] Updated the README file --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 593d642..69d4908 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ Possible options are: * :first_day_of_week: The first day of the week (0 = Sunday, 1 = Monday etc.). Defaults to the :'date.first_day_of_week' translation in the respective locale or 0 if no translation can be found. * :hide_day_names: Hide the table header showing day names. Defaults to false. * :hide_month_name: Hide the table header showing the month name. Defaults to false. +* :show_week_numbers: Shows the week numbers on the left of the calendar. Defaults to false. Week numbers are calculated considering Monday as the first day of week except if the :first_day_of_week is set to 0 (Sunday). * :use_full_day_names: Use full instead of abbreviated day names. Defaults to false. * :use_full_month_names: Use full instead of abbreviated month names. Defaults to true. * :yield_surrounding_days: Defines whether or not days or the previous and next month are yielded to the passed block. Defaults to false. @@ -124,6 +125,8 @@ You can do all kinds of mighty stuff with a proc. Imagine a situation where you The option :next_and_previous_month can be used as a shortcut if you want to use the same formatting for the next and previous month. Both, :next_month and :previous_month override the :next_and_previous_month if given. +An alternative description for the week header can be provided by using the option :weeks_column_name. This defaults to "Week". + i18n ==== From aa21efccb806f21c0592a69ab7cf94003b486f7b Mon Sep 17 00:00:00 2001 From: "brunocostalopes@gmail.com" Date: Sun, 14 Jul 2013 21:52:03 +0100 Subject: [PATCH 4/5] set the week header cell to be assigned to the CSS "week" class --- lib/later_dude/calendar.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/later_dude/calendar.rb b/lib/later_dude/calendar.rb index 2a1e11c..1bf33d1 100644 --- a/lib/later_dude/calendar.rb +++ b/lib/later_dude/calendar.rb @@ -138,7 +138,7 @@ def show_week_headers rows -= 1 if @options[:hide_day_names] content_tag(:tr, :class => "week_header") do - content_tag(:th, @options[:weeks_column_name].html_safe, :scope => "col", :rowspan => rows) + + content_tag(:th, @options[:weeks_column_name].html_safe, :scope => "col", :rowspan => rows, :class => "week") + content_tag(:th, "", :colspan => "7") end end From 785fe3af4179c4cbb989dc52b319ac8af37e7c94 Mon Sep 17 00:00:00 2001 From: "brunocostalopes@gmail.com" Date: Mon, 15 Jul 2013 21:43:27 +0100 Subject: [PATCH 5/5] Added 'active_support/core_ext' to be able to run the tests that use the reverse_merge method --- test/test_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_helper.rb b/test/test_helper.rb index 927a2ed..22ab69a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,5 +1,6 @@ require 'test/unit' require 'active_support/test_case' +require 'active_support/core_ext' require 'mocha' require 'later_dude'