Skip to navigation

Thu Mar 27th

Copyright Rails Helper

# Pretty formatting of the copyright for the footer of your pages
module CopyrightHelper
  def copyright(year, now=Time.now)
    if now.year == year
      year.to_s
    elsif year / 1000 == now.year / 1000 # same century
      year.to_s + "–" + now.year.to_s[-2..3]
    else
      year.to_s + "–" + now.year.to_s
    end
  end
end

if $0 == __FILE__
  require 'test/unit'
  class CopyrightHelperTest < Test::Unit::TestCase
    include CopyrightHelper
    
    def test_should_return_year_if_copyright_year_is_the_same
      assert_equal "2000", copyright(2000, Time.local(2000))
    end
    def test_should_return_year_range_minus_century_if_the_same_century
      assert_equal "2000&ndash;01", copyright(2000, Time.local(2001))
    end
    def test_should_return_full_year_range_if_different_century
      assert_equal "1999&ndash;2000", copyright(1999, Time.local(2000))
    end
  end
end
Page 1 of 1

Tumble

toolmantim

I'm Tim Lucas, a web app designer and developer currently in Sydney Australia.

I run a web application design and development company Agency Rainford, present on various technical topics, snap the occasional photo, tweet my going-ons, share teh codes and post other tid-bits to the tumble.

Shoot an email to and say hello.

Powered by tumblr.com