Skip to content

Commit d123e52

Browse files
committed
Ensure collections/lps with deleted items can be still be updated
1 parent 985e29e commit d123e52

File tree

7 files changed

+88
-10
lines changed

7 files changed

+88
-10
lines changed

app/assets/javascripts/templates/autocompleter/collection_item.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
<input type="hidden" name="{{ prefix }}[][id]" value="{{ item.id }}" />
1010
<input type="hidden" name="{{ prefix }}[][resource_id]" value="{{ item.resource_id }}" />
1111
<input type="hidden" name="{{ prefix }}[][resource_type]" value="{{ item.resource_type }}" />
12+
{{#if item.url}}
1213
<a href="{{ item.url }}" target="_blank">{{ item.title }}</a>
14+
{{ else }}
15+
<span class="muted">{{ item.title }}</span>
16+
{{/if}}
1317
<input type="text" class="form-control input-sm" name="{{ prefix }}[][comment]" placeholder="Enter a comment" value="{{ item.comment }}">
1418
</div>
1519

app/models/collection_item.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ def log_activity
1414
self.collection.create_activity(:add_item, owner: User.current_user,
1515
parameters: { resource_id: self.resource_id,
1616
resource_type: self.resource_type,
17-
resource_title: self.resource.title })
18-
self.resource.create_activity(:add_to_collection, owner: User.current_user,
19-
parameters: { collection_id: self.collection_id,
20-
collection_title: self.collection.title })
17+
resource_title: self.resource&.title })
18+
if self.resource
19+
self.resource.create_activity(:add_to_collection, owner: User.current_user,
20+
parameters: { collection_id: self.collection_id,
21+
collection_title: self.collection.title })
22+
end
2123
end
2224

2325
def reindex_resource

app/models/learning_path_topic_item.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ def log_activity
1313
self.topic.create_activity(:add_item, owner: User.current_user,
1414
parameters: { resource_id: self.resource_id,
1515
resource_type: self.resource_type,
16-
resource_title: self.resource.title })
17-
self.resource.create_activity(:add_to_topic, owner: User.current_user,
18-
parameters: { topic_id: self.topic_id,
19-
topic_title: self.topic.title })
16+
resource_title: self.resource&.title })
17+
if self.resource
18+
self.resource.create_activity(:add_to_topic, owner: User.current_user,
19+
parameters: { topic_id: self.topic_id,
20+
topic_title: self.topic.title })
21+
end
2022
end
2123

2224
def previous_item

app/views/collections/_collection_items_form.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
json = items.map do |item|
1616
{ item: { id: item.id, order: item.order, comment: item.comment,
1717
resource_id: item.resource_id, resource_type: item.resource_type,
18-
title: item.resource.title, url: polymorphic_path(item.resource) },
18+
title: item.resource&.title || t('deleted_resource'), url: item.resource ? polymorphic_path(item.resource) : nil },
1919
prefix: form_field_name }
2020
end.to_json
2121

app/views/public_activity/common/_add_item.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<% type = activity.parameters[:resource_type].constantize %>
22
<i class="fa fa-pencil-square-o"></i>
33
<%= activity_owner(activity) %>
4-
<%= t('activity.actions.add') %> <%= type.model_name.human.downcase %> <%= link_to activity.parameters[:resource_title],
4+
<%= t('activity.actions.add') %> <%= type.model_name.human.downcase %> <%= link_to activity.parameters[:resource_title] || t('deleted_resource'),
55
polymorphic_path(type.model_name.singular.to_sym,
66
id: activity.parameters[:resource_id]) %>
77
<%= t('activity.target_html', resource: activity_resource(activity)) %>

test/controllers/collections_controller_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,4 +750,42 @@ class CollectionsControllerTest < ActionController::TestCase
750750
assert_select 'h4', text: 'Deleted resource'
751751
end
752752
end
753+
754+
test 'can edit collection with deleted resource' do
755+
sign_in @collection.user
756+
757+
m1 = materials(:biojs)
758+
m2 = materials(:interpro)
759+
ci1 = @collection.items.create!(resource: materials(:biojs), order: 2, comment: 'hello')
760+
ci2 = @collection.items.create!(resource: materials(:interpro), order: 1, comment: 'hello!!')
761+
assert_no_difference('CollectionItem.count') do
762+
m1.destroy!
763+
end
764+
765+
get :edit, params: { id: @collection }
766+
assert_response :success
767+
end
768+
769+
test 'can update collection with deleted resource' do
770+
sign_in @collection.user
771+
772+
m1 = materials(:biojs)
773+
m2 = materials(:interpro)
774+
ci1 = @collection.items.create!(resource: materials(:biojs), order: 2, comment: 'hello')
775+
ci2 = @collection.items.create!(resource: materials(:interpro), order: 1, comment: 'hello!!')
776+
assert_no_difference('CollectionItem.count') do
777+
m1.destroy!
778+
end
779+
780+
params = { id: ci1.id, resource_type: ci1.resource_type, resource_id: ci1.resource_id }
781+
782+
patch :update, params: { id: @collection.id,
783+
collection: {
784+
items_attributes: { '1': params.merge(comment: 'Some comment') }
785+
}
786+
}
787+
788+
assert_redirected_to collection_path(assigns(:collection))
789+
assert_equal 'Some comment', ci1.reload.comment
790+
end
753791
end

test/controllers/learning_path_topics_controller_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,4 +566,36 @@ class LearningPathTopicsControllerTest < ActionController::TestCase
566566
assert_select 'h4', text: 'Deleted resource'
567567
end
568568
end
569+
570+
test 'can edit learning_path_topic with deleted resource' do
571+
sign_in @learning_path_topic.user
572+
573+
topic_item = @learning_path_topic.items.first
574+
assert_no_difference('LearningPathTopicItem.count') do
575+
topic_item.resource.destroy!
576+
end
577+
578+
get :edit, params: { id: @learning_path_topic }
579+
assert_response :success
580+
end
581+
582+
test 'can update learning_path_topic with deleted resource' do
583+
sign_in @learning_path_topic.user
584+
585+
topic_item = @learning_path_topic.items.first
586+
params = { id: topic_item.id, resource_type: topic_item.resource_type, resource_id: topic_item.resource_id }
587+
assert_no_difference('LearningPathTopicItem.count') do
588+
topic_item.resource.destroy!
589+
end
590+
591+
get :edit, params: { id: @learning_path_topic }
592+
patch :update, params: { id: @learning_path_topic.id,
593+
learning_path_topic: {
594+
items_attributes: { '1': params.merge(comment: 'Some comment') }
595+
}
596+
}
597+
598+
assert_redirected_to learning_path_topic_path(assigns(:learning_path_topic))
599+
assert_equal 'Some comment', topic_item.reload.comment
600+
end
569601
end

0 commit comments

Comments
 (0)