Skip to content

Commit 9e582f0

Browse files
authored
Merge pull request #1290 from ElixirTeSS/global-space
GlobalSpace & DefaultSpace
2 parents d1e779a + ba519ce commit 9e582f0

5 files changed

Lines changed: 118 additions & 54 deletions

File tree

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/integration/sitemap_test.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ class SitemapTest < ActionDispatch::IntegrationTest
4343
SitemapGenerator::Interpreter.run(verbose: false)
4444
end
4545

46-
# Global sitemap should include all content
47-
global_urls = parse
48-
assert_includes global_urls, material_url(materials(:good_material))
49-
assert_includes global_urls, material_url(materials(:plant_space_material))
46+
# Default sitemap should only include default space content (content with null `space_id`)
47+
default_space_urls = parse
48+
assert_includes default_space_urls, material_url(materials(:good_material))
49+
refute_includes default_space_urls, material_url(materials(:plant_space_material))
5050

5151
# Space sitemap should only include content for that space
5252
space_urls = parse_space(space)
@@ -56,6 +56,17 @@ class SitemapTest < ActionDispatch::IntegrationTest
5656
refute_includes space_urls, "#{space.url}/materials/#{materials(:good_material).friendly_id}"
5757
end
5858

59+
test 'generates global sitemap when spaces feature is not enabled' do
60+
with_settings(feature: { spaces: false }) do
61+
SitemapGenerator::Interpreter.run(verbose: false)
62+
end
63+
64+
# Global sitemap should include all content
65+
global_urls = parse
66+
assert_includes global_urls, material_url(materials(:good_material))
67+
assert_includes global_urls, material_url(materials(:plant_space_material))
68+
end
69+
5970
private
6071

6172
# Hacked to read files from disk rather than fetching via URL.

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)