Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/latest materials on home #951

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
27 changes: 22 additions & 5 deletions app/controllers/static_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ def home

@resources = @resources.sort_by(&:created_at).reverse

@events = []
@events = set_upcoming_events
@materials = set_latest_materials
end

def showcase
@container_class = 'showcase-container container-fluid'
end

def set_upcoming_events
n_events = TeSS::Config.site.dig('home_page', 'upcoming_events')
return unless n_events
return [] unless n_events

@events += Event.search_and_filter(
Event.search_and_filter(
nil,
'',
{ 'start' => "#{Date.tomorrow.beginning_of_day}/" },
Expand All @@ -33,7 +41,16 @@ def home
).results
end

def showcase
@container_class = 'showcase-container container-fluid'
def set_latest_materials
n_materials = TeSS::Config.site.dig('home_page', 'latest_materials')
return [] unless n_materials

Material.search_and_filter(
nil,
'',
{ 'max_age' => '1 month' },
sort_by: 'new',
per_page: 10 * n_materials
)&.results&.group_by(&:content_provider_id)&.map { |_p_id, p_materials| p_materials&.first }&.first(n_materials)
end
end
2 changes: 2 additions & 0 deletions app/views/static/home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@
<%= render partial: 'static/home/promo_blocks' if TeSS::Config.site.dig('home_page', 'promo_blocks') %>

<%= render partial: 'static/home/upcoming_events' if @events.present? %>

<%= render partial: 'static/home/latest_materials' if @materials.present? %>
11 changes: 11 additions & 0 deletions app/views/static/home/_latest_materials.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<% cache(['home', 'latest_materials', @materials]) do %>
<section id="latest_materials">
<div class="tab-content">
<h2 class="text-center"><%= 'Latest materials' %></h2>
<ul class="tab-pane fade in active">
<%= render partial: 'common/masonry_grid', locals: { objects: @materials } %>
</ul>
</div>
</section>
<% end %>

1 change: 1 addition & 0 deletions config/tess.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ default: &default
faq: # [who, how, why, embed, subscribe] # Questions/answers in config/en.yml: home > faq. Use empty array to hide section
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
# The order in which the tabs appear (if feature enabled)
tab_order: ['about', 'events', 'materials', 'elearning_materials', 'workflows', 'collections', 'trainers', 'content_providers', 'nodes']
# The tabs that should be collapsed under the "Directory" tab. Can be left blank to hide it.
Expand Down
12 changes: 12 additions & 0 deletions test/controllers/static_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,16 @@ class StaticControllerTest < ActionController::TestCase
end
end
end

test 'should show latest materials' do
my_materials = [materials(:good_material), materials(:interpro)]
Material.stub(:search_and_filter, MockSearch.new(my_materials)) do
with_settings({ site: { home_page: { latest_materials: 5 } } }) do
get :home
assert_select 'section#latest_materials', count: 1
assert_select 'section#latest_materials h2', count: 1
assert_select 'section#latest_materials ul', count: 2
end
end
end
end
Loading