Reputation: 1331
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!
Upvotes: 2
Views: 3751
Reputation: 915
I've managed to make a pull request into rails and now you can pass symbol argument to beginning_of_week
method. For example beginning_of_week(:sunday)
will give you a Sunday assuming that week starts on Sunday. The same for end_of_week
method. But you have to wait until rails 3.2 release in case you are not on the bleeding edge.
See this for more info: https://github.com/rails/rails/pull/3547
UPDATE: Now I'm waiting for the new PR to be accepted, it makes possible to set default week start in your rails app config. See this for more info https://github.com/rails/rails/pull/5339
UPDATE:
Merged!
rafaelfranca merged commit 5428de1 into rails:master from gregolsen:week_start_config 4 months ago
Closed
rafaelfranca closed the pull request 4 months ag
o
Upvotes: 10
Reputation: 6967
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
Upvotes: 4
Reputation: 44080
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
Upvotes: 2