Skip to content
Browse files

Added an option to customize start day of week.

I18n section date.abbr_day_names of application
should be adjusted so that first day of week goes first
(for correct calendar header).
  • Loading branch information...
1 parent 0491b1a commit 4c6e9d386ca8f372c64802514f984124863b8435 @a-vakulenko committed Dec 13, 2012
Showing with 7 additions and 10 deletions.
  1. +1 −5 README.md
  2. +6 −5 lib/simple_calendar/view_helpers.rb
View
6 README.md
@@ -91,6 +91,7 @@ Possible options:
:month # current month, default: from params or current month
:prev_text # previous month link text, default: «
:next_text # next month link text, default: »
+ :start_day # starting day of week, default: :sunday
CSS
---
@@ -103,8 +104,3 @@ the following line to your css:
By default simple_calendar will set the calendar to use .bordered-table
and .calendar classes.
-
-TODO
-====
-
-* Customizable starting day of week
View
11 lib/simple_calendar/view_helpers.rb
@@ -9,26 +9,27 @@ def calendar(events, options={}, &block)
:year => (params[:year] || Time.zone.now.year).to_i,
:month => (params[:month] || Time.zone.now.month).to_i,
:prev_text => raw("«"),
- :next_text => raw("»")
+ :next_text => raw("»"),
+ :start_day => :sunday
}
options.reverse_merge! opts
events ||= []
selected_month = Date.civil(options[:year], options[:month])
current_date = Date.today
- range = build_range selected_month
+ range = build_range selected_month, options
month_array = build_month range
draw_calendar(selected_month, month_array, current_date, events, options, block)
end
private
- def build_range(selected_month)
+ def build_range(selected_month, options)
start_date = selected_month.beginning_of_month
- start_date = start_date.sunday? ? start_date : start_date.beginning_of_week(:sunday)
+ start_date = start_date.send(options[:start_day].to_s+'?') ? start_date : start_date.beginning_of_week(options[:start_day])
end_date = selected_month.end_of_month
- end_date = end_date.saturday? ? end_date : end_date.end_of_week(:sunday)
+ end_date = end_date.saturday? ? end_date : end_date.end_of_week(options[:start_day])
(start_date..end_date).to_a
end

0 comments on commit 4c6e9d3

Please sign in to comment.
Something went wrong with that request. Please try again.