Commit 87de1841 authored by Jiri Strojil's avatar Jiri Strojil

Merge branch 'fixes/semester-not-preselecting' into 'develop'

Fixes/semester not preselecting

See merge request !914
parents 2efbe9e5 fd5539c8
Pipeline #33626 passed with stage
in 44 minutes and 26 seconds
......@@ -75,7 +75,7 @@ class SemesterProcessVolunteersController < ApplicationController
if params[:semester]
@selected_semester = Semester.parse(params[:semester])
else
@selected_semester = @semester.current
@selected_semester = @semester.preselect_semester
params[:semester] = Semester.to_s(@selected_semester)
end
end
......
......@@ -16,8 +16,6 @@ class Semester
11 => 2
}.freeze
SEMESTER_OFFSET = 4.months
class << self
def parse(string)
return unless string
......@@ -92,10 +90,10 @@ class Semester
# semester_number - required if year integer
def initialize(year = nil, number = 1)
@context = if year.nil?
Time.zone.now + SEMESTER_OFFSET
Time.zone.now
else
year -= 1 if number == 1
Time.zone.local(year, (18 - number * 6), 1) + SEMESTER_OFFSET
Time.zone.local(year, (18 - number * 6), 1)
end
end
......@@ -118,6 +116,10 @@ class Semester
)
end
def preselect_semester
previous
end
# params:
# count - shift of semesters - only positive integers
def previous(count = 1)
......@@ -178,7 +180,8 @@ class Semester
def existing_collection(count = 6)
collection(count) do |semester|
next unless Semester.new.current == semester || Semester.taken_semesters.include?(semester)
s = Semester.new
next unless s.preselect_semester == semester || s.current == semester || Semester.taken_semesters.include?(semester)
[Semester.i18n_t(semester, short: false), to_s(semester)]
end
end
......
require 'test_helper'
class SemesterServiceTest < ActionView::TestCase
test '#initialize' do
subject = Semester.new
assert_equal Time.zone.now.to_date, subject.context.to_date
subject = Semester.new(2018, 2)
assert_equal time_z(2018, 6, 1), subject.context
subject = Semester.new(2018, 1)
assert_equal time_z(2017, 12, 1), subject.context
end
test '#preselect_semester' do
travel_to time_z(2018, 9, 10)
subject = Semester.new
assert_equal time_z(2017, 12, 1).to_date...time_z(2018, 5, 1).end_of_month.to_date, subject.preselect_semester
travel_to time_z(2018, 11, 30)
subject = Semester.new
assert_equal time_z(2017, 12, 1).to_date...time_z(2018, 5, 1).end_of_month.to_date, subject.preselect_semester
travel_to time_z(2017, 12, 1)
subject = Semester.new
assert_equal time_z(2017, 6, 1).to_date...time_z(2017, 11, 1).end_of_month.to_date, subject.preselect_semester
end
test '#to_s' do
travel_to time_z(2018, 6, 10)
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal '2018,2', subject.to_s
travel_to time_z(2017, 7, 10)
travel_to time_z(2017, 12, 10)
subject = Semester.new
assert_equal '2017,2', subject.to_s
assert_equal '2018,1', subject.to_s
travel_to time_z(2018, 2, 10)
subject = Semester.new
assert_equal '2018,2', subject.to_s
assert_equal '2018,1', subject.to_s
end
test '#number' do
travel_to time_z(2018, 6, 10)
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal 2, subject.number
travel_to time_z(2018, 4, 10)
subject = Semester.new
assert_equal 2, subject.number
assert_equal 1, subject.number
travel_to time_z(2017, 12, 10)
subject = Semester.new
assert_equal 1, subject.number
......@@ -36,11 +61,7 @@ class SemesterServiceTest < ActionView::TestCase
test '#current' do
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal time_z(2018, 12, 1).to_date...time_z(2019, 5, 1).end_of_month.to_date, subject.current
subject = Semester.new(2018, 2)
assert_equal time_z(2018, 6, 1).to_date...time_z(2018, 11, 1).end_of_month.to_date, subject.current
subject = Semester.new(2017, 2)
......@@ -54,52 +75,40 @@ class SemesterServiceTest < ActionView::TestCase
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal time_z(2018, 6, 1).to_date...time_z(2018, 11, 1).end_of_month.to_date, subject.previous
assert_equal time_z(2017, 12, 1).to_date...time_z(2018, 5, 1).end_of_month.to_date, subject.previous(2)
assert_equal time_z(2017, 6, 1).to_date...time_z(2017, 11, 1).end_of_month.to_date, subject.previous(3)
assert_equal time_z(2016, 12, 1).to_date...time_z(2017, 5, 1).end_of_month.to_date, subject.previous(4)
assert_equal time_z(2017, 12, 1).to_date...time_z(2018, 5, 1).end_of_month.to_date, subject.previous
assert_equal time_z(2017, 6, 1).to_date...time_z(2017, 11, 1).end_of_month.to_date, subject.previous(2)
assert_equal time_z(2016, 12, 1).to_date...time_z(2017, 5, 1).end_of_month.to_date, subject.previous(3)
end
test '#next_semester' do
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal time_z(2018, 12, 1).to_date...time_z(2019, 5, 1).end_of_month.to_date, subject.current
assert_equal time_z(2019, 6, 1).to_date...time_z(2019, 11, 1).end_of_month.to_date, subject.next_semester
assert_equal time_z(2019, 12, 1).to_date...time_z(2020, 5, 1).end_of_month.to_date, subject.next_semester(2)
assert_equal time_z(2018, 12, 1).to_date...time_z(2019, 5, 1).end_of_month.to_date, subject.next_semester
assert_equal time_z(2019, 6, 1).to_date...time_z(2019, 11, 1).end_of_month.to_date, subject.next_semester(2)
assert_equal time_z(2019, 12, 1).to_date...time_z(2020, 5, 1).end_of_month.to_date, subject.next_semester(3)
end
test '#previous_s' do
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal '2018,2', subject.previous_s
assert_equal '2018,1', subject.previous_s(2)
assert_equal '2017,2', subject.previous_s(3)
travel_to time_z(2018, 6, 10)
subject = Semester.new
assert_equal '2018,1', subject.previous_s
assert_equal '2017,2', subject.previous_s(2)
assert_equal '2017,1', subject.previous_s(3)
end
test '#next_s' do
travel_to time_z(2018, 6, 10)
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal '2019,1', subject.next_s
assert_equal '2019,2', subject.next_s(2)
assert_equal '2020,1', subject.next_s(3)
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal '2019,1', subject.to_s
assert_equal '2020,1', subject.next_s(2)
end
test '#list' do
travel_to time_z(2018, 6, 10)
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal [
......@@ -127,15 +136,6 @@ class SemesterServiceTest < ActionView::TestCase
time_z(2019, 6, 1).to_date...time_z(2019, 11, 1).end_of_month.to_date,
time_z(2019, 12, 1).to_date...time_z(2020, 5, 1).end_of_month.to_date
], subject.list(direction: :next_semester)
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal [
time_z(2018, 12, 1).to_date...time_z(2019, 5, 1).end_of_month.to_date,
time_z(2018, 6, 1).to_date...time_z(2018, 11, 1).end_of_month.to_date,
time_z(2017, 12, 1).to_date...time_z(2018, 5, 1).end_of_month.to_date,
time_z(2017, 6, 1).to_date...time_z(2017, 11, 1).end_of_month.to_date
], subject.list
end
test '#collection' do
......@@ -143,10 +143,10 @@ class SemesterServiceTest < ActionView::TestCase
subject = Semester.new
assert_equal [
collection_entry(1, 2019, time_z(2018, 12, 1).to_date, time_z(2019, 5, 31).end_of_month.to_date),
collection_entry(2, 2018, time_z(2018, 6, 1).to_date, time_z(2018, 11, 30).end_of_month.to_date),
collection_entry(1, 2018, time_z(2017, 12, 1).to_date, time_z(2018, 5, 31).end_of_month.to_date),
collection_entry(2, 2017, time_z(2017, 6, 1).to_date, time_z(2017, 11, 30).end_of_month.to_date),
collection_entry(2, 2018, time_z(2018, 6, 1).to_date, time_z(2018, 11, 1).end_of_month.to_date),
collection_entry(1, 2018, time_z(2017, 12, 1).to_date, time_z(2018, 5, 1).end_of_month.to_date),
collection_entry(2, 2017, time_z(2017, 6, 1).to_date, time_z(2017, 11, 1).end_of_month.to_date),
collection_entry(1, 2017, time_z(2016, 12, 1).to_date, time_z(2017, 5, 1).end_of_month.to_date),
], subject.collection
end
......
Markdown is supported
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