views:

177

answers:

2

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!

+2  A: 

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
Mladen Jablanović
again, what i want is to specify the begining of the week to Sunday for example! in stead of rails default "Monday". Thanks
amrnt
ammt - the code above will reset the start day to saturday (I think) - if you want to set it to Sunday, then just modify the line "(self.orig_wday +2) %7" to read: "(self.orig_wday +1) %7" - the difference being the "+1" part... use +3 for Friday, +4 for Thursday etc etc
stephenmurdoch
it works well in the 'irb' but when I put it in the initializers folder in the rails application it deosnt affect! Am i did sth wrong?When i try on Time.now it works and returns Saturday. But if i pick a record from the database to see beginning_of_week it returns Monday!
amrnt
Perhaps you are checking `Date#beginning_of_week`? this code fixes only `Time` class. Anyway, I said that this was just an idea and not a ready solution you can just paste in your code and expect it to work. :(
Mladen Jablanović
+2  A: 

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
Ben
Worked perfectly for me on a 2.3.5 project, what version of Rails is your project running? Also, did you put it in a file under conifg/initializers/?
Ben
Well, how to make it returns Saturday?
amrnt
It works after put it in Date module!
amrnt
Oh--looks like it's actually defined twice--in Date and Time--I would add both to avoid issues. `(self - self.wday.days).midnight` is Sunday, so `self - self.wday.days).midnight - 1.day` is Saturday, and so on.
Ben
"- 1.day" didnt make the trick!
amrnt
I'm not testing any of this--I guess it would be `(self - self.wday.days - 1.day).midnight`. I don't know, play around with it, you're on the right track.
Ben