Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions app/controllers/static_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/tess.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 28 additions & 1 deletion test/controllers/static_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down