Verified Commit fa3a2d6a authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻
Browse files

convert format_hours_period helper to format_hours_semester helper

- and also display "1. Semester 2018 - 2. Semester 2019"
parent a4510c71
......@@ -8,10 +8,35 @@ module FormatHelper
pluralize hours, 'Stunde', 'Stunden'
end
def format_hours_period(hours)
def format_hours_semester(hours)
return '' if hours.blank?
dates = hours.map(&:meeting_date)
"#{I18n.l dates.min} - #{I18n.l dates.max}"
if dates.size == 1
"#{BillingExpense.semester_of_year(dates.first)}. Semester #{semester_display_year(dates.first)}"
else
format_hours_multiple_dates_semester(dates)
end
end
def format_hours_multiple_dates_semester(dates)
min_date = dates.min
max_date = dates.max
if semester_display_year(min_date) != semester_display_year(max_date)
"#{BillingExpense.semester_of_year(min_date)}. Semester #{semester_display_year(min_date)} - "\
"#{BillingExpense.semester_of_year(max_date)}. Semester #{max_date.year}"
elsif BillingExpense.semester_of_year(min_date) == BillingExpense.semester_of_year(max_date)
"#{BillingExpense.semester_of_year(max_date)}. Semester #{semester_display_year(max_date)}"
else
"#{BillingExpense.semester_of_year(min_date)}. - "\
"#{BillingExpense.semester_of_year(max_date)}. Semester #{semester_display_year(max_date)}"
end
end
def semester_display_year(date)
if date.month == 12
date.year + 1
else
date.year
end
end
end
......@@ -14,10 +14,21 @@ class FormatHelperTest < ActionView::TestCase
end
test 'format_hours_period' do
assert_equal '01.02.2013 - 03.04.2015', format_hours_period([
assert_equal '1. Semester 2013 - 1. Semester 2015', format_hours_semester([
create(:hour, meeting_date: '2015-04-03'),
create(:hour, meeting_date: '2014-05-06'),
create(:hour, meeting_date: '2013-02-01')
create(:hour, meeting_date: '2012-12-01')
])
assert_equal '1. - 2. Semester 2015', format_hours_semester([
create(:hour, meeting_date: '2015-04-03'),
create(:hour, meeting_date: '2015-09-06')
])
assert_equal '1. Semester 2015', format_hours_semester([
create(:hour, meeting_date: '2015-04-03'),
create(:hour, meeting_date: '2015-04-06')
])
assert_equal '1. Semester 2015', format_hours_semester([
create(:hour, meeting_date: '2015-04-03')
])
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment