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
6 changes: 3 additions & 3 deletions app/controllers/trainers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def index
# GET /trainers/1.json

def show
@bioschemas = @trainer.user.registrations.flat_map(&:to_bioschemas)
respond_to do |format|
format.html
format.json
format.json_api { render json: @trainer }
format.html
end
end

Expand All @@ -45,5 +46,4 @@ def trainer_params
{ :expertise_technical => [] }, { :interest => [] }, { :activity => [] },
{ :fields => [] }, { :social_media => [] })
end

end
end
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def invitees
# GET /users/1
# GET /users/1.json
def show
@bioschemas = @user.registrations.flat_map(&:to_bioschemas)
respond_to do |format|
format.html
format.json
Expand Down
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ def has_role_in_any_space?(role)
space_roles.where(key: role).any?
end

# Get user's registrations
def registrations
n_events = events.in_current_space
n_materials = materials.in_current_space
n_events + n_materials
end

protected

def reassign_resources(new_owner = User.get_default_user)
Expand Down
102 changes: 102 additions & 0 deletions app/views/common/_registrations_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<% resource_limit = UsersHelper.user_profile_resource_limit %>
<h3>
List of registrations in <%= TeSS::Config.site['title_short'] %>
</h3>
<div class="my-3">
<% materials = user.materials.in_current_space.limit(resource_limit) %>
<% materials_count = user.materials.in_current_space.count %>
<% upcoming_events = user.events.in_current_space.not_finished %>
<% past_events = user.events.in_current_space.finished %>
<% events = upcoming_events.limit(resource_limit) %>
<% e = user.events.in_current_space.from_verified_users.not_disabled %>
<% events_count = e.count %>
<% not_finished_events_count = e.not_finished.count %>
<% workflows = user.workflows.in_current_space.visible_by(current_user).limit(resource_limit) %>
<% workflows_count = user.workflows.in_current_space.visible_by(current_user).count %>
<% collections = user.collections.in_current_space.visible_by(current_user).limit(resource_limit) %>
<% collections_count = user.collections.in_current_space.visible_by(current_user).count %>
<% activator = tab_activator %>
<% total = materials_count.to_i + events_count.to_i + workflows_count.to_i + collections_count.to_i %>

<!-- Field: registrations -->
<% if total.positive? %>
<p>Added a total of <b><%= total %></b> training resources in <%= TeSS::Config.site['title_short'] %>.</p>
<% end %>

<ul class="nav nav-tabs">
<!-- Tab: Events -->
<% if TeSS::Config.feature['events'] %>
<%= tab('Events', icon_class_for_model('events'), 'events', activator: activator,
disabled: { check: events_count.zero?, message: 'No associated events' },
count: not_finished_events_count.zero? && events_count.positive? ? '0*' : not_finished_events_count) %>
<% end %>

<!-- Tab: Materials -->
<% if TeSS::Config.feature['materials'] %>
<%= tab('Materials', icon_class_for_model('materials'), 'materials', activator: activator,
disabled: { check: materials.none?, message: 'No registered training materials' },
count: materials_count) %>
<% end %>

<!-- Tab: Collections -->
<% if TeSS::Config.feature['collections'] %>
<%= tab('Collections', icon_class_for_model('collections'), 'collections', activator: activator,
disabled: { check: collections.none?, message: 'No registered collections' },
count: collections_count) %>
<% end %>

<!-- Tab: Workflows -->
<% if TeSS::Config.feature['workflows'] %>
<%= tab('Workflows', icon_class_for_model('workflows'), 'workflows', activator: activator,
disabled: { check: workflows.none?, message: 'No workflows' },
count: workflows_count) %>
<% end %>
</ul>

<div class="tab-content">
<!-- Tab: Events -->
<% if TeSS::Config.feature['events'] %>
<%= render partial: 'common/associated_events',
locals: { total_count: upcoming_events.count,
past_count: past_events.count,
limit: resource_limit,
resources: events,
activator: activator,
view_all_link: events_path(user: user.username),
inc_expired_link: events_path(user: user.username, include_expired: true) } %>
<% end %>

<!-- Tab: Materials -->
<% if TeSS::Config.feature['materials'] %>
<%= render partial: 'common/associated_resources',
locals: { model: Material,
total_count: materials_count,
limit: resource_limit,
resources: materials,
activator: activator,
view_all_link: materials_path(user: user.username) } %>
<% end %>

<!-- Tab: Collections -->
<% if TeSS::Config.feature['collections'] %>
<%= render partial: 'common/associated_resources',
locals: { model: Collection,
total_count: collections_count,
limit: resource_limit,
resources: collections,
activator: activator,
view_all_link: collections_path(user: user.username) } %>
<% end %>

<!-- Tab: Workflows -->
<% if TeSS::Config.feature['workflows'] %>
<%= render partial: 'common/associated_resources',
locals: { model: Workflow,
total_count: workflows_count,
limit: resource_limit,
resources: workflows,
activator: activator,
view_all_link: workflows_path(user: user.username) } %>
<% end %>
</div> <!-- end tab-content -->
</div>
3 changes: 3 additions & 0 deletions app/views/trainers/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@
<!-- Other metadata -->
<%= render partial: 'trainers/partials/twitter_card', locals: { trainer: @trainer } %>
</div>

<!-- Field: registrations list with Events, Materials, Collections, Workflows -->
<%= render(partial: 'common/registrations_list', locals: { user: @trainer.user }) %>
</div>
</div>
95 changes: 2 additions & 93 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<% resource_limit = UsersHelper.user_profile_resource_limit %>
<div class="wrapper collapsing-wrapper-tb">
<%- model_class = Profile -%>

Expand Down Expand Up @@ -128,97 +127,7 @@

<%= render(partial: 'users/partials/ban', locals: { ban: @user.ban }) if @user.banned? && current_user.try(:is_admin?) %>

<div class="my-3">
<% materials = @user.materials.in_current_space.limit(resource_limit) %>
<% materials_count = @user.materials.in_current_space.count %>
<% upcoming_events = @user.events.in_current_space.not_finished %>
<% past_events = @user.events.in_current_space.finished %>
<% events = upcoming_events.limit(resource_limit) %>
<% e = @user.events.in_current_space.from_verified_users.not_disabled %>
<% events_count = e.count %>
<% not_finished_events_count = e.not_finished.count %>
<% workflows = @user.workflows.in_current_space.visible_by(current_user).limit(resource_limit) %>
<% workflows_count = @user.workflows.in_current_space.visible_by(current_user).count %>
<% collections = @user.collections.in_current_space.visible_by(current_user).limit(resource_limit) %>
<% collections_count = @user.collections.in_current_space.visible_by(current_user).count %>
<% activator = tab_activator %>

<ul class="nav nav-tabs">
<!-- Tab: Events -->
<% if feature_enabled?('events') %>
<%= tab('Events', icon_class_for_model('events'), 'events', activator: activator,
disabled: { check: events_count.zero?, message: 'No associated events' },
count: not_finished_events_count.zero? && events_count.positive? ? '0*' : not_finished_events_count) %>
<% end %>

<!-- Tab: Materials -->
<% if feature_enabled?('materials') %>
<%= tab('Materials', icon_class_for_model('materials'), 'materials', activator: activator,
disabled: { check: materials.none?, message: 'No registered training materials' },
count: materials_count) %>
<% end %>

<!-- Tab: Collections -->
<% if feature_enabled?('collections') %>
<%= tab('Collections', icon_class_for_model('collections'), 'collections', activator: activator,
disabled: { check: collections.none?, message: 'No registered collections' },
count: collections_count) %>
<% end %>

<!-- Tab: Workflows -->
<% if feature_enabled?('workflows') %>
<%= tab('Workflows', icon_class_for_model('workflows'), 'workflows', activator: activator,
disabled: { check: workflows.none?, message: 'No workflows' },
count: workflows_count) %>
<% end %>
</ul>

<div class="tab-content">
<!-- Tab: Events -->
<% if feature_enabled?('events') %>
<%= render partial: 'common/associated_events',
locals: { total_count: upcoming_events.count,
past_count: past_events.count,
limit: resource_limit,
resources: events,
activator: activator,
view_all_link: events_path(user: @user.username),
inc_expired_link: events_path(user: @user.username, include_expired: true) } %>
<% end %>

<!-- Tab: Materials -->
<% if feature_enabled?('materials') %>
<%= render partial: 'common/associated_resources',
locals: { model: Material,
total_count: materials_count,
limit: resource_limit,
resources: materials,
activator: activator,
view_all_link: materials_path(user: @user.username) } %>
<% end %>

<!-- Tab: Collections -->
<% if feature_enabled?('collections') %>
<%= render partial: 'common/associated_resources',
locals: { model: Collection,
total_count: collections_count,
limit: resource_limit,
resources: collections,
activator: activator,
view_all_link: collections_path(user: @user.username) } %>
<% end %>

