{"id":2643,"date":"2012-10-22T13:48:24","date_gmt":"2012-10-22T10:48:24","guid":{"rendered":"http:\/\/railsware.com\/blog\/?p=2643"},"modified":"2021-08-16T15:26:47","modified_gmt":"2021-08-16T12:26:47","slug":"rails-4-0-activesupport-small-improvement","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/rails-4-0-activesupport-small-improvement\/","title":{"rendered":"Rails 4.0 ActiveSupport small improvement"},"content":{"rendered":"\n<p>A few days ago my patch with the <code>beginning_of_week<\/code> config option has been merged to Rails master, so starting from Rails 4.0 we&#8217;ll gain full control over the week start day.<br><!-- more --><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Preface<\/h3>\n\n\n\n<p>Working on Interstellar, I&#8217;ve found out that<br>ActiveSupport has a lot of useful methods like <code>Date#beginning_of_week<\/code>, <code>Date#end_of_week<\/code> and many others.<br>However, there was a small thing that was quite a bit annoying:<br>all those API methods were tied to Monday as the first day of a week.<br>Not a big deal while <a href=\"https:\/\/railsware.com\/web\/\" target=\"_blank\" rel=\"noreferrer noopener\">developing web applications<\/a> targeted to European market, but working on a US oriented stuff it might be useful to set Sunday as the beginning of a week.<br>Sure, you can fix this in a couple of methods&#8230; But hey, we are open source community, right?<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">We can patch it up baby&#8230;<\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft\"><a href=\"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2012\/10\/elvis_patch_it_up.gif\"><img loading=\"lazy\" decoding=\"async\" width=\"194\" height=\"196\" src=\"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2012\/10\/elvis_patch_it_up.gif\" alt=\"Ruby On Rails development\" class=\"wp-image-2646\" title=\"Ruby On Rails development. Patch it up\"\/><\/a><\/figure><\/div>\n\n\n\n<p><br>The <a href=\"https:\/\/github.com\/rails\/rails\/pull\/3547\" target=\"_blank\" rel=\"noreferrer noopener\">first patch<\/a> I come up with was an extension that makes possible to pass week start to <code>beginning_of_week<\/code> and <code>end_of_week<\/code> methods. It was accepted and merged into Rails 3.2 relatively fast.<\/p>\n\n\n\n<p>And all was great, but there was another bunch of cool API methods: <code>next_week<\/code> and <code>prev_week<\/code>.<br>They already have a single argument: a day in next (previous) week. That said, we cannot pass week start as an argument to these guys.<br>One of the possible solutions was an application wide config option for the week start.<br>In this case, we can use <code>beginning_of_week<\/code> inside <code>next_week<\/code> and <code>prev_week<\/code> without having to pass the start of the week manually.<br>So I hit another pull request with the <code>beginning_of_week<\/code> config option.<br>It took more than 6 months to get this merged, and now I&#8217;m happy as I can be.<br>Since Rails 4.0 we&#8217;ll get full access to ActiveSupport calendaring API.<\/p>\n\n\n\n<p>Examples:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    \nTime.now.beginning_of_week.to_date          #=&gt; Mon, 22 Oct 2012\nTime.now.beginning_of_week(:sunday).to_date #=&gt; Sun, 21 Oct 2012\nDate.beginning_of_week = :thursday\nTime.now.next_week.to_date                  #=&gt; Thu, 25 Oct 2012\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Instead of epilogue<\/h3>\n\n\n\n<p>There&#8217;s a lot of stuff out there that still should be improved.<br>For example, `Date` now knows too much about `Week`. Begging for a separate class (as <a href=\"https:\/\/github.com\/rails\/rails\/pull\/5339#issuecomment-8514854k\">Jeremy stated<\/a>).<\/p>\n\n\n\n<p>Another thing is that Rails stores week days in a different way than Ruby:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Date::DAYS_INTO_WEEK[:monday] #=&gt; 0\nTime.now.monday.wday #=&gt; 1 (because Ruby stores Sunday as zero week day)\n<\/pre>\n\n\n\n<p>It&#8217;s time to contribute, so go for it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few days ago my patch with the beginning_of_week config option has been merged to Rails master, so starting from Rails 4.0 we&#8217;ll gain full control over the week start day. Preface Working on Interstellar, I&#8217;ve found out thatActiveSupport has a lot of useful methods like Date#beginning_of_week, Date#end_of_week and many others.However, there was a small&#8230;<\/p>\n","protected":false},"author":34,"featured_media":2854,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Innokenty Mihailov"],"class_list":["post-2643","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2012\/10\/elvis_patch_it_up.gif","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/2643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=2643"}],"version-history":[{"count":35,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/2643\/revisions"}],"predecessor-version":[{"id":14130,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/2643\/revisions\/14130"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/2854"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=2643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=2643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=2643"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=2643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}