diff --git a/app/controllers/conferences_controller.rb b/app/controllers/conferences_controller.rb index 5a502f94..a9717296 100644 --- a/app/controllers/conferences_controller.rb +++ b/app/controllers/conferences_controller.rb @@ -12,7 +12,7 @@ class ConferencesController < ApplicationController def show; end def current - current = Conference.first + current = Conference.where('start_date <= ? AND end_date >= ?', Date.current, Date.current).first redirect_to conference_path(current.short_title) end diff --git a/config/routes.rb b/config/routes.rb index 3e88876e..9f66c25d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,4 @@ -require "root_route_constraint" +require 'root_route_constraint' Osem::Application.routes.draw do if ENV['OSEM_ICHAIN_ENABLED'] == 'true' diff --git a/lib/root_route_constraint.rb b/lib/root_route_constraint.rb index 197fdffc..c957a18d 100644 --- a/lib/root_route_constraint.rb +++ b/lib/root_route_constraint.rb @@ -1,10 +1,19 @@ class RootRouteConstraint def initialize - @current = Conference.where('end_date >= ?', Date.current).reorder(start_date: :asc) + @current = Conference.where('start_date <= ? AND end_date >= ?', Date.current, Date.current).reorder(start_date: :asc) end + ## + # Checks if only one conference is live and has a public splashpage + # If any conference is live it checks how many live conferences are there + # ====Returns + # * +true+ -> only one conferene is live AND the conference has public splashpage + # * +false+ -> no or more than one conferences are live or the only live conferece has no public splashpage def matches?(*) - return unless @current.present? && @current.first.splashpage.present? - @current.count == 1 && @current.first.splashpage.public? + if @current.present? && @current.first.splashpage.present? + @current.count == 1 && @current.first.splashpage.public? + else + false + end end end diff --git a/spec/controllers/conferences_controller_spec.rb b/spec/controllers/conferences_controller_spec.rb index 6479eb34..40bae2b4 100644 --- a/spec/controllers/conferences_controller_spec.rb +++ b/spec/controllers/conferences_controller_spec.rb @@ -26,6 +26,13 @@ describe ConferencesController do end end + describe 'GET #current' do + it 'redirects to first conference splashpage' do + get :current + expect(response).to redirect_to(conference_path(Conference.first.short_title)) + end + end + describe 'OPTIONS #index' do it 'Response code is 200' do process :index, 'OPTIONS' diff --git a/spec/lib/root_route_constraint_spec.rb b/spec/lib/root_route_constraint_spec.rb new file mode 100644 index 00000000..338dbea0 --- /dev/null +++ b/spec/lib/root_route_constraint_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +describe RootRouteConstraint do + describe '#matches?' do + + it 'returns false, if no conference is live' do + constraint = RootRouteConstraint.new + expect(constraint.matches?).to eq false + end + + it 'returns false, when one conference is live but no splashpage' do + create(:conference) + constraint = RootRouteConstraint.new + expect(constraint.matches?).to eq false + end + + it 'returns false, when one conference is live but no public splashpage' do + conference = create(:full_conference) + conference.splashpage.public = false + conference.splashpage.save + constraint = RootRouteConstraint.new + expect(constraint.matches?).to eq false + end + + it 'returns true, when one conference is live and has public splashpage' do + create(:full_conference) + constraint = RootRouteConstraint.new + expect(constraint.matches?).to eq true + end + + it 'returns false, if more than one conference is live' do + create(:full_conference) + create(:full_conference) + constraint = RootRouteConstraint.new + expect(constraint.matches?).to eq false + end + + end +end