diff --git a/lib/groupdate.rb b/lib/groupdate.rb index cfab053..bca3bad 100644 --- a/lib/groupdate.rb +++ b/lib/groupdate.rb @@ -21,3 +21,12 @@ module ActiveRecord end end + +module Groupdate + mattr_accessor :week_starts_with + @@week_starts_with = :sun + + def self.configure + yield self + end +end diff --git a/lib/groupdate/scopes.rb b/lib/groupdate/scopes.rb index b856c23..749e2e9 100644 --- a/lib/groupdate/scopes.rb +++ b/lib/groupdate/scopes.rb @@ -19,7 +19,7 @@ module Groupdate end # for week - week_start = [:mon, :tue, :wed, :thu, :fri, :sat, :sun].index((options[:start] || :sun).to_sym) + week_start = [:mon, :tue, :wed, :thu, :fri, :sat, :sun].index((options[:start] || Groupdate.week_starts_with).to_sym) if field == "week" and !week_start raise "Unrecognized :start option" end diff --git a/test/test_helper.rb b/test/test_helper.rb index 6eec017..a8fc0f6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -32,6 +32,8 @@ end module TestGroupdate + @@default_config = Hash[Groupdate.class_variables.collect { |opt| [opt[2..-1].to_sym, Groupdate.class_variable_get(opt)] }] + # second def test_second_end_of_second @@ -134,6 +136,32 @@ module TestGroupdate assert_result_time :week, "2013-03-16 00:00:00 PDT", "2013-03-16 07:00:00", true, :start => :sat end + # config week starting key + + def test_week_start_of_week_mon_from_config + with_config :week_starts_with => :mon do + assert_result_time :week, "2013-03-25 00:00:00 UTC", "2013-03-25 00:00:00", false + end + end + + def test_week_end_of_week_mon_from_config + with_config :week_starts_with => :mon do + assert_result_time :week, "2013-03-18 00:00:00 UTC", "2013-03-24 23:59:59", false + end + end + + def test_week_end_of_week_with_time_zone_mon_from_config + with_config :week_starts_with => :mon do + assert_result_time :week, "2013-03-11 00:00:00 PDT", "2013-03-18 06:59:59", true + end + end + + def test_week_start_of_week_with_time_zone_mon_from_config + with_config :week_starts_with => :mon do + assert_result_time :week, "2013-03-18 00:00:00 PDT", "2013-03-18 07:00:00", true + end + end + # month def test_month_end_of_month @@ -372,4 +400,19 @@ module TestGroupdate User.delete_all end + def with_config(config) + setup_config config + yield + ensure + setup_config @@default_config + end + +private + + def setup_config(config) + config.each do |option, value| + Groupdate.send "#{option}=", value + end + end + end -- libgit2 0.21.0