Skip to content

Commit cac3210

Browse files
committed
Filter resources in DefaultSpace. Use GlobalSpace when spaces disabled. #1286
1 parent 4db5db6 commit cac3210

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
@@ -34,7 +34,7 @@ def self.current_space
3434
end
3535

3636
def self.default
37-
DefaultSpace.new
37+
TeSS::Config.feature['spaces'] ? DefaultSpace.new : GlobalSpace.new
3838
end
3939

4040
def logo_alt

test/models/space_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,32 @@ class SpaceTest < ActiveSupport::TestCase
174174
refute @space.is_subdomain?
175175
assert Space.new(host: 'test.example.com').is_subdomain?
176176
end
177+
178+
test 'get default space' do
179+
with_settings(feature: { spaces: false }) do
180+
assert Space.default.is_a?(GlobalSpace)
181+
end
182+
183+
with_settings(feature: { spaces: true }) do
184+
assert Space.default.is_a?(DefaultSpace)
185+
end
186+
end
187+
188+
test 'resources scoped to space' do
189+
plant_materials = spaces(:plants).materials.all.to_a
190+
assert_includes plant_materials, materials(:plant_space_material)
191+
assert_not_includes plant_materials, materials(:good_material)
192+
193+
astro_materials = spaces(:astro).materials.all.to_a
194+
assert_not_includes astro_materials, materials(:plant_space_material)
195+
assert_not_includes astro_materials, materials(:good_material)
196+
197+
default_materials = DefaultSpace.new.materials.all.to_a
198+
assert_not_includes default_materials, materials(:plant_space_material)
199+
assert_includes default_materials, materials(:good_material)
200+
201+
all_materials = GlobalSpace.new.materials.all.to_a
202+
assert_includes all_materials, materials(:plant_space_material)
203+
assert_includes all_materials, materials(:good_material)
204+
end
177205
end

0 commit comments

Comments
 (0)