Robby on Rails: DRY(a): Year After Yearthoughts.sort_by{|t| t[:topic]}.collect tag:www.robbyonrails.com,2005:TypoTypo2008-03-24T23:40:24-04:00Robby Russellurn:uuid:4228a063-facc-4a13-bdb0-342c0fab415e2008-03-24T22:05:00-04:002008-03-24T23:40:24-04:00DRY(a): Year After Year<p>I’m guilty of it. Many of you are likely guilty of it… and I know that several customers of our <a href="http://www.robbyonrails.com/articles/2007/06/17/audit-your-rails-development-team">Rails Code Audit and Review service</a> are guilty of it.</p>
<p>How many times have you realized (after a few months has passed) that your Copyright date/year on your web site was no longer current?</p>
<p>How many of you had the same problem last year? The year before?</p>
<p>Let me share some advice with you all… <span class="caps">DRY</span> (a)!</p>
<p><strong>Don’t Repeat Yourself (again)!</strong></p>
<p>This is really a simple problem to fix but when we’re busy tackling bigger problems… little things like this slip by. Don’t worry, you’re not the only one who was reminded by a colleague three months into the year that you forgot to update this.</p>
<p>On client projects, we have a handful of helpers that we drop into the application. We’re starting to extract more of these into plugins and will be releasing those as time permits. It just happened that I found myself looking at yet-another Rails code base this afternoon that was showing 2007 in the footer. An easily forgivable offense.. but if you’re going to go in there and change it (again), <em>take a moment to do the right thing</em>. ;-)</p>
<p>Our solution at <a href="http://planetargon.com">Planet Argon</a> on client projects is to create a basic view helper that renders the current year. This allows us to do the following.</p>
<pre><code>
<div id="footer">
&copy; Copyright <%= current_year -%>. All Rights Reserved.
</div>
</code></pre>
<p>The helper code looks like:</p>
<pre><code>
# add to application_helper.rb
module ApplicationHelper
def current_year
Time.now.strftime('%Y')
end
end
</code></pre>
<p>Voila. Not rocket science.. is it?</p>
<p>Guess what? I’m getting really tired of adding this to every Rails project that I touch. So, I bottled this little gem into a new Rails plugin that we’ll just add to future projects.</p>
<h2>Introducing Year after Year</h2>
<p>This is really the smallest plugin that I could put together (and it includes specs!)</p>
<p><strong>What does it provide you?</strong></p>
<p>YearAfterYear will provide you a helper that will render the current year (dynamically)! That’s right… just add the plugin to your Rails application and you too can enjoy New Years 2009 without having to have a deployment ready with a one line change from 2008 to 2009!</p>
<p>To use.. add the following to any view from within Ruby on Rails.</p>
<pre><code>
<%= current_year -%>
</code></pre>
<h3>Installation</h3>
<p>As I’m using git, you’ll need to grab this and put it into <code>vendor/plugins</code>. That’s it!</p>
<p>You can grab it on <a href="http://github.com">GitHub</a>!</p>
<ul>
<li><a href="http://github.com/robbyrussell/year_after_year/">http://github.com/robbyrussell/year_after_year/</a></li>
<li>Bugs / Feature Requests <a href="http://planetargon.lighthouseapp.com/projects/5187-open-source-projects/tickets">here</a></li>
</ul>
<p>Happy New Years (8+ months early)!</p>
<p>Just a friendly reminder to not forget the small stuff… because your visitors will notice! ;-)</p>
<h3>Updates…</h3>
<p>I got a few requests for this to also provide a range of years for people who like to do: <strong>2005-2007</strong>. So this is now provided as well.</p>
<p><code>year_range(start_year)</code></p>
Example:
<pre><code>
<%= year_range(2005) %> # => 2005-2008
</code></pre><p>I’m guilty of it. Many of you are likely guilty of it… and I know that several customers of our <a href="http://www.robbyonrails.com/articles/2007/06/17/audit-your-rails-development-team">Rails Code Audit and Review service</a> are guilty of it.</p>
<p>How many times have you realized (after a few months has passed) that your Copyright date/year on your web site was no longer current?</p>
<p>How many of you had the same problem last year? The year before?</p>
<p>Let me share some advice with you all… <span class="caps">DRY</span> (a)!</p>
<p><strong>Don’t Repeat Yourself (again)!</strong></p>
<p>This is really a simple problem to fix but when we’re busy tackling bigger problems… little things like this slip by. Don’t worry, you’re not the only one who was reminded by a colleague three months into the year that you forgot to update this.</p>
<p>On client projects, we have a handful of helpers that we drop into the application. We’re starting to extract more of these into plugins and will be releasing those as time permits. It just happened that I found myself looking at yet-another Rails code base this afternoon that was showing 2007 in the footer. An easily forgivable offense.. but if you’re going to go in there and change it (again), <em>take a moment to do the right thing</em>. ;-)</p>
<p>Our solution at <a href="http://planetargon.com">Planet Argon</a> on client projects is to create a basic view helper that renders the current year. This allows us to do the following.</p>
<pre><code>
<div id="footer">
&copy; Copyright <%= current_year -%>. All Rights Reserved.
</div>
</code></pre>
<p>The helper code looks like:</p>
<pre><code>
# add to application_helper.rb
module ApplicationHelper
def current_year
Time.now.strftime('%Y')
end
end
</code></pre>
<p>Voila. Not rocket science.. is it?</p>
<p>Guess what? I’m getting really tired of adding this to every Rails project that I touch. So, I bottled this little gem into a new Rails plugin that we’ll just add to future projects.</p>
<h2>Introducing Year after Year</h2>
<p>This is really the smallest plugin that I could put together (and it includes specs!)</p>
<p><strong>What does it provide you?</strong></p>
<p>YearAfterYear will provide you a helper that will render the current year (dynamically)! That’s right… just add the plugin to your Rails application and you too can enjoy New Years 2009 without having to have a deployment ready with a one line change from 2008 to 2009!</p>
<p>To use.. add the following to any view from within Ruby on Rails.</p>
<pre><code>
<%= current_year -%>
</code></pre>
<h3>Installation</h3>
<p>As I’m using git, you’ll need to grab this and put it into <code>vendor/plugins</code>. That’s it!</p>
<p>You can grab it on <a href="http://github.com">GitHub</a>!</p>
<ul>
<li><a href="http://github.com/robbyrussell/year_after_year/">http://github.com/robbyrussell/year_after_year/</a></li>
<li>Bugs / Feature Requests <a href="http://planetargon.lighthouseapp.com/projects/5187-open-source-projects/tickets">here</a></li>
</ul>
<p>Happy New Years (8+ months early)!</p>
<p>Just a friendly reminder to not forget the small stuff… because your visitors will notice! ;-)</p>
<h3>Updates…</h3>
<p>I got a few requests for this to also provide a range of years for people who like to do: <strong>2005-2007</strong>. So this is now provided as well.</p>
<p><code>year_range(start_year)</code></p>
Example:
<pre><code>
<%= year_range(2005) %> # => 2005-2008
</code></pre>