How to Automate Copyright Notice Updates in RubyonRails

Suppose you want to automate the update process in a website written in RubyonRails.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 > 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):
© <%= copyright_notice_year_range(2005) %> <%= link_to “YOUR COMPANY NAME”, “COMPANY URL” %> All Rights Reserved.

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, 2013, the notice will read:

© 2005 – 2013 YOUR COMPANY, INC. All Rights Reserved.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s