Rietta
Rietta: Web Apps Where Security Matters
You are reading The Rietta Blog, a publication about the web since 2005.

How to Automate Copyright Notice Updates in Ruby on Rails

I sure hope everyone had a wonderful Christmas with their families. I am personally enjoying taking a few days off work and doing some reading. On Christmas morning, I bought a copy of Rework (Kindle edition) by Jason Fried and David Heinemeier Hansson. The “Go” Chapter alone is worth the price of admission!

One day I may share more about this book. However, today it seems like a great time to share another Ruby on Rails coding trick with you. That is how to automatically have your web apps update their copyright notices.

With only a few days remaining until the end of 2011, copyright notice update day is coming soon. That is the day that many web masters tweak the content of their page footers to reflect the new year.

However, suppose you wanted to automate the update process in a website written in Ruby on Rails. Here is one way to do it with a simple layout helper function.

In helpers/layout_helper.rb:

helpers/layout_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def copyright_notice_year_range(start_year)
  # In case the input was not a number (nil.to_i will return a 0)
  start_year = start_year.to_i

  # Get the current year from the system
  current_year = Time.new.year

  # When the current year is more recent than the start year, return a string 
  # of a range (e.g., 2010 - 2012). Alternatively, as long as the start year 
  # is reasonable, return it as a string. Otherwise, return the current year 
  # from the system.
  if current_year > start_year && start_year > 2000
    "#{start_year} - #{current_year}"
  elsif start_year > 2000
    "#{start_year}"
  else
    "#{current_year}"
  end
end

In layouts/application.html.erb (or your footer partial):

layouts/_copyright_notice.html.erb
1
  &copy; <%= copyright_notice_year_range(2010) %> <%= link_to "YOUR COMPANY, INC.", "#" %>  All Rights Reserved.

What is happening here?

The layout template calls copyright_notice_year_range, supplying the first year that the website was copyrighted as an integer. The helper returns the string that should be shown after the copyright symbol. Given the example above, on January 1, 2012, the notice will read:

© 2010 - 2012 YOUR COMPANY, INC. All Rights Reserved.

Be sure to replace the # with your company’s main website URL.

Here’s wishing everyone a happy and prosperous 2012!

Comments

Lenilson

Hi!,

Why year "2000", legal reason or rule?

Thank you

About Frank Rietta

Frank Rietta's photo

Frank Rietta is specialized in working with startups, new Internet businesses, and in developing with the Ruby on Rails platform to build scalable businesses. He is a computer scientist with a Masters in Information Security from the College of Computing at the Georgia Institute of Technology. He teaches about security topics and is a contributor to the security chapter of the 7th edition of the "Fundamentals of Database Systems" textbook published by Addison-Wesley.