diff options
author | Gareth Rees <gareth@mysociety.org> | 2014-06-03 11:45:18 +0100 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2014-06-05 10:56:04 +0100 |
commit | dbeecf59d62bf878fc6f4fe8a976f66f19cde415 (patch) | |
tree | 002e9cf69f9fdc57dca54ae83c5ec0238096a6d0 | |
parent | 77ce9b795c62d8241b22878cf60ca688fa4b44a1 (diff) |
Add specs for DateTimeHelper#year_from_date
-rw-r--r-- | app/helpers/date_time_helper.rb | 12 | ||||
-rw-r--r-- | spec/helpers/date_time_helper_spec.rb | 16 |
2 files changed, 27 insertions, 1 deletions
diff --git a/app/helpers/date_time_helper.rb b/app/helpers/date_time_helper.rb index 831006e29..c96265006 100644 --- a/app/helpers/date_time_helper.rb +++ b/app/helpers/date_time_helper.rb @@ -57,7 +57,17 @@ module DateTimeHelper return date.strftime("%H:%M:%S").strip end + # Return the year component from a date + # + # date - a DateTime, Date or Time + # + # Examples + # + # year_from_date(Date.new(2012, 11, 21)) + # # => "2012" + # + # Returns a String def year_from_date(date) - return date.strftime("%Y").strip + date.strftime("%Y").strip end end diff --git a/spec/helpers/date_time_helper_spec.rb b/spec/helpers/date_time_helper_spec.rb index aa047052c..67d2efdd3 100644 --- a/spec/helpers/date_time_helper_spec.rb +++ b/spec/helpers/date_time_helper_spec.rb @@ -51,4 +51,20 @@ describe DateTimeHelper do end + describe :year_from_date do + + it 'returns the year component of a date' do + year_from_date(Date.new(2012, 11, 21)).should == '2012' + end + + it 'returns the year component of a datetime' do + year_from_date(DateTime.new(2012, 11, 21)).should == '2012' + end + + it 'returns the year component of a time' do + year_from_date(Time.now).should == Date.today.year.to_s + end + + end + end |