In an application am building, i'm trying to make the week starts with Saturday. In ruby on rails, by default, the week begins on Monday.
So If you have any trick or a patch to make it work for me!
Thanks in advance!
In an application am building, i'm trying to make the week starts with Saturday. In ruby on rails, by default, the week begins on Monday.
So If you have any trick or a patch to make it work for me!
Thanks in advance!
You can try replacing Date#wday
and Time#wday
methods with your own. I think Rails' support methods like beginning_of_week
etc. rely on wday and will work out of the box.
Here's some code, but it's definitely just an idea, neither tested nor recommended thing to do:
require 'activesupport'
#=> true
Time.now.wday
#=> 4
Time.now.beginning_of_week
#=> 2010-04-19 00:00:00 0200
class Time
alias_method :orig_wday, :wday
def wday
(self.orig_wday + 2) % 7
end
end
Time.now.wday
#=> 6
Time.now.beginning_of_week
#=> 2010-04-17 00:00:00 0200
You could throw this into an initializer to make beginning_of_week
return Sunday:
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Time #:nodoc:
module Calculations
def beginning_of_week
(self - self.wday.days).midnight
end
end
end
end
end
It may be safer however for you to define your own method and leave the stock one intact:
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Time #:nodoc:
module Calculations
def traditional_beginning_of_week
(self - self.wday.days).midnight
end
end
end
end
end