diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index e5b3e5538..4444d638a 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -32,13 +32,22 @@ def showcase end def set_featured_trainer - return nil unless TeSS::Config.site.dig('home_page', 'featured_trainer') + featured_trainer = TeSS::Config.site.dig('home_page', 'featured_trainer') + return nil if featured_trainer.blank? || featured_trainer == false + # featured_trainers used to be boolean, we keep this safety net for TeSS instances using the legacy value + # https://github.com/ElixirTeSS/TeSS/pull/1340 + n_trainers = case featured_trainer + when true then 1 + else [featured_trainer.to_i, 0].max + end + return nil if n_trainers.zero? + srand(Date.today.beginning_of_day.to_i) trainers = Trainer.joins(:user).order(:id) f_trainers = trainers.where.not(users: { image_file_size: nil }) trainers = f_trainers.exists? ? f_trainers : trainers - trainers.sample(1) + trainers.sample(n_trainers) end def set_content_providers diff --git a/config/tess.example.yml b/config/tess.example.yml index 70560649e..c8a51db5e 100644 --- a/config/tess.example.yml +++ b/config/tess.example.yml @@ -109,7 +109,7 @@ default: &default promo_blocks: false upcoming_events: # Number of events on home page. Leave blank to turn off latest_materials: # Number of materials on home page. Leave blank to turn off - featured_trainer: false + featured_trainer: # Number of trainers to feature on home page. Leave blank to turn off counters: false # Whether or not to show number of database objects in separate blocks search_box: true # Whether or not to show the search box additional_links: # Add custom links in separate blocks diff --git a/test/controllers/static_controller_test.rb b/test/controllers/static_controller_test.rb index 8e57815c1..8cf57380d 100644 --- a/test/controllers/static_controller_test.rb +++ b/test/controllers/static_controller_test.rb @@ -311,7 +311,7 @@ class StaticControllerTest < ActionController::TestCase end end - test 'should show featured trainer' do + test 'should show featured trainer (legacy boolean value set to `true`)' do with_settings({ site: { home_page: { featured_trainer: true } } }) do get :home assert_select 'section#featured_trainer', count: 1 @@ -320,6 +320,33 @@ class StaticControllerTest < ActionController::TestCase end end + test 'should not show featured trainer (legacy boolean value set to `false`)' do + with_settings({ site: { home_page: { featured_trainer: false } } }) do + get :home + assert_select 'section#featured_trainer', count: 0 + assert_select 'section#featured_trainer h2', count: 0 + assert_select 'section#featured_trainer li', count: 0 + end + end + + test 'should show featured trainer (positive int case)' do + with_settings({ site: { home_page: { featured_trainer: 2 } } }) do + get :home + assert_select 'section#featured_trainer', count: 1 + assert_select 'section#featured_trainer h2', count: 1 + assert_select 'section#featured_trainer li', count: 2 + end + end + + test 'should not show featured trainer (nil case)' do + with_settings({ site: { home_page: { featured_trainer: nil } } }) do + get :home + assert_select 'section#featured_trainer', count: 0 + assert_select 'section#featured_trainer h2', count: 0 + assert_select 'section#featured_trainer li', count: 0 + end + end + test 'should show event counts in counter blocks' do Event.destroy_all user = users(:regular_user)