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
This commit is contained in:
parent
99992750bb
commit
ef9ba3b981
2 changed files with 30 additions and 9 deletions
|
|
@ -165,17 +165,16 @@ module ApplicationHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def show_time(length)
|
def show_time(length)
|
||||||
h = length / 60
|
return '0 h 0 min' if length.blank?
|
||||||
min = length - h * 60
|
|
||||||
|
|
||||||
if h != 0
|
h, min = length.divmod(60)
|
||||||
if min != 0
|
|
||||||
"#{h} h #{min} min"
|
if h == 0
|
||||||
else
|
"#{min.round} min"
|
||||||
"#{h} h"
|
elsif min == 0
|
||||||
end
|
"#{h} h"
|
||||||
else
|
else
|
||||||
"#{min} min"
|
"#{h} h #{min.round} min"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,28 @@ describe ApplicationHelper, type: :helper do
|
||||||
end
|
end
|
||||||
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
|
describe 'show_roles' do
|
||||||
it 'formats the hash passed' do
|
it 'formats the hash passed' do
|
||||||
roles = { 'organizer' => ['oSC16', 'oSC15'], 'cfp' => ['oSC16'] }
|
roles = { 'organizer' => ['oSC16', 'oSC15'], 'cfp' => ['oSC16'] }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue