From ea48afb042ad4bbd934a89ee716a4ef4a9d89716 Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Tue, 10 Nov 2026 10:56:01 +0000 Subject: [PATCH 1/7] Scheduled per-space dumps of Bioschemas JSON-LD --- app/controllers/events_controller.rb | 8 +++ app/controllers/materials_controller.rb | 8 +++ config/schedule.example.yml | 3 + config/schedule.rb | 10 +++ lib/bioschemas/data_dump.rb | 92 +++++++++++++++++++++++++ lib/tasks/tess.rake | 9 +++ 6 files changed, 130 insertions(+) create mode 100644 lib/bioschemas/data_dump.rb diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 622733a6e..2cf446526 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -32,6 +32,14 @@ def index format.csv format.ics format.rss + format.jsonld do + dump = Bioschemas::DataDump.new(Event, space: current_space) + if dump.exists? + send_file(dump.file, type: :jsonld) + else + head :not_found + end + end end end diff --git a/app/controllers/materials_controller.rb b/app/controllers/materials_controller.rb index 9a53fa0d3..6bb377d3d 100644 --- a/app/controllers/materials_controller.rb +++ b/app/controllers/materials_controller.rb @@ -23,6 +23,14 @@ def index format.html { render elearning ? 'elearning_materials/index' : 'index' } format.json format.json_api { render({ json: @materials }.merge(api_collection_properties)) } + format.jsonld do + dump = Bioschemas::DataDump.new(Material, space: current_space) + if dump.exists? + send_file(dump.file, type: :jsonld) + else + head :not_found + end + end end end diff --git a/config/schedule.example.yml b/config/schedule.example.yml index 9deeef4e2..d9b8bcd6b 100644 --- a/config/schedule.example.yml +++ b/config/schedule.example.yml @@ -17,3 +17,6 @@ autocomplete_suggestions: dead_link_check: every: day at: 6am +bioschemas_dump: + every: sunday + at: 9pm diff --git a/config/schedule.rb b/config/schedule.rb index d9ddcf4cf..651a9fc06 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -74,3 +74,13 @@ rake 'tess:check_resource_urls' end end + +if !schedules['bioschemas_dump'].nil? + every :"#{schedules['bioschemas_dump']['every']}", at: "#{schedules['bioschemas_dump']['at']}" do + rake 'tess:dump_bioschemas' + end +else + every :day, at: '6am' do + rake 'tess:dump_bioschemas' + end +end diff --git a/lib/bioschemas/data_dump.rb b/lib/bioschemas/data_dump.rb new file mode 100644 index 000000000..1b3e38759 --- /dev/null +++ b/lib/bioschemas/data_dump.rb @@ -0,0 +1,92 @@ +module Bioschemas + class DataDump + attr_reader :name + + def initialize(model, space: Space.default, name: nil, records: nil) + @name = name || model.model_name.plural + @space = space + Space.with_current_space(space) do + @records = records || model.in_current_space.from_verified_users + end + end + + def file + File.open(file_path, 'r') + end + + def write + FileUtils.mkdir_p(file_path_base) + File.atomic_write(file_path) do |f| + f.write("[\n") + first = true + # Write each record at a time to avoid loading entire set into memory + bioschemas do |record| + f.write(",\n") unless first + record.to_json.each_line do |line| + f.write(' ', line) # Indent 2 spaces + end + first = false + end + f.write("\n]") + end + end + + def bioschemas + if block_given? + @records.each do |record| + yield record.to_bioschemas.first + end + else + @records.each.map { |record| record.to_bioschemas.first } + end + end + + def file_name + "#{@space&.default? ? '' : "space-#{@space.id}-"}#{@name}-bioschemas-dump.jsonld" + end + + def exists? + File.exist?(file_path) + end + + def size + File.size(file_path) if exists? + end + + def date_modified + File.mtime(file_path) if exists? + end + + def self.generate_dumps + generate_dumps_for_space(Space.default) + + if TeSS::Config.feature['spaces'] + Space.find_each do |space| + generate_dumps_for_space(space) + end + end + end + + def self.generate_dumps_for_space(space) + [Material, Event].map do |model| + generate_dump(model, space) + end + end + + def self.generate_dump(model, space) + dump = new(model, space: space) + dump.write + dump + end + + private + + def file_path + File.join(file_path_base, file_name) + end + + def file_path_base + Rails.root.join('public', 'bioschemas_dumps') + end + end +end diff --git a/lib/tasks/tess.rake b/lib/tasks/tess.rake index 665865ac3..2d1dca4ab 100644 --- a/lib/tasks/tess.rake +++ b/lib/tasks/tess.rake @@ -292,4 +292,13 @@ namespace :tess do puts 'done' puts 'Finished!' end + + desc 'Dump Event & Materials Bioschemas' + task dump_bioschemas: :environment do + puts 'Dumping Bioschemas...' + + Bioschemas::DataDump.generate_dumps + + puts 'Finished!' + end end From e5b5d952534048d858ebce2061b038cb77ed3c4a Mon Sep 17 00:00:00 2001 From: fbacall <503373+fbacall@users.noreply.github.com> Date: Tue, 10 Nov 2026 13:08:18 +0000 Subject: [PATCH 2/7] Add tests for Bioschemas data dumps Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- test/unit/bioschemas/data_dump_test.rb | 74 ++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 test/unit/bioschemas/data_dump_test.rb diff --git a/test/unit/bioschemas/data_dump_test.rb b/test/unit/bioschemas/data_dump_test.rb new file mode 100644 index 000000000..c13fca5f6 --- /dev/null +++ b/test/unit/bioschemas/data_dump_test.rb @@ -0,0 +1,74 @@ +require 'test_helper' +require 'tmpdir' + +class BioschemasDataDumpTest < ActiveSupport::TestCase + setup do + @dump_directory = Pathname.new(Dir.mktmpdir) + end + + teardown do + FileUtils.remove_entry(@dump_directory) if @dump_directory&.exist? + end + + test 'writes a JSON-LD array and exposes file metadata' do + material = materials(:good_material) + dump = Bioschemas::DataDump.new(Material, records: [material]) + + dump.stub(:file_path_base, @dump_directory) do + dump.write + + assert dump.exists? + assert_operator dump.size, :>, 0 + assert_kind_of Time, dump.date_modified + assert_equal [JSON.parse(material.to_bioschemas.first.to_json)], + JSON.parse(File.read(@dump_directory.join(dump.file_name))) + end + end + + test 'uses distinct filenames for default and non-default spaces' do + default_dump = Bioschemas::DataDump.new(Material, records: []) + space = spaces(:plants) + space_dump = Bioschemas::DataDump.new(Material, space: space, records: []) + + assert_equal 'materials-bioschemas-dump.jsonld', default_dump.file_name + assert_equal "space-#{space.id}-materials-bioschemas-dump.jsonld", space_dump.file_name + end + + test 'generates dumps for the default space and enabled spaces' do + other_space = spaces(:plants) + generated_spaces = [] + + Bioschemas::DataDump.stub(:generate_dumps_for_space, ->(space) { generated_spaces << space }) do + Space.stub(:find_each, ->(&block) { [other_space].each(&block) }) do + with_settings(feature: { spaces: true }) do + default_space = Space.default + Bioschemas::DataDump.generate_dumps + assert_equal [default_space, other_space], generated_spaces + end + end + end + end + + test 'does not generate space-specific dumps when spaces are disabled' do + generated_spaces = [] + + Bioschemas::DataDump.stub(:generate_dumps_for_space, ->(space) { generated_spaces << space }) do + with_settings(feature: { spaces: false }) do + default_space = Space.default + Bioschemas::DataDump.generate_dumps + assert_equal [default_space], generated_spaces + end + end + end + + test 'generates material and event dumps for a space' do + space = spaces(:plants) + generated_models = [] + + Bioschemas::DataDump.stub(:generate_dump, ->(model, dump_space) { generated_models << [model, dump_space] }) do + Bioschemas::DataDump.generate_dumps_for_space(space) + end + + assert_equal [[Material, space], [Event, space]], generated_models + end +end From 809556a425e27fecab11b9ee41be36abc86be4bf Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Tue, 10 Nov 2026 14:24:04 +0000 Subject: [PATCH 3/7] Test dump content per space --- lib/bioschemas/data_dump.rb | 6 ++--- test/unit/bioschemas/data_dump_test.rb | 31 +++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/bioschemas/data_dump.rb b/lib/bioschemas/data_dump.rb index 1b3e38759..e3cef9ba9 100644 --- a/lib/bioschemas/data_dump.rb +++ b/lib/bioschemas/data_dump.rb @@ -15,7 +15,7 @@ def file end def write - FileUtils.mkdir_p(file_path_base) + FileUtils.mkdir_p(self.class.file_path_base) File.atomic_write(file_path) do |f| f.write("[\n") first = true @@ -82,10 +82,10 @@ def self.generate_dump(model, space) private def file_path - File.join(file_path_base, file_name) + File.join(self.class.file_path_base, file_name) end - def file_path_base + def self.file_path_base Rails.root.join('public', 'bioschemas_dumps') end end diff --git a/test/unit/bioschemas/data_dump_test.rb b/test/unit/bioschemas/data_dump_test.rb index c13fca5f6..05e2c6604 100644 --- a/test/unit/bioschemas/data_dump_test.rb +++ b/test/unit/bioschemas/data_dump_test.rb @@ -14,7 +14,7 @@ class BioschemasDataDumpTest < ActiveSupport::TestCase material = materials(:good_material) dump = Bioschemas::DataDump.new(Material, records: [material]) - dump.stub(:file_path_base, @dump_directory) do + Bioschemas::DataDump.stub(:file_path_base, @dump_directory) do dump.write assert dump.exists? @@ -71,4 +71,33 @@ class BioschemasDataDumpTest < ActiveSupport::TestCase assert_equal [[Material, space], [Event, space]], generated_models end + + test 'dumps contain only content for given space' do + with_settings(feature: { spaces: true }) do + space_material = materials(:plant_space_material) + non_space_material = materials(:good_material) + Material.where.not(id: [space_material, non_space_material]).destroy_all + Event.destroy_all + + space = spaces(:plants) + space_dump = Bioschemas::DataDump.new(Material, space: space) + non_space_dump = Bioschemas::DataDump.new(Material) + + Bioschemas::DataDump.stub(:file_path_base, @dump_directory) do + Bioschemas::DataDump.generate_dumps + + assert space_dump.exists? + assert non_space_dump.exists? + + space_dump_titles = JSON.parse(space_dump.file.read).map { |r| r['name'] } + non_space_dump_titles = JSON.parse(non_space_dump.file.read).map { |r| r['name'] } + + assert_equal 1, space_dump_titles.length + assert_includes space_dump_titles, space_material.title + + assert_equal 1, non_space_dump_titles.length + assert_includes non_space_dump_titles, non_space_material.title + end + end + end end From faf72009b1094bbd2b97016d2e100cc89703858b Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Tue, 10 Nov 2026 14:51:58 +0000 Subject: [PATCH 4/7] Fix `in_current_space` filtering incorrectly in a `DefaultSpace` --- app/models/concerns/in_space.rb | 5 ++- app/models/default_space.rb | 4 ++ app/models/global_space.rb | 4 ++ app/models/space.rb | 4 ++ .../content_providers_controller_test.rb | 2 +- test/controllers/oai_controller_test.rb | 2 +- test/controllers/users_controller_test.rb | 8 ++-- test/models/space_test.rb | 42 +++++++++++++++++++ 8 files changed, 63 insertions(+), 8 deletions(-) diff --git a/app/models/concerns/in_space.rb b/app/models/concerns/in_space.rb index 90109d482..c203292cf 100644 --- a/app/models/concerns/in_space.rb +++ b/app/models/concerns/in_space.rb @@ -12,13 +12,14 @@ module InSpace end def space= s - return if s.default? + return if s.global? + return super(nil) if s.default? super end class_methods do def in_current_space - Space.current_space&.default? ? all : where(space_id: Space.current_space&.id) + Space.current_space&.global? ? all : where(space_id: Space.current_space&.id) end end end diff --git a/app/models/default_space.rb b/app/models/default_space.rb index b36e5daf1..bd539db6f 100644 --- a/app/models/default_space.rb +++ b/app/models/default_space.rb @@ -22,4 +22,8 @@ def learning_paths def learning_path_topics LearningPathTopic.where(space_id: nil) end + + def global? + false + end end diff --git a/app/models/global_space.rb b/app/models/global_space.rb index cda1b15f5..56f4856ab 100644 --- a/app/models/global_space.rb +++ b/app/models/global_space.rb @@ -57,6 +57,10 @@ def default? true end + def global? + true + end + def administrators User.with_role('admin') end diff --git a/app/models/space.rb b/app/models/space.rb index 7929a6bd8..6c36ad726 100644 --- a/app/models/space.rb +++ b/app/models/space.rb @@ -107,6 +107,10 @@ def default? false end + def global? + false + end + # Finds the users who hold a given SpaceRole in this space. # # role:: the role key (e.g. :admin) to filter by. diff --git a/test/controllers/content_providers_controller_test.rb b/test/controllers/content_providers_controller_test.rb index 6b600d279..d7093282a 100644 --- a/test/controllers/content_providers_controller_test.rb +++ b/test/controllers/content_providers_controller_test.rb @@ -587,7 +587,7 @@ class ContentProvidersControllerTest < ActionController::TestCase new_material = Material.create!(title: 'my_material', description: 'visible material', url: 'http://new.url.com', content_provider: @content_provider, user: @content_provider.user) get :show, params: { id: @content_provider } assert_response :success - not_disabled_material_count = @content_provider.materials.not_disabled.length + not_disabled_material_count = @content_provider.materials.in_current_space.not_disabled.length assert not_disabled_material_count > 1 assert_select '.search-results-count.my-3', text: "Showing #{not_disabled_material_count} materials" assert_select '.masonry-brick-heading h4', text: 'my_material' diff --git a/test/controllers/oai_controller_test.rb b/test/controllers/oai_controller_test.rb index a49f6af7a..cedb37c3f 100644 --- a/test/controllers/oai_controller_test.rb +++ b/test/controllers/oai_controller_test.rb @@ -86,7 +86,7 @@ class OaiControllerTest < ActionDispatch::IntegrationTest parsed = Nokogiri::XML(@response.body) titles = parsed.xpath('//dc:title', @ns).map(&:text) assert_includes titles, materials(:training_material).title - assert_includes titles, materials(:plant_space_material).title + refute_includes titles, materials(:plant_space_material).title plant_space = spaces(:plants) with_host(plant_space.host) do diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index 0360e5fa1..b0000b54b 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -425,10 +425,10 @@ class UsersControllerTest < ActionController::TestCase end end # Tabs - assert_select 'a[data-toggle="tab"]', text: "Events (#{@user.events.not_finished.count})" - assert_select 'a[data-toggle="tab"]', text: "Materials (#{@user.materials.count})" - assert_select 'a[data-toggle="tab"]', text: "Collections (#{@user.collections.count})" - assert_select 'a[data-toggle="tab"]', text: "Workflows (#{@user.workflows.count})" + assert_select 'a[data-toggle="tab"]', text: "Events (#{@user.events.in_current_space.not_finished.count})" + assert_select 'a[data-toggle="tab"]', text: "Materials (#{@user.materials.in_current_space.count})" + assert_select 'a[data-toggle="tab"]', text: "Collections (#{@user.collections.in_current_space.count})" + assert_select 'a[data-toggle="tab"]', text: "Workflows (#{@user.workflows.in_current_space.count})" # Links assert_select '#events a[href=?]', events_path(user: @user.username) diff --git a/test/models/space_test.rb b/test/models/space_test.rb index 28f47666f..a808b4e37 100644 --- a/test/models/space_test.rb +++ b/test/models/space_test.rb @@ -255,4 +255,46 @@ class SpaceTest < ActiveSupport::TestCase assert_includes all_materials, materials(:plant_space_material) assert_includes all_materials, materials(:good_material) end + + test 'resources scoped to space when using in_current_space' do + Space.with_current_space(spaces(:plants)) do + plant_materials = Material.in_current_space.to_a + assert_includes plant_materials, materials(:plant_space_material) + assert_not_includes plant_materials, materials(:good_material) + end + + Space.with_current_space(spaces(:astro)) do + astro_materials = Material.in_current_space.to_a + assert_not_includes astro_materials, materials(:plant_space_material) + assert_not_includes astro_materials, materials(:good_material) + end + + Space.with_current_space(DefaultSpace.new) do + default_materials = Material.in_current_space.to_a + assert_not_includes default_materials, materials(:plant_space_material) + assert_includes default_materials, materials(:good_material) + end + + Space.with_current_space(GlobalSpace.new) do + all_materials = Material.in_current_space.to_a + assert_includes all_materials, materials(:plant_space_material) + assert_includes all_materials, materials(:good_material) + end + end + + test 'space assignment' do + material = materials(:good_material) + + material.space = spaces(:plants) + assert_equal spaces(:plants), material.space + assert_equal spaces(:plants).id, material.space_id + + material.space = GlobalSpace.new # Does nothing + assert_equal spaces(:plants), material.space + assert_equal spaces(:plants).id, material.space_id + + material.space = DefaultSpace.new # Sets to nil + assert_nil material.space + assert_nil material.space_id + end end From 76c4be99e17d6803c9e45e88de2f57ba30d8ff6d Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Tue, 10 Nov 2026 15:02:25 +0000 Subject: [PATCH 5/7] Default to current space --- app/controllers/events_controller.rb | 2 +- app/controllers/materials_controller.rb | 2 +- lib/bioschemas/data_dump.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 2cf446526..9b9e4116d 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -33,7 +33,7 @@ def index format.ics format.rss format.jsonld do - dump = Bioschemas::DataDump.new(Event, space: current_space) + dump = Bioschemas::DataDump.new(Event) if dump.exists? send_file(dump.file, type: :jsonld) else diff --git a/app/controllers/materials_controller.rb b/app/controllers/materials_controller.rb index 6bb377d3d..cc1b92aad 100644 --- a/app/controllers/materials_controller.rb +++ b/app/controllers/materials_controller.rb @@ -24,7 +24,7 @@ def index format.json format.json_api { render({ json: @materials }.merge(api_collection_properties)) } format.jsonld do - dump = Bioschemas::DataDump.new(Material, space: current_space) + dump = Bioschemas::DataDump.new(Material) if dump.exists? send_file(dump.file, type: :jsonld) else diff --git a/lib/bioschemas/data_dump.rb b/lib/bioschemas/data_dump.rb index e3cef9ba9..defb986aa 100644 --- a/lib/bioschemas/data_dump.rb +++ b/lib/bioschemas/data_dump.rb @@ -2,7 +2,7 @@ module Bioschemas class DataDump attr_reader :name - def initialize(model, space: Space.default, name: nil, records: nil) + def initialize(model, space: Space.current_space, name: nil, records: nil) @name = name || model.model_name.plural @space = space Space.with_current_space(space) do From 4b1495dce9c2e915d39472f2289cb082afccb523 Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Tue, 10 Nov 2026 15:02:48 +0000 Subject: [PATCH 6/7] Default Bioschemas dump schedule to once per week --- config/schedule.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/schedule.rb b/config/schedule.rb index 651a9fc06..a1fcb541a 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -80,7 +80,7 @@ rake 'tess:dump_bioschemas' end else - every :day, at: '6am' do + every :sunday, at: '9pm' do rake 'tess:dump_bioschemas' end end From b4388f176119ecc35e534d59fd906795d9975c63 Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Wed, 11 Nov 2026 11:01:01 +0000 Subject: [PATCH 7/7] Only `@context` once in data dump JSON --- lib/bioschemas/data_dump.rb | 12 +++++++----- lib/bioschemas/generator.rb | 12 ++++++------ test/unit/bioschemas/data_dump_test.rb | 9 +++++---- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/bioschemas/data_dump.rb b/lib/bioschemas/data_dump.rb index defb986aa..54a94a603 100644 --- a/lib/bioschemas/data_dump.rb +++ b/lib/bioschemas/data_dump.rb @@ -17,17 +17,19 @@ def file def write FileUtils.mkdir_p(self.class.file_path_base) File.atomic_write(file_path) do |f| - f.write("[\n") + f.write("{\n") + f.write(" \"@context\" : \"http://schema.org\",\n") + f.write(" \"@graph\": [\n") first = true # Write each record at a time to avoid loading entire set into memory - bioschemas do |record| + bioschemas do |generator| f.write(",\n") unless first - record.to_json.each_line do |line| - f.write(' ', line) # Indent 2 spaces + JSON.pretty_generate(generator.generate(include_context: false)).each_line do |line| + f.write(' ', line) # Indent 2 spaces end first = false end - f.write("\n]") + f.write("\n ]\n}") end end diff --git a/lib/bioschemas/generator.rb b/lib/bioschemas/generator.rb index 046bba106..dfecc771e 100644 --- a/lib/bioschemas/generator.rb +++ b/lib/bioschemas/generator.rb @@ -14,12 +14,12 @@ def initialize(resource) @resource = resource end - def generate - template = { - '@context' => 'http://schema.org', - '@id' => routes.polymorphic_url(resource), - '@type' => self.class.type - } + def generate(include_context: true) + template = {} + + template['@context'] = 'http://schema.org' if include_context + template['@id'] = routes.polymorphic_url(resource) + template['@type'] = self.class.type if self.class.bioschemas_profile template['dct:conformsTo'] = { diff --git a/test/unit/bioschemas/data_dump_test.rb b/test/unit/bioschemas/data_dump_test.rb index 05e2c6604..535a7256c 100644 --- a/test/unit/bioschemas/data_dump_test.rb +++ b/test/unit/bioschemas/data_dump_test.rb @@ -20,8 +20,9 @@ class BioschemasDataDumpTest < ActiveSupport::TestCase assert dump.exists? assert_operator dump.size, :>, 0 assert_kind_of Time, dump.date_modified - assert_equal [JSON.parse(material.to_bioschemas.first.to_json)], - JSON.parse(File.read(@dump_directory.join(dump.file_name))) + json = JSON.parse(material.to_bioschemas.first.to_json) + json.delete('@context') + assert_equal [json], JSON.parse(File.read(@dump_directory.join(dump.file_name)))['@graph'] end end @@ -89,8 +90,8 @@ class BioschemasDataDumpTest < ActiveSupport::TestCase assert space_dump.exists? assert non_space_dump.exists? - space_dump_titles = JSON.parse(space_dump.file.read).map { |r| r['name'] } - non_space_dump_titles = JSON.parse(non_space_dump.file.read).map { |r| r['name'] } + space_dump_titles = JSON.parse(space_dump.file.read)['@graph'].map { |r| r['name'] } + non_space_dump_titles = JSON.parse(non_space_dump.file.read)['@graph'].map { |r| r['name'] } assert_equal 1, space_dump_titles.length assert_includes space_dump_titles, space_material.title