Skip to content

Commit e67ce73

Browse files
authored
Merge pull request #1251 from pan-training/clickable_scientific_topics
Clickable scientific topics
2 parents 8241ac0 + bb52234 commit e67ce73

7 files changed

Lines changed: 227 additions & 169 deletions

File tree

app/assets/stylesheets/application.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,3 +1111,7 @@ td.day .calendar-text {
11111111
display: flex;
11121112
justify-content: center;
11131113
}
1114+
1115+
.tag-topic, .tag-operation {
1116+
text-decoration: none;
1117+
}

app/helpers/materials_helper.rb

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,15 @@ def display_attribute(resource, attribute, show_label: true, title: nil, markdow
7171
].any?
7272

7373
value = resource.send(attribute)
74-
if markdown
75-
value = render_markdown(value)
76-
end
74+
value = render_markdown(value) if markdown
7775
if value.present?
78-
if list
79-
value = value.map do |v|
80-
html_escape(block_given? ? yield(v) : v)
81-
end
82-
else
83-
value = html_escape(block_given? ? yield(value) : value)
84-
end
76+
value = if list
77+
value.map do |v|
78+
html_escape(block_given? ? yield(v) : v)
79+
end
80+
else
81+
html_escape(block_given? ? yield(value) : value)
82+
end
8583
end
8684
string = "<p class=\"#{attribute}#{show_label ? ' no-spacing' : ''}\">"
8785
unless value.blank? || value.try(:strip) == 'License Not Specified'
@@ -135,18 +133,31 @@ def embed_youtube(material)
135133
def keywords_and_topics(resource, limit: nil)
136134
tags = []
137135

138-
%i[scientific_topic_names operation_names keywords].each do |field|
139-
tags |= resource.send(field) if resource.respond_to?(field)
136+
if resource.respond_to?(:scientific_topics)
137+
tags += resource.scientific_topics.map do |term|
138+
content_tag(:span, class: 'label label-info tag-topic') do
139+
content_tag(:i, '', class: 'fa fa-flask') + ' ' + term.preferred_label
140+
end
141+
end
140142
end
141143

142-
limit_exceeded = limit && (tags.length > limit)
143-
tags = tags.first(limit) if limit
144+
if resource.respond_to?(:operations)
145+
tags += resource.operations.map do |term|
146+
content_tag(:span, class: 'label label-info tag-operation') do
147+
content_tag(:i, '', class: 'fa fa-cogs') + ' ' + term.preferred_label
148+
end
149+
end
150+
end
144151

145-
elements = tags.map do |tag|
146-
content_tag(:span, tag, class: 'label label-info')
152+
if resource.respond_to?(:keywords)
153+
tags += resource.keywords.map do |tag|
154+
content_tag(:span, tag, class: 'label label-info tag-keyword')
155+
end
147156
end
148-
elements << '&hellip;' if limit_exceeded
149157

150-
elements.join(' ').html_safe
158+
limit_exceeded = limit && (tags.length > limit)
159+
tags = tags.first(limit) if limit
160+
tags << '&hellip;'.html_safe if limit_exceeded
161+
safe_join(tags, ' ')
151162
end
152163
end

app/views/common/_extra_metadata.html.erb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363
<%= display_people(resource, :contributors) if resource.respond_to?(:contributors) %>
6464
<%= display_attribute(resource, :remote_created_date) if resource.respond_to?(:remote_created_date) %>
6565
<%= display_attribute(resource, :remote_updated_date) if resource.respond_to?(:remote_updated_date) %>
66-
<%= display_attribute(resource, :scientific_topics) { |values| values.map { |x| x.preferred_label }.join(', ') } %>
66+
<%= display_attribute(resource, :scientific_topics) {
67+
|values| safe_join(values.map { |x| link_to(x.preferred_label, x.uri, target: '_blank') }, ', ')
68+
} %>
6769

6870
<% if resource.is_a?(LearningPath) %>
6971
<%= display_attribute(resource, :status) { |value| material_status_title_for_label(value) } %>
@@ -73,5 +75,7 @@
7375
<% end %>
7476

7577
<% if resource.respond_to?(:operations) -%>
76-
<%= display_attribute(resource, :operations) { |values| values.map { |x| x.preferred_label }.join(', ') } %>
78+
<%= display_attribute(resource, :operations) {
79+
|values| safe_join(values.map { |x| link_to(x.preferred_label, x.uri, target: '_blank') }, ', ')
80+
} %>
7781
<% end %>

test/controllers/learning_paths_controller_test.rb

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class LearningPathsControllerTest < ActionController::TestCase
1212
description: 'New description'
1313
}
1414
end
15-
#INDEX TESTS
15+
# INDEX TESTS
1616
test 'should get index' do
1717
get :index
1818
assert_response :success
@@ -49,7 +49,7 @@ class LearningPathsControllerTest < ActionController::TestCase
4949
assert_equal learning_paths_path, body['links']['self']
5050
end
5151

52-
#NEW TESTS
52+
# NEW TESTS
5353

5454
test 'should get new page for curators and admins only' do
5555
get :new
@@ -72,14 +72,14 @@ class LearningPathsControllerTest < ActionController::TestCase
7272
assert_response :success
7373
end
7474

75-
#EDIT TESTS
75+
# EDIT TESTS
7676
test 'should not get edit page for not logged in users' do
77-
#Not logged in = Redirect to login
77+
# Not logged in = Redirect to login
7878
get :edit, params: { id: @learning_path }
7979
assert_redirected_to new_user_session_path
8080
end
8181

82-
#logged in but insufficient permissions = ERROR
82+
# logged in but insufficient permissions = ERROR
8383
test 'should get edit for learning_path owner' do
8484
sign_in @learning_path.user
8585
get :edit, params: { id: @learning_path }
@@ -93,20 +93,20 @@ class LearningPathsControllerTest < ActionController::TestCase
9393
end
9494

9595
test 'should get edit for admin' do
96-
#Owner of learning_path logged in = SUCCESS
96+
# Owner of learning_path logged in = SUCCESS
9797
sign_in users(:admin)
9898
get :edit, params: { id: @learning_path }
9999
assert_response :success
100100
end
101101

102102
test 'should not get edit page for regular user' do
103-
#Administrator = SUCCESS
103+
# Administrator = SUCCESS
104104
sign_in users(:another_regular_user)
105105
get :edit, params: { id: @learning_path }
106106
assert :forbidden
107107
end
108108

109-
#CREATE TEST
109+
# CREATE TEST
110110
test 'should create learning_path for curator' do
111111
sign_in users(:curator)
112112
assert_difference('LearningPath.count') do
@@ -146,7 +146,7 @@ class LearningPathsControllerTest < ActionController::TestCase
146146
assert_redirected_to new_user_session_path
147147
end
148148

149-
#SHOW TEST
149+
# SHOW TEST
150150
test 'should show learning_path' do
151151
get :show, params: { id: @learning_path }
152152
assert_response :success
@@ -183,21 +183,22 @@ class LearningPathsControllerTest < ActionController::TestCase
183183
assert_equal learning_path_path(assigns(:learning_path)), body['data']['links']['self']
184184
end
185185

186-
#UPDATE TEST
186+
# UPDATE TEST
187187
test 'should update learning_path' do
188188
sign_in @learning_path.user
189189
patch :update, params: { id: @learning_path, learning_path: @updated_learning_path }
190190
assert_redirected_to learning_path_path(assigns(:learning_path))
191191
end
192192

193-
test "should add topics to learning_path" do
193+
test 'should add topics to learning_path' do
194194
sign_in @learning_path.user
195195
learning_path = learning_paths(:two)
196196
assert_no_difference('LearningPathTopic.count') do
197197
assert_difference('LearningPathTopicLink.count', 1) do
198198
assert_difference('learning_path.topics.count', 1) do
199199
patch :update, params: { learning_path: {
200-
topic_links_attributes: { '1': { topic_id: learning_path_topics(:goblet_things).id, order: 300 } } },
200+
topic_links_attributes: { '1': { topic_id: learning_path_topics(:goblet_things).id, order: 300 } }
201+
},
201202
id: learning_path.id }
202203
end
203204
end
@@ -211,14 +212,15 @@ class LearningPathsControllerTest < ActionController::TestCase
211212
assert_equal learning_path_topics(:goblet_things), links[1].topic
212213
end
213214

214-
test "should remove topic from learning_path" do
215+
test 'should remove topic from learning_path' do
215216
sign_in @learning_path.user
216217
learning_path = learning_paths(:two)
217218
assert_no_difference('LearningPathTopic.count') do
218219
assert_difference('LearningPathTopicLink.count', -1) do
219220
assert_difference('learning_path.topics.count', -1) do
220221
patch :update, params: { learning_path: {
221-
topic_links_attributes: { '1': { id: learning_path.topic_link_ids.first, _destroy: '1' } } },
222+
topic_links_attributes: { '1': { id: learning_path.topic_link_ids.first, _destroy: '1' } }
223+
},
222224
id: learning_path.id }
223225
end
224226
end
@@ -227,7 +229,7 @@ class LearningPathsControllerTest < ActionController::TestCase
227229
assert_empty assigns(:learning_path).topic_links
228230
end
229231

