Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Week Numbers #13

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
====

Expand Down
38 changes: 33 additions & 5 deletions lib/later_dude/calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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 << "</tr><tr>".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 << "</tr><tr>".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 ;-)
Expand All @@ -117,6 +131,18 @@ def show_month_names
content_tag(:tr, "#{previous_month}#{current_month}#{next_month}".html_safe, :class => '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]

content_tag(:tr, :class => "week_header") do
content_tag(:th, @options[:weeks_column_name].html_safe, :scope => "col", :rowspan => rows, :class => "week") +
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
Expand Down Expand Up @@ -207,6 +233,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,
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'test/unit'
require 'active_support/test_case'
require 'active_support/core_ext'
require 'mocha'

require 'later_dude'