Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def index
format.csv
format.ics
format.rss
format.jsonld do
dump = Bioschemas::DataDump.new(Event)
if dump.exists?
send_file(dump.file, type: :jsonld)
else
head :not_found
end
end
end
end

Expand Down
8 changes: 8 additions & 0 deletions app/controllers/materials_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
if dump.exists?
send_file(dump.file, type: :jsonld)
else
head :not_found
end
end
end
end

Expand Down
5 changes: 3 additions & 2 deletions app/models/concerns/in_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions app/models/default_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ def learning_paths
def learning_path_topics
LearningPathTopic.where(space_id: nil)
end

def global?
false
end
end
4 changes: 4 additions & 0 deletions app/models/global_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def default?
true
end

def global?
true
end

def administrators
User.with_role('admin')
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions config/schedule.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ autocomplete_suggestions:
dead_link_check:
every: day
at: 6am
bioschemas_dump:
every: sunday
at: 9pm
10 changes: 10 additions & 0 deletions config/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 :sunday, at: '9pm' do
rake 'tess:dump_bioschemas'
end
end
94 changes: 94 additions & 0 deletions lib/bioschemas/data_dump.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
module Bioschemas
class DataDump
attr_reader :name

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
@records = records || model.in_current_space.from_verified_users
end
end

def file
File.open(file_path, 'r')
end

def write
FileUtils.mkdir_p(self.class.file_path_base)
File.atomic_write(file_path) do |f|
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 |generator|
f.write(",\n") unless first
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 ]\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(self.class.file_path_base, file_name)
end

def self.file_path_base
Rails.root.join('public', 'bioschemas_dumps')
end
end
end
12 changes: 6 additions & 6 deletions lib/bioschemas/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = {
Expand Down
9 changes: 9 additions & 0 deletions lib/tasks/tess.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/controllers/content_providers_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/oai_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/controllers/users_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions test/models/space_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading