Skip to content

Commit 4e8605c

Browse files
committed
Filter associated resources on various show pages according to current space
1 parent cbae95c commit 4e8605c

11 files changed

+111
-27
lines changed

app/models/concerns/in_space.rb

+6
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ def space= s
1515
return if s.default?
1616
super
1717
end
18+
19+
class_methods do
20+
def in_current_space
21+
Space.current_space&.default? ? all : where(space_id: Space.current_space&.id)
22+
end
23+
end
1824
end

app/models/space.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ class Space < ApplicationRecord
33
include LogParameterChanges
44

55
belongs_to :user
6-
has_many :materials
7-
has_many :events
8-
has_many :workflows
9-
has_many :collections
10-
has_many :learning_paths
11-
has_many :learning_path_topics
6+
has_many :materials, dependent: :nullify
7+
has_many :events, dependent: :nullify
8+
has_many :workflows, dependent: :nullify
9+
has_many :collections, dependent: :nullify
10+
has_many :learning_paths, dependent: :nullify
11+
has_many :learning_path_topics, dependent: :nullify
1212

1313
has_image(placeholder: TeSS::Config.placeholder['content_provider'])
1414

app/views/content_providers/show.html.erb

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121

2222
<div class="my-3">
2323
<% resource_limit = 30 %>
24-
<% materials = @content_provider.materials.from_verified_users.not_disabled.limit(resource_limit) %>
25-
<% materials_count = @content_provider.materials.from_verified_users.not_disabled.count %>
26-
<% upcoming_events = @content_provider.events.from_verified_users.not_finished.not_disabled.order(start: :asc) %>
27-
<% past_events = @content_provider.events.from_verified_users.finished.not_disabled %>
24+
<% materials = @content_provider.materials.in_current_space.from_verified_users.not_disabled.limit(resource_limit) %>
25+
<% materials_count = @content_provider.materials.in_current_space.from_verified_users.not_disabled.count %>
26+
<% upcoming_events = @content_provider.events.in_current_space.from_verified_users.not_finished.not_disabled.order(start: :asc) %>
27+
<% past_events = @content_provider.events.in_current_space.from_verified_users.finished.not_disabled %>
2828
<% events = upcoming_events.limit(resource_limit) %>
29-
<% e = @content_provider.events.from_verified_users.not_disabled %>
29+
<% e = @content_provider.events.in_current_space.from_verified_users.not_disabled %>
3030
<% events_count = e.count %>
3131
<% not_finished_events_count = e.not_finished.count %>
3232
<% sources = @content_provider.sources.order(enabled: :desc, finished_at: :desc) %>

app/views/nodes/show.html.erb

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
<% resource_limit = 30 %>
1919
<% content_providers = @node.content_providers.from_verified_users.limit(resource_limit) %>
2020
<% content_providers_count = @node.content_providers.from_verified_users.count %>
21-
<% materials = @node.related_materials.from_verified_users.limit(resource_limit) %>
22-
<% materials_count = @node.related_materials.from_verified_users.count %>
23-
<% upcoming_events = @node.related_events.from_verified_users.not_finished %>
24-
<% past_events = @node.related_events.from_verified_users.finished %>
21+
<% materials = @node.related_materials.in_current_space.from_verified_users.limit(resource_limit) %>
22+
<% materials_count = @node.related_materials.in_current_space.from_verified_users.count %>
23+
<% upcoming_events = @node.related_events.in_current_space.from_verified_users.not_finished %>
24+
<% past_events = @node.related_events.in_current_space.from_verified_users.finished %>
2525
<% events = upcoming_events.limit(resource_limit) %>
26-
<% e = @node.events.from_verified_users.not_disabled %>
26+
<% e = @node.events.in_current_space.from_verified_users.not_disabled %>
2727
<% events_count = e.count %>
2828
<% not_finished_events_count = e.not_finished.count %>
2929
<% activator = tab_activator %>

app/views/users/show.html.erb

+9-9
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,18 @@
126126
<%= render(partial: 'users/partials/ban', locals: { ban: @user.ban }) if @user.banned? && current_user.try(:is_admin?) %>
127127

128128
<div class="my-3">
129-
<% materials = @user.materials.limit(resource_limit) %>
130-
<% materials_count = @user.materials.count %>
131-
<% upcoming_events = @user.events.not_finished %>
132-
<% past_events = @user.events.finished %>
129+
<% materials = @user.materials.in_current_space.limit(resource_limit) %>
130+
<% materials_count = @user.materials.in_current_space.count %>
131+
<% upcoming_events = @user.events.in_current_space.not_finished %>
132+
<% past_events = @user.events.in_current_space.finished %>
133133
<% events = upcoming_events.limit(resource_limit) %>
134-
<% e = @user.events.from_verified_users.not_disabled %>
134+
<% e = @user.events.in_current_space.from_verified_users.not_disabled %>
135135
<% events_count = e.count %>
136136
<% not_finished_events_count = e.not_finished.count %>
137-
<% workflows = @user.workflows.visible_by(current_user).limit(resource_limit) %>
138-
<% workflows_count = @user.workflows.visible_by(current_user).count %>
139-
<% collections = @user.collections.visible_by(current_user).limit(resource_limit) %>
140-
<% collections_count = @user.collections.visible_by(current_user).count %>
137+
<% workflows = @user.workflows.in_current_space.visible_by(current_user).limit(resource_limit) %>
138+
<% workflows_count = @user.workflows.in_current_space.visible_by(current_user).count %>
139+
<% collections = @user.collections.in_current_space.visible_by(current_user).limit(resource_limit) %>
140+
<% collections_count = @user.collections.in_current_space.visible_by(current_user).count %>
141141
<% activator = tab_activator %>
142142

