-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcommunities_controller.rb
More file actions
67 lines (56 loc) · 1.85 KB
/
Copy pathcommunities_controller.rb
File metadata and controls
67 lines (56 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# The controller for actions related to home page
class CommunitiesController < ApplicationController
before_action :set_community
skip_before_action :authenticate_user!, :authenticate_user_from_token!
def show
@hide_search_box = true
@resources = []
if TeSS::Config.solr_enabled
enabled = []
enabled.append(Event) if feature_enabled?('events')
enabled.append(Material) if feature_enabled?('materials')
enabled.append(Collection) if feature_enabled?('collections')
enabled.each do |resource|
@resources += resource.search_and_filter(nil, '', { 'max_age' => '1 month' },
sort_by: 'new', per_page: 5).results
end
end
@resources = @resources.sort_by(&:created_at).reverse
@featured_trainer = nil # set_featured_trainer
@events = set_upcoming_events
@materials = set_latest_materials
end
private
def set_featured_trainer
srand(Date.today.beginning_of_day.to_i)
Trainer.order(:id).sample(1)
end
def set_latest_materials
n_materials = 5
Material.search_and_filter(
nil,
'',
{ **@community.filters },
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
def set_upcoming_events
n_events = 5
events = Event.search_and_filter(
nil,
'',
{
'start' => "#{10.years.ago.beginning_of_day}/",
**@community.filters
},
sort_by: 'early',
per_page: 5 * n_events
).results
@events = Event.from_varied_providers(events, n_events).sort_by(&:created_at).reverse
end
def set_community
@community = Community.find(params[:id])
raise ActionController::RoutingError.new('Not a recognized community') unless @community
end
end