<!-- Tab: Workflows -->
<% if feature_enabled?('workflows') %>
<%= render partial: 'common/associated_resources',
locals: { model: Workflow,
total_count: workflows_count,
limit: resource_limit,
resources: workflows,
activator: activator,
view_all_link: workflows_path(user: @user.username) } %>
<% end %>
</div> <!-- end tab-content -->
</div>
<!-- Registrations list: Events, Materials, Collections, Workflows -->
<%= render(partial: 'common/registrations_list', locals: { user: @user }) %>
</div>
</div>
49 changes: 49 additions & 0 deletions test/controllers/trainers_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
class TrainersControllerTest < ActionController::TestCase
include Devise::Test::ControllerHelpers

setup do
@user = users(:trainer_user)
@trainer = @user.profile
end

test 'get all trainers' do
get :index
assert_response :success
Expand All @@ -12,4 +17,48 @@ class TrainersControllerTest < ActionController::TestCase
assert_includes trainers, users(:trainer_user).profile
end

test 'should show material and event in trainer page as bioschemas JSON-LD' do
material = materials(:good_material)
material.user_id = @user.id
material.scientific_topic_uris = ['http://edamontology.org/topic_0654']
material.save!

event = events(:one)
event.user = @user
event.scientific_topic_uris = ['http://edamontology.org/topic_0654']
event.save!

get :show, params: { id: @trainer, format: :html }
assert_response :success
assert assigns(:trainer)

doc = Nokogiri::HTML(response.body)
jsonld = doc
.css('script[type="application/ld+json"]')
.map do |s|
JSON.parse(s.text)
rescue JSON::ParserError
nil
end
.compact

json_material = jsonld.find { |entry| entry['name'] == material.title }
assert_equal material.title, json_material['name']
assert_equal 'http://schema.org', json_material['@context']
assert_equal 'LearningResource', json_material['@type']
assert_equal 'https://bioschemas.org/profiles/TrainingMaterial/1.0-RELEASE', json_material['dct:conformsTo']['@id']
assert_equal material.url, json_material['url']
assert_equal material.scientific_topic_uris.first, json_material['about'].first['@id']

json_event = jsonld.find { |entry| entry['name'] == event.title }
assert_equal 'http://schema.org', json_event['@context']
assert_equal 'Course', json_event['@type']
assert_equal 'https://bioschemas.org/profiles/Course/1.0-RELEASE', json_event['dct:conformsTo']['@id']
assert_equal event.title, json_event['name']
assert_equal event.url, json_event['url']
assert_equal event.scientific_topic_uris.first, json_event['about'].first['@id']
external_resource = event.external_resources.first
assert_not_nil external_resource
assert_equal external_resource.url, json_event['mentions'].first['url']
end
end
45 changes: 45 additions & 0 deletions test/controllers/users_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,49 @@ class UsersControllerTest < ActionController::TestCase
assert_response :success
assert_select '#sidebar button', text: 'Authenticate your ORCID', count: 0
end

test 'should show material and event in trainer page as bioschemas JSON-LD' do
material = materials(:good_material)
material.user_id = @user.id
material.scientific_topic_uris = ['http://edamontology.org/topic_0654']
material.save!

event = events(:one)
event.user = @user
event.scientific_topic_uris = ['http://edamontology.org/topic_0654']
event.save!

get :show, params: { id: @user, format: :html }
assert_response :success
assert assigns(:user)

doc = Nokogiri::HTML(response.body)
jsonld = doc
.css('script[type="application/ld+json"]')
.map do |s|
JSON.parse(s.text)
rescue JSON::ParserError
nil
end
.compact

json_material = jsonld.find { |entry| entry['name'] == material.title }
assert_equal material.title, json_material['name']
assert_equal 'http://schema.org', json_material['@context']
assert_equal 'LearningResource', json_material['@type']
assert_equal 'https://bioschemas.org/profiles/TrainingMaterial/1.0-RELEASE', json_material['dct:conformsTo']['@id']
assert_equal material.url, json_material['url']
assert_equal material.scientific_topic_uris.first, json_material['about'].first['@id']

json_event = jsonld.find { |entry| entry['name'] == event.title }
assert_equal 'http://schema.org', json_event['@context']
assert_equal 'Course', json_event['@type']
assert_equal 'https://bioschemas.org/profiles/Course/1.0-RELEASE', json_event['dct:conformsTo']['@id']
assert_equal event.title, json_event['name']
assert_equal event.url, json_event['url']
assert_equal event.scientific_topic_uris.first, json_event['about'].first['@id']
external_resource = event.external_resources.first
assert_not_nil external_resource
assert_equal external_resource.url, json_event['mentions'].first['url']
end
end