143143
<ul class="nav nav-tabs">

test/controllers/content_providers_controller_test.rb

+14-2
Original file line numberDiff line numberDiff line change
@@ -587,13 +587,25 @@ class ContentProvidersControllerTest < ActionController::TestCase
587587
new_material = Material.create!(title: 'my_material', description: 'visible material', url: 'http://new.url.com', content_provider: @content_provider, user: @content_provider.user)
588588
get :show, params: { id: @content_provider }
589589
assert_response :success
590-
assert_select '.search-results-count.my-3', text: 'Showing 10 materials'
590+
not_disabled_material_count = @content_provider.materials.not_disabled.length
591+
assert not_disabled_material_count > 1
592+
assert_select '.search-results-count.my-3', text: "Showing #{not_disabled_material_count} materials"
591593
assert_select '.masonry-brick-heading h4', text: 'my_material'
592594
new_material.visible = false
593595
new_material.save!
594596
get :show, params: { id: @content_provider }
595597
assert_response :success
596-
assert_select '.search-results-count.my-3', text: 'Showing 9 materials'
598+
assert_select '.search-results-count.my-3', text: "Showing #{not_disabled_material_count - 1} materials"
597599
assert_select '.masonry-brick-heading h4', text: 'my_material', count: 0
598600
end
601+
602+
test 'should only show content for current space on show page' do
603+
with_host('plants.mytess.training') do
604+
get :show, params: { id: @content_provider }
605+
assert_select '.search-results-count.my-3', text: 'Showing 1 material'
606+
assert_select '.masonry-brick-heading h4', text: 'Plant material'
607+
assert_select '.search-results-count.my-3', text: 'Showing 1 event'
608+
assert_select '.masonry-brick-heading h4', text: 'Learn about plants'
609+
end
610+
end
599611
end

test/controllers/nodes_controller_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,14 @@ class NodesControllerTest < ActionController::TestCase
239239

240240
assert_select 'h4', text: 'No Country Node'
241241
end
242+
243+
test 'should only show content for current space on show page' do
244+
with_host('plants.mytess.training') do
245+
get :show, params: { id: @node }
246+
assert_select '.search-results-count.my-3', text: 'Showing 1 material'
247+
assert_select '.masonry-brick-heading h4', text: 'Plant material'
248+
assert_select '.search-results-count.my-3', text: 'Showing 1 event'
249+
assert_select '.masonry-brick-heading h4', text: 'Learn about plants'
250+
end
251+
end
242252
end

test/controllers/users_controller_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -458,4 +458,14 @@ class UsersControllerTest < ActionController::TestCase
458458
assert_response :success
459459
refute assigns(:users).include?(users(:basic_user))
460460
end
461+
462+
test 'should only show content for current space on show page' do
463+
with_host('plants.mytess.training') do
464+
get :show, params: { id: @user }
465+
assert_select '.search-results-count.my-3', text: 'Showing 1 material'
466+
assert_select '.masonry-brick-heading h4', text: 'Plant material'
467+
assert_select '.search-results-count.my-3', text: 'Showing 1 event'
468+
assert_select '.masonry-brick-heading h4', text: 'Learn about plants'
469+
end
470+
end
461471
end

test/fixtures/events.yml

+11
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,14 @@ hybrid_event:
493493
start: 2024-12-12 10:00:00
494494
end: 2024-12-12 12:00:00
495495
timezone: UTC
496+
497+
plant_space_event:
498+
title: Learn about plants
499+
url: http://example.com/plants/event123
500+
description: Plants are very interesting indeed
501+
user: regular_user
502+
content_provider: goblet
503+
start: 2126-12-12 10:00:00
504+
end: 2126-12-12 12:00:00
505+
timezone: UTC
506+
space: plants

test/fixtures/materials.yml

+12
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,15 @@ youtube_video_material:
178178
resource_type:
179179
- Video
180180
learning_objectives: In this video, I will show you how to create and run data quality checks in REDCap.
181+
182+
plant_space_material:
183+
user: regular_user
184+
title: Plant material
185+
slug: plant-material
186+
url: http://example.com/plants/material123
187+
description: This is a material in the plant space
188+
licence: CC-BY-4.0
189+
keywords: ['plants']
190+
content_provider: goblet
191+
status: active
192+
space: plants

test/models/content_provider_test.rb

+23
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,27 @@ class ContentProviderTest < ActiveSupport::TestCase
106106
assert_nil LearningPath.find_by_id(learning_path.id)
107107
assert_nil Source.find_by_id(source.id)
108108
end
109+
110+
test 'in_current_space limits resources to current space if feature enabled' do
111+
orig_space = Space.current_space
112+
plant_space = spaces(:plants)
113+
Space.current_space = plant_space
114+
content_provider = content_providers(:goblet)
115+
materials = content_provider.materials.in_current_space
116+
assert_equal 1, materials.length
117+
assert_includes materials, materials(:plant_space_material)
118+
ensure
119+
Space.current_space = orig_space
120+
end
121+
122+
test 'in_current_space does not limit resources to current space if feature disabled' do
123+
content_provider = content_providers(:goblet)
124+
with_settings(feature: { spaces: false }) do
125+
assert Space.current_space.default?
126+
materials = content_provider.materials.in_current_space
127+
assert_equal 10, materials.length
128+
assert_includes materials, materials(:plant_space_material)
129+
assert_includes materials, materials(:good_material)
130+
end
131+
end
109132
end

0 commit comments

Comments
 (0)