diff --git a/app/controllers/conferences_controller.rb b/app/controllers/conferences_controller.rb index 53e5bd6d..dcb38eeb 100644 --- a/app/controllers/conferences_controller.rb +++ b/app/controllers/conferences_controller.rb @@ -2,7 +2,7 @@ class ConferencesController < ApplicationController protect_from_forgery with: :null_session before_action :respond_to_options load_and_authorize_resource find_by: :short_title - load_resource :program, through: :conference, singleton: true, except: :index + load_resource :program, through: :conference, singleton: true, except: [:index, :current] def index @current = Conference.where('end_date >= ?', Date.current).reorder(start_date: :asc) @@ -11,6 +11,12 @@ class ConferencesController < ApplicationController def show; end + def current + current = Conference.where('start_date <= ? AND end_date >= ?', Date.current, Date.current).first + current = Conference.where('start_date = ?', Date.current + 1).first if current.blank? + redirect_to conference_path(current.short_title) + end + private def respond_to_options diff --git a/app/models/ability.rb b/app/models/ability.rb index 6fa8c12f..58ced4c2 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -29,7 +29,7 @@ class Ability # Abilities for not signed in users (guests) def not_signed_in - can [:index], Conference + can [:index, :current], Conference can [:show], Conference do |conference| conference.splashpage && conference.splashpage.public == true end diff --git a/config/routes.rb b/config/routes.rb index ae67a820..42d527de 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,4 @@ +require 'root_route_constraint' Osem::Application.routes.draw do if ENV['OSEM_ICHAIN_ENABLED'] == 'true' @@ -152,6 +153,8 @@ Osem::Application.routes.draw do end get '/admin' => redirect('/admin/conferences') - + constraints RootRouteConstraint.new do + get '/' => 'conferences#current' + end root to: 'conferences#index', via: [:get, :options] end diff --git a/lib/root_route_constraint.rb b/lib/root_route_constraint.rb new file mode 100644 index 00000000..5aee9b7c --- /dev/null +++ b/lib/root_route_constraint.rb @@ -0,0 +1,16 @@ +class RootRouteConstraint + def initialize + @current = Conference.where('start_date <= ? AND end_date >= ?', Date.current, Date.current) + @current = Conference.where('start_date = ?', Date.current + 1) if @current.blank? + 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?(*) + @current.present? && @current.first.splashpage.present? && @current.count == 1 && @current.first.splashpage.public? + 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..ed9a5ac6 --- /dev/null +++ b/spec/lib/root_route_constraint_spec.rb @@ -0,0 +1,38 @@ +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