Skip to content

Commit 9b195c2

Browse files
committed
Set comment attachment replacements on edit
When a comment is updated, replace attachments. https://community.openproject.org/wp/63384
1 parent 8214b31 commit 9b195c2

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

app/controllers/work_packages/activities_tab_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def update
129129
call = update_journal_service_call
130130

131131
if call.success? && call.result
132+
claim_journal_attachments_for(call.result)
132133
update_item_show_component(journal: call.result, grouped_emoji_reactions: grouped_emoji_reactions_for_journal)
133134
else
134135
handle_failed_create_or_update_call(call)

spec/features/activities/work_package/activity_tab_comment_editor_spec.rb

+38
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,43 @@ def expect_editor_to_be_dismissed_with_confirmation(&)
147147
journal = work_package.reload.journals.last
148148
expect(journal.attachments).to contain_exactly(attachment)
149149
end
150+
151+
context "when editing an existing comment" do
152+
let(:comment) do
153+
create(:work_package_journal,
154+
user: admin,
155+
notes: notes_with_attachment(existing_attachment),
156+
journable: work_package,
157+
version: 2).tap do |journal|
158+
journal.attachments << existing_attachment
159+
journal.save(validate: false)
160+
end
161+
end
162+
163+
let!(:existing_attachment) { create(:attachment, author: admin, container: nil) }
164+
165+
it "updates the comment with new attachments" do
166+
expect(comment.reload.attachments).to contain_exactly(existing_attachment)
167+
168+
activity_tab.edit_comment(comment, text: "Some notes", save: false)
169+
170+
editor.drag_attachment(image_fixture.path, "An image caption")
171+
editor.wait_until_upload_progress_toaster_cleared
172+
173+
click_on "Save"
174+
175+
expect(page).to have_content("An image caption")
176+
expect(comment.reload.attachments).to contain_exactly(Attachment.where(author: admin).last)
177+
expect(comment.reload.attachments).not_to include(existing_attachment)
178+
end
179+
180+
def notes_with_attachment(attachment)
181+
<<~HTML
182+
<img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{attachment.id}/content">
183+
184+
First attachment
185+
HTML
186+
end
187+
end
150188
end
151189
end

spec/services/work_packages/activities_tab/comment_attachments_claims/claims_service_spec.rb

+48
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,53 @@
104104
expect(journal.reload.attachments).to contain_exactly(attachment1, attachment2, attachment3)
105105
end
106106
end
107+
108+
context "with existing comment attachments" do
109+
let(:comment) do
110+
work_package.add_journal(user:, notes:)
111+
work_package.save(validate: false)
112+
work_package.journals.last
113+
end
114+
115+
let(:existing_attachment) { create(:attachment, author: user, container: nil) }
116+
let(:existing_attachment_excluded) { create(:attachment, author: user, container: nil) }
117+
let(:newly_attached) { create(:attachment, author: user, container: nil) }
118+
119+
let(:notes) do
120+
<<~HTML
121+
<img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{existing_attachment.id}/content">
122+
123+
First attachment
124+
125+
<br>
126+
127+
<img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{newly_attached.id}/content">
128+
129+
Second attachment
130+
HTML
131+
end
132+
133+
before do
134+
comment.attachments << [existing_attachment, existing_attachment_excluded]
135+
comment.save(validate: false)
136+
end
137+
138+
subject(:attachment_claims_service) do
139+
described_class.new(
140+
user: user,
141+
model: comment
142+
)
143+
end
144+
145+
it "replaces the comment attachments with newly update ones" do
146+
expect(comment.reload.attachments).to contain_exactly(existing_attachment, existing_attachment_excluded)
147+
148+
claim_result = attachment_claims_service.call
149+
expect(claim_result).to be_success
150+
151+
expect(comment.reload.attachments).to contain_exactly(existing_attachment, newly_attached)
152+
expect(Attachment.exists?(existing_attachment_excluded.id)).to be(false)
153+
end
154+
end
107155
end
108156
end

0 commit comments

Comments
 (0)