Skip to content

Commit f9c465f

Browse files
committed
Filter resources in DefaultSpace. Use GlobalSpace when spaces disabled. #1286
1 parent 5d38b85 commit f9c465f

5 files changed

Lines changed: 104 additions & 51 deletions

File tree

app/controllers/application_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def user_not_authorized(exception)
114114
end
115115

116116
def set_current_space
117-
Space.current_space = TeSS::Config.feature['spaces'] ? Space.find_by_host(request.host) : Space.default
117+
Space.current_space = TeSS::Config.feature['spaces'] ? Space.find_by_host(request.host) : nil
118118
end
119119

120120
def current_space

app/models/default_space.rb

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,25 @@
1-
class DefaultSpace
2-
class Image
3-
def url
4-
TeSS::Config.site['logo']
5-
end
6-
end
7-
8-
def id
9-
nil
10-
end
11-
12-
def title
13-
TeSS::Config.site['title_short']
14-
end
15-
16-
def logo_alt
17-
TeSS::Config.site['logo_alt']
18-
end
19-
20-
def theme
21-
nil
22-
end
23-
24-
def image?
25-
true
26-
end
27-
28-
def image
29-
Image.new
30-
end
31-
1+
class DefaultSpace < GlobalSpace
322
def materials
33-
Material.all
3+
Material.where(space_id: nil)
344
end
355

366
def events
37-
Event.all
7+
Event.where(space_id: nil)
388
end
399

4010
def workflows
41-
Workflow.all
11+
Workflow.where(space_id: nil)
4212
end
4313

4414
def collections
45-
Collection.all
15+
Collection.where(space_id: nil)
4616
end
4717

4818
def learning_paths
49-
LearningPath.all
19+
LearningPath.where(space_id: nil)
5020
end
5121

5222
def learning_path_topics
53-
LearningPathTopic.all
54-
end
55-
56-
def default?
57-
true
58-
end
59-
60-
def administrators
61-
User.with_role('admin')
62-
end
63-
64-
def feature_enabled?(feature)
65-
TeSS::Config.feature[feature]
23+
LearningPathTopic.where(space_id: nil)
6624
end
6725
end

app/models/global_space.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class GlobalSpace
2+
class Image
3+
def url
4+
TeSS::Config.site['logo']
5+
end
6+
end
7+
8+
def id
9+
nil
10+
end
11+
12+
def title
13+
TeSS::Config.site['title_short']
14+
end
15+
16+
def logo_alt
17+
TeSS::Config.site['logo_alt']
18+
end
19+
20+
def theme
21+
nil
22+
end
23+
24+
def image?
25+
true
26+
end
27+
28+
def image
29+
Image.new
30+
end
31+
32+
def materials
33+
Material.all
34+
end
35+
36+
def events
37+
Event.all
38+
end
39+
40+
def workflows
41+
Workflow.all
42+
end
43+
44+
def collections
45+
Collection.all
46+
end
47+
48+
def learning_paths
49+
LearningPath.all
50+
end
51+
52+
def learning_path_topics
53+
LearningPathTopic.all
54+
end
55+
56+
def default?
57+
true
58+
end
59+
60+
def administrators
61+
User.with_role('admin')
62+
end
63+
64+
def feature_enabled?(feature)
65+
TeSS::Config.feature[feature]
66+
end
67+
end

app/models/space.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def self.with_current_space(space)
4444
end
4545

4646
def self.default
47-
DefaultSpace.new
47+
TeSS::Config.feature['spaces'] ? DefaultSpace.new : GlobalSpace.new
4848
end
4949

5050
def logo_alt

test/models/space_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,32 @@ class SpaceTest < ActiveSupport::TestCase
216216
assert_equal 'https://plants.mytess.training', @space.url
217217
end
218218
end
219+
220+
test 'get default space' do
221+
with_settings(feature: { spaces: false }) do
222+
assert Space.default.is_a?(GlobalSpace)
223+
end
224+
225+
with_settings(feature: { spaces: true }) do
226+
assert Space.default.is_a?(DefaultSpace)
227+
end
228+
end
229+
230+
test 'resources scoped to space' do
231+
plant_materials = spaces(:plants).materials.all.to_a
232+
assert_includes plant_materials, materials(:plant_space_material)
233+
assert_not_includes plant_materials, materials(:good_material)
234+
235+
astro_materials = spaces(:astro).materials.all.to_a
236+
assert_not_includes astro_materials, materials(:plant_space_material)
237+
assert_not_includes astro_materials, materials(:good_material)
238+
239+
default_materials = DefaultSpace.new.materials.all.to_a
240+
assert_not_includes default_materials, materials(:plant_space_material)
241+
assert_includes default_materials, materials(:good_material)
242+
243+
all_materials = GlobalSpace.new.materials.all.to_a
244+
assert_includes all_materials, materials(:plant_space_material)
245+
assert_includes all_materials, materials(:good_material)
246+
end
219247
end

0 commit comments

Comments
 (0)