Skip to content

GlobalSpace & DefaultSpace#1290

Open
fbacall wants to merge 3 commits intomasterfrom
global-space
Open

GlobalSpace & DefaultSpace#1290
fbacall wants to merge 3 commits intomasterfrom
global-space

Conversation

@fbacall
Copy link
Copy Markdown
Member

@fbacall fbacall commented Apr 22, 2026

Summary of changes

  • Separate GlobalSpace and DefaultSpace concepts. The former includes all resources, the latter includes only "default space" resources (where space_id is null).

Motivation and context

#1286

Checklist

  • I have read and followed the CONTRIBUTING guide.
  • I confirm that I have the authority necessary to make this contribution on behalf of its copyright owner and agree to license it to the TeSS codebase under the BSD license.

@fbacall fbacall requested a review from Copilot April 22, 2026 12:33
@fbacall fbacall changed the title Filter resources in DefaultSpace. Use GlobalSpace when spaces dis… GlobalSpace & DefaultSpace Apr 22, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Separates the “default space” concept (resources with space_id: nil) from a new “global space” concept (all resources), fixing the issue where DefaultSpace incorrectly selected all items when spaces are enabled.

Changes:

  • Updated Space.default to return DefaultSpace when spaces are enabled, otherwise GlobalSpace.
  • Introduced GlobalSpace and refactored DefaultSpace to inherit from it while overriding resource relations to scope to space_id: nil.
  • Added model tests to validate default space selection and basic resource scoping behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
app/models/space.rb Switches Space.default implementation to choose between DefaultSpace and GlobalSpace based on the feature flag.
app/models/global_space.rb Adds GlobalSpace implementation that exposes unscoped (“all resources”) relations and site-level metadata.
app/models/default_space.rb Refactors DefaultSpace to inherit from GlobalSpace and overrides relations to filter space_id: nil.
app/controllers/application_controller.rb Adjusts set_current_space behavior when spaces are disabled.
test/models/space_test.rb Adds tests for Space.default selection and validates resource scoping for materials.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


def set_current_space
Space.current_space = TeSS::Config.feature['spaces'] ? Space.find_by_host(request.host) : Space.default
Space.current_space = TeSS::Config.feature['spaces'] ? Space.find_by_host(request.host) : nil
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When spaces is disabled this sets Space.current_space to nil, which means every subsequent Space.current_space call in the request will instantiate a new Space.default (because Thread.current[:current_space] remains nil). Set it to Space.default (or memoize the default space object) so the current space is stable and only constructed once per request.

Suggested change
Space.current_space = TeSS::Config.feature['spaces'] ? Space.find_by_host(request.host) : nil
Space.current_space = TeSS::Config.feature['spaces'] ? Space.find_by_host(request.host) : Space.default

Copilot uses AI. Check for mistakes.
Comment thread test/models/space_test.rb

all_materials = GlobalSpace.new.materials.all.to_a
assert_includes all_materials, materials(:plant_space_material)
assert_includes all_materials, materials(:good_material)
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test asserts scoping for materials, but the PR also changes scoping behavior for events, workflows, collections, learning_paths, and learning_path_topics via DefaultSpace/GlobalSpace. Add assertions for at least one of those additional relations (or table-driven assertions across all of them) so regressions in the other methods are caught.

Suggested change
assert_includes all_materials, materials(:good_material)
assert_includes all_materials, materials(:good_material)
%i[materials events workflows collections learning_paths learning_path_topics].each do |relation_name|
table_name = Space.new.public_send(relation_name).klass.table_name
quoted_space_column = /["`]#{Regexp.escape(table_name)}["`]\.["`]space_id["`]/
assert_match(
/#{quoted_space_column.source} = #{spaces(:plants).id}/,
spaces(:plants).public_send(relation_name).to_sql
)
assert_match(
/#{quoted_space_column.source} IS NULL/,
DefaultSpace.new.public_send(relation_name).to_sql
)
assert_no_match(
quoted_space_column,
GlobalSpace.new.public_send(relation_name).to_sql
)
end

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants