From ef9ba3b98191a60b042f5537dbfde835fab792df Mon Sep 17 00:00:00 2001 From: charliequinn Date: Tue, 16 Aug 2016 00:44:49 +0100 Subject: [PATCH] Added specs for ApplicationHelper.show_time - refactor of code to use divmod and handle decimal number parameter - return '0 h 0 min' if length blank --- app/helpers/application_helper.rb | 17 ++++++++--------- spec/helpers/application_helper_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 8a8bf5ad..adea355d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -165,17 +165,16 @@ module ApplicationHelper end def show_time(length) - h = length / 60 - min = length - h * 60 + return '0 h 0 min' if length.blank? - if h != 0 - if min != 0 - "#{h} h #{min} min" - else - "#{h} h" - end + h, min = length.divmod(60) + + if h == 0 + "#{min.round} min" + elsif min == 0 + "#{h} h" else - "#{min} min" + "#{h} h #{min.round} min" end end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 42070572..4c4d45f9 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -15,6 +15,28 @@ describe ApplicationHelper, type: :helper do end end + describe 'show_time' do + it 'when length > 60' do + expect(show_time(67)).to eq '1 h 7 min' + end + + it 'when length = 60' do + expect(show_time(60)).to eq '1 h' + end + + it 'when length < 60' do + expect(show_time(58)).to eq '58 min' + end + + it 'when length > 60 and is a decimal number' do + expect(show_time(68.3)).to eq '1 h 8 min' + end + + it 'when length is nil' do + expect(show_time(nil)).to eq '0 h 0 min' + end + end + describe 'show_roles' do it 'formats the hash passed' do roles = { 'organizer' => ['oSC16', 'oSC15'], 'cfp' => ['oSC16'] }