Commit c1caf8d1320cac09498c26d71834de60b15caf74

Authored by Sergey Kuchmistov
1 parent 779a07bd

add week_starts_with global option

lib/groupdate.rb
... ... @@ -21,3 +21,12 @@ module ActiveRecord
21 21  
22 22 end
23 23 end
  24 +
  25 +module Groupdate
  26 + mattr_accessor :week_starts_with
  27 + @@week_starts_with = :sun
  28 +
  29 + def self.configure
  30 + yield self
  31 + end
  32 +end
... ...
lib/groupdate/scopes.rb
... ... @@ -19,7 +19,7 @@ module Groupdate
19 19 end
20 20  
21 21 # for week
22   - week_start = [:mon, :tue, :wed, :thu, :fri, :sat, :sun].index((options[:start] || :sun).to_sym)
  22 + week_start = [:mon, :tue, :wed, :thu, :fri, :sat, :sun].index((options[:start] || Groupdate.week_starts_with).to_sym)
23 23 if field == "week" and !week_start
24 24 raise "Unrecognized :start option"
25 25 end
... ...
test/test_helper.rb
... ... @@ -32,6 +32,8 @@ end
32 32  
33 33 module TestGroupdate
34 34  
  35 + @@default_config = Hash[Groupdate.class_variables.collect { |opt| [opt[2..-1].to_sym, Groupdate.class_variable_get(opt)] }]
  36 +
35 37 # second
36 38  
37 39 def test_second_end_of_second
... ... @@ -134,6 +136,32 @@ module TestGroupdate
134 136 assert_result_time :week, "2013-03-16 00:00:00 PDT", "2013-03-16 07:00:00", true, :start => :sat
135 137 end
136 138  
  139 + # config week starting key
  140 +
  141 + def test_week_start_of_week_mon_from_config
  142 + with_config :week_starts_with => :mon do
  143 + assert_result_time :week, "2013-03-25 00:00:00 UTC", "2013-03-25 00:00:00", false
  144 + end
  145 + end
  146 +
  147 + def test_week_end_of_week_mon_from_config
  148 + with_config :week_starts_with => :mon do
  149 + assert_result_time :week, "2013-03-18 00:00:00 UTC", "2013-03-24 23:59:59", false
  150 + end
  151 + end
  152 +
  153 + def test_week_end_of_week_with_time_zone_mon_from_config
  154 + with_config :week_starts_with => :mon do
  155 + assert_result_time :week, "2013-03-11 00:00:00 PDT", "2013-03-18 06:59:59", true
  156 + end
  157 + end
  158 +
  159 + def test_week_start_of_week_with_time_zone_mon_from_config
  160 + with_config :week_starts_with => :mon do
  161 + assert_result_time :week, "2013-03-18 00:00:00 PDT", "2013-03-18 07:00:00", true
  162 + end
  163 + end
  164 +
137 165 # month
138 166  
139 167 def test_month_end_of_month
... ... @@ -372,4 +400,19 @@ module TestGroupdate
372 400 User.delete_all
373 401 end
374 402  
  403 + def with_config(config)
  404 + setup_config config
  405 + yield
  406 + ensure
  407 + setup_config @@default_config
  408 + end
  409 +
  410 +private
  411 +
  412 + def setup_config(config)
  413 + config.each do |option, value|
  414 + Groupdate.send "#{option}=", value
  415 + end
  416 + end
  417 +
375 418 end
... ...