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 webmasters 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:
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 > 2000In layouts/application.html.erb (or your footer partial):
"#{start_year} - #{current_year}"
elsif start_year > 2000
"#{start_year}"
else
"#{current_year}"
end
end
© <%= 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!