Skip to content

Commit 0380ddb

Browse files
committed
Fixed issues displaying resources on user's profile page
Some counts were wrong and the collections link was broken
1 parent 18196a1 commit 0380ddb

5 files changed

Lines changed: 53 additions & 15 deletions

File tree

app/helpers/users_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# The helper for Workflow classes
2+
module UsersHelper
3+
4+
def self.user_profile_resource_limit
5+
30
6+
end
7+
8+
end

app/models/workflow.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def attribute_for_inspect(attr)
119119
end
120120

121121
def self.visible_by(user)
122-
if user && user.is_admin?
122+
if user&.is_admin?
123123
all
124124
elsif user
125125
references(:collaborations).includes(:collaborations).

app/views/users/show.html.erb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<% resource_limit = 30 %>
1+
<% resource_limit = UsersHelper.user_profile_resource_limit %>
22
<div class="wrapper collapsing-wrapper-tb">
33
<%- model_class = Profile -%>
44

@@ -143,40 +143,44 @@
143143

144144
<div class="row">
145145
<% materials = @user.materials.limit(resource_limit) %>
146+
<% materials_count = @user.materials.count %>
146147
<% upcoming_events = @user.events.not_finished %>
147148
<% past_events = @user.events.finished %>
148149
<% events = upcoming_events.limit(resource_limit) %>
149-
<% workflows = @user.workflows.visible_by(current_user) %>
150-
<% collections = @user.collections.limit(resource_limit) %>
150+
<% events_count = @user.events.count %>
151+
<% workflows = @user.workflows.visible_by(current_user).limit(resource_limit) %>
152+
<% workflows_count = @user.workflows.visible_by(current_user).count %>
153+
<% collections = @user.collections.visible_by(current_user).limit(resource_limit) %>
154+
<% collections_count = @user.collections.visible_by(current_user).count %>
151155

152156
<ul class="nav nav-tabs">
153157

154158
<!-- Tab: Events -->
155159
<% if TeSS::Config.feature['events'] %>
156160
<%= tab('Events', icon_class_for_model('events'), 'events', active: true,
157-
disabled: { check: @user.events.count.zero?, message: 'No registered events' },
158-
count: @user.events.count) %>
161+
disabled: { check: events_count.zero?, message: 'No registered events' },
162+
count: events_count) %>
159163
<% end %>
160164

161165
<!-- Tab: Materials -->
162166
<% if TeSS::Config.feature['materials'] %>
163167
<%= tab('Materials', icon_class_for_model('materials'), 'materials',
164168
disabled: { check: materials.none?, message: 'No registered training materials' },
165-
count: @user.materials.count) %>
169+
count: materials_count) %>
166170
<% end %>
167171

168172
<!-- Tab: Collections -->
169173
<% if TeSS::Config.feature['collections'] %>
170174
<%= tab('Collections', icon_class_for_model('collections'), 'collections',
171175
disabled: { check: collections.none?, message: 'No registered collections' },
172-
count: @user.collections.count) %>
176+
count: collections_count) %>
173177
<% end %>
174178

175179
<!-- Tab: Workflows -->
176180
<% if TeSS::Config.feature['workflows'] %>
177181
<%= tab('Workflows', icon_class_for_model('workflows'), 'workflows',
178182
disabled: { check: workflows.none?, message: 'No workflows' },
179-
count: workflows.count) %>
183+
count: workflows_count) %>
180184
<% end %>
181185
</ul>
182186

@@ -214,8 +218,8 @@
214218
<div id="materials" class="tab-pane fade">
215219
<div class="row">
216220
<div class="search-results-count">
217-
<%= (materials.count > 0 ? "Showing" : "Found") + " #{pluralize(materials.count, "material")}#{(@user.materials.count > resource_limit) ? " out of #{@user.materials.count}" : ''}." %>
218-
<%= link_to('View all results.', materials_path(user: @user.username)) if (@user.materials.count > resource_limit) %>
221+
<%= (materials.count > 0 ? "Showing" : "Found") + " #{pluralize(materials.count, "material")}#{(materials_count > resource_limit) ? " out of #{materials_count}" : ''}." %>
222+
<%= link_to('View all results', materials_path(user: @user.username)) if (materials_count > resource_limit) %>
219223
</div>
220224
<% materials.each do |material| %>
221225
<%= render material %>
@@ -229,8 +233,8 @@
229233
<div id="collections" class="tab-pane fade">
230234
<div class="row">
231235
<div class="search-results-count">
232-
<%= (collections.count > 0 ? "Showing" : "Found") + " #{pluralize(collections.count, "collection")}#{(@user.collections.count > resource_limit) ? " out of #{@user.collections.count}" : ''}." %>
233-
<%= link_to('View all results.', collections(user: @user.username)) if (@user.collections.count > resource_limit) %>
236+
<%= (collections.count > 0 ? "Showing" : "Found") + " #{pluralize(collections.count, "collection")}#{(collections_count > resource_limit) ? " out of #{collections_count}" : ''}." %>
237+
<%= link_to('View all results', collections_path(user: @user.username)) if (collections_count > resource_limit) %>
234238
</div>
235239
<% collections.each do |collection| %>
236240
<%= render collection %>
@@ -244,7 +248,8 @@
244248
<div id="workflows" class="tab-pane fade">
245249
<div class="row">
246250
<div class="search-results-count">
247-
<%= pluralize(workflows.count, 'workflow') %> found
251+
<%= (workflows.count > 0 ? "Showing" : "Found") + " #{pluralize(workflows.count, "workflow")}#{(workflows_count > resource_limit) ? " out of #{workflows_count}" : ''}." %>
252+
<%= link_to('View all results', workflows_path(user: @user.username)) if (workflows_count > resource_limit) %>
248253
</div>
249254
<% if workflows.any? %>
250255
<ul class="masonry media-grid" style="margin-top: 15px;">

config/initializers/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
APP_VERSION = '1.2.0'
1+
APP_VERSION = '1.2.1'

test/controllers/users_controller_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,29 @@ class UsersControllerTest < ActionController::TestCase
410410
assert_not_includes all_users, users(:basic_user)
411411
assert_includes all_users, users(:regular_user)
412412
end
413+
414+
test 'should show tabs for resource types and link to view all' do
415+
sign_in(@user)
416+
417+
assert @user.events.count > 1
418+
assert @user.materials.count > 1
419+
assert @user.collections.count > 1
420+
assert @user.workflows.count > 1
421+
422+
UsersHelper.stub(:user_profile_resource_limit, 1) do
423+
get :show, params: { id: @user }
424+
end
425+
426+
# Tabs
427+
assert_select 'a[data-toggle="tab"]', text: "Events (#{@user.events.count})"
428+
assert_select 'a[data-toggle="tab"]', text: "Materials (#{@user.materials.count})"
429+
assert_select 'a[data-toggle="tab"]', text: "Collections (#{@user.collections.count})"
430+
assert_select 'a[data-toggle="tab"]', text: "Workflows (#{@user.workflows.count})"
431+
432+
# Links
433+
assert_select '#events a[href=?]', events_path(user: @user.username, include_expired: true)
434+
assert_select '#materials a[href=?]', materials_path(user: @user.username)
435+
assert_select '#collections a[href=?]', collections_path(user: @user.username)
436+
assert_select '#workflows a[href=?]', workflows_path(user: @user.username)
437+
end
413438
end

0 commit comments

Comments
 (0)