230-
test "should modify items in learning_path" do
232+
test 'should modify items in learning_path' do
231233
sign_in @learning_path.user
232234

233235
l1 = @learning_path.topic_links[0]
@@ -248,7 +250,7 @@ class LearningPathsControllerTest < ActionController::TestCase
248250
end
249251
end
250252

251-
#DESTROY TEST
253+
# DESTROY TEST
252254
test 'should destroy learning_path owned by user' do
253255
sign_in @learning_path.user
254256
assert_difference('LearningPath.count', -1) do
@@ -273,9 +275,8 @@ class LearningPathsControllerTest < ActionController::TestCase
273275
assert_response :forbidden
274276
end
275277

276-
277-
#CONTENT TESTS
278-
#BREADCRUMBS
278+
# CONTENT TESTS
279+
# BREADCRUMBS
279280
test 'breadcrumbs for learning_paths index' do
280281
get :index
281282
assert_response :success
@@ -326,7 +327,7 @@ class LearningPathsControllerTest < ActionController::TestCase
326327
end
327328
end
328329

329-
#OTHER CONTENT
330+
# OTHER CONTENT
330331
test 'learning path lists topics' do
331332
sign_in(users(:regular_user))
332333

@@ -344,17 +345,17 @@ class LearningPathsControllerTest < ActionController::TestCase
344345
get :show, params: { id: @learning_path }
345346

346347
assert_response :success
347-
assert_select 'a.btn[href=?]', edit_learning_path_path(@learning_path), count: 0 #No Edit
348-
assert_select 'a.btn[href=?]', learning_path_path(@learning_path), count: 0 #No Edit
348+
assert_select 'a.btn[href=?]', edit_learning_path_path(@learning_path), count: 0 # No Edit
349+
assert_select 'a.btn[href=?]', learning_path_path(@learning_path), count: 0 # No Edit
349350
end
350351

351352
test 'do not show action buttons when not owner or admin' do
352353
sign_in users(:another_regular_user)
353354

354355
get :show, params: { id: @learning_path }
355356

356-
assert_select 'a.btn[href=?]', edit_learning_path_path(@learning_path), count: 0 #No Edit
357-
assert_select 'a.btn[href=?]', learning_path_path(@learning_path), count: 0 #No Edit
357+
assert_select 'a.btn[href=?]', edit_learning_path_path(@learning_path), count: 0 # No Edit
358+
assert_select 'a.btn[href=?]', learning_path_path(@learning_path), count: 0 # No Edit
358359
end
359360

360361
test 'show action buttons when learning_path_curator' do
@@ -375,7 +376,7 @@ class LearningPathsControllerTest < ActionController::TestCase
375376
assert_select 'a.btn[href=?]', learning_path_path(@learning_path), text: 'Delete', count: 1
376377
end
377378

378-
#API Actions
379+
# API Actions
379380
test 'should not allow access to private learning_paths' do
380381
sign_in users(:regular_user)
381382
get :show, params: { id: learning_paths(:in_development_learning_path) }
@@ -453,11 +454,11 @@ class LearningPathsControllerTest < ActionController::TestCase
453454

454455
assert_difference('LearningPathTopicLink.count', -1) do
455456
patch :update, params: { learning_path: {
456-
topic_links_attributes: { '1': { id: @learning_path.topic_link_ids.first, _destroy: '1' } } },
457+
topic_links_attributes: { '1': { id: @learning_path.topic_link_ids.first, _destroy: '1' } }
458+
},
457459
id: @learning_path.id }
458460
assert_redirected_to learning_path_path(assigns(:learning_path))
459461
end
460-
461462
end
462463

463464
test 'should not allow non-collaborator to edit' do
@@ -511,10 +512,10 @@ class LearningPathsControllerTest < ActionController::TestCase
511512
end
512513

513514
response_materials = body.dig('data', 'relationships', 'materials', 'data')
514-
assert_equal [materials[1].id, materials[0].id, materials[2].id], response_materials.map { |m| m['id'].to_i }
515+
assert_equal([materials[1].id, materials[0].id, materials[2].id], response_materials.map { |m| m['id'].to_i })
515516

516517
response_events = body.dig('data', 'relationships', 'events', 'data')
517-
assert_equal [events[1].id, events[0].id], response_events.map { |e| e['id'].to_i }
518+
assert_equal([events[1].id, events[0].id], response_events.map { |e| e['id'].to_i })
518519
end
519520

520521
test 'should include back-reference to learning path in item links' do

0 commit comments

Comments
 (0)