From 529b8eb71023fde621effee07f967ba29c09cb00 Mon Sep 17 00:00:00 2001 From: divyanshumehta Date: Sat, 11 Feb 2017 16:45:27 +0530 Subject: [PATCH 1/3] Solved Issue 1259 --- app/controllers/conferences_controller.rb | 7 ++++++- app/models/ability.rb | 2 +- config/routes.rb | 5 ++++- lib/root_route_constraint.rb | 10 ++++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 lib/root_route_constraint.rb diff --git a/app/controllers/conferences_controller.rb b/app/controllers/conferences_controller.rb index 53e5bd6d..5a502f94 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,11 @@ class ConferencesController < ApplicationController def show; end + def current + current = Conference.first + 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 14913e75..ca979c94 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 9fa1c29a..3e88876e 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' @@ -147,6 +148,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..197fdffc --- /dev/null +++ b/lib/root_route_constraint.rb @@ -0,0 +1,10 @@ +class RootRouteConstraint + def initialize + @current = Conference.where('end_date >= ?', Date.current).reorder(start_date: :asc) + end + + def matches?(*) + return unless @current.present? && @current.first.splashpage.present? + @current.count == 1 && @current.first.splashpage.public? + end +end From ee7684d84524855260ea65f0ce32ef2c6a691e60 Mon Sep 17 00:00:00 2001 From: divyanshumehta Date: Sun, 19 Mar 2017 09:17:31 +0530 Subject: [PATCH 2/3] Added test for RootRouteConstraint and conferences#current --- app/controllers/conferences_controller.rb | 2 +- config/routes.rb | 2 +- lib/root_route_constraint.rb | 15 +++++-- .../conferences_controller_spec.rb | 7 ++++ spec/lib/root_route_constraint_spec.rb | 39 +++++++++++++++++++ 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 spec/lib/root_route_constraint_spec.rb 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 From d425349bdd209840cd78a0c60cba7b72bd3e7aa2 Mon Sep 17 00:00:00 2001 From: divyanshumehta Date: Sun, 16 Apr 2017 10:42:46 +0530 Subject: [PATCH 3/3] Redirect home to conference#show if only one conference live tommorow --- app/controllers/conferences_controller.rb | 1 + lib/root_route_constraint.rb | 9 +++------ spec/lib/root_route_constraint_spec.rb | 1 - 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/app/controllers/conferences_controller.rb b/app/controllers/conferences_controller.rb index a9717296..dcb38eeb 100644 --- a/app/controllers/conferences_controller.rb +++ b/app/controllers/conferences_controller.rb @@ -13,6 +13,7 @@ class ConferencesController < ApplicationController 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 diff --git a/lib/root_route_constraint.rb b/lib/root_route_constraint.rb index c957a18d..5aee9b7c 100644 --- a/lib/root_route_constraint.rb +++ b/lib/root_route_constraint.rb @@ -1,6 +1,7 @@ class RootRouteConstraint def initialize - @current = Conference.where('start_date <= ? AND end_date >= ?', Date.current, Date.current).reorder(start_date: :asc) + @current = Conference.where('start_date <= ? AND end_date >= ?', Date.current, Date.current) + @current = Conference.where('start_date = ?', Date.current + 1) if @current.blank? end ## @@ -10,10 +11,6 @@ class RootRouteConstraint # * +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?(*) - if @current.present? && @current.first.splashpage.present? - @current.count == 1 && @current.first.splashpage.public? - else - false - end + @current.present? && @current.first.splashpage.present? && @current.count == 1 && @current.first.splashpage.public? end end diff --git a/spec/lib/root_route_constraint_spec.rb b/spec/lib/root_route_constraint_spec.rb index 338dbea0..ed9a5ac6 100644 --- a/spec/lib/root_route_constraint_spec.rb +++ b/spec/lib/root_route_constraint_spec.rb @@ -34,6 +34,5 @@ describe RootRouteConstraint do constraint = RootRouteConstraint.new expect(constraint.matches?).to eq false end - end end