Skip to content

Commit

Permalink
Add AttachmentsCopier concern
Browse files Browse the repository at this point in the history
  • Loading branch information
aapomm committed Jun 18, 2024
1 parent d9327ba commit f352a1c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[v#.#.#] ([month] [YYYY])
- Attachments: Copy attachments when moving an evidence/note
- Liquid: Make project-level collections available for Liquid syntax
- Upgraded gems: nokogiri, rails, rexml
- Bugs fixes:
Expand Down
19 changes: 19 additions & 0 deletions app/controllers/concerns/attachments_copier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module AttachmentsCopier
def copy_attachments(record)
record.content.scan(Attachment::SCREENSHOT_REGEX).each do |screenshot_url|
_, _, _, _, project_id, node_id, filename, _ = screenshot_url

attachment = Attachment.find_by(filename: filename, node_id: record.node_id_was)

if attachment
new_attachment = attachment.copy_to(record.node)
new_url = screenshot_url[0].gsub(
/nodes\/[0-9]+\/attachments/,
"nodes/#{new_attachment.node_id}/attachments"
)

record.content = record.content.gsub(screenshot_url[0], new_url)
end
end
end
end
3 changes: 3 additions & 0 deletions app/controllers/evidence_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class EvidenceController < NestedNodeResourceController
include AttachmentsCopier
include ConflictResolver
include EvidenceHelper
include LiquidEnabledResource
Expand Down Expand Up @@ -55,6 +56,8 @@ def update
@evidence.assign_attributes(evidence_params)
autogenerate_issue if evidence_params[:issue_id].blank?

copy_attachments(@evidence) if @evidence.node_changed?

if @evidence.save
track_updated(@evidence)
check_for_edit_conflicts(@evidence, updated_at_before_save)
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# This controller exposes the REST operations required to manage the Note
# resource.
class NotesController < NestedNodeResourceController
include AttachmentsCopier
include ConflictResolver
include LiquidEnabledResource
include Mentioned
Expand Down Expand Up @@ -44,7 +45,11 @@ def edit
# Update the attributes of a Note
def update
updated_at_before_save = @note.updated_at.to_i
if @note.update(note_params)

@note.assign_attributes(note_params)
copy_attachments(@note) if @note.node_changed?

if @note.save
track_updated(@note)
check_for_edit_conflicts(@note, updated_at_before_save)
# if the note has just been moved to another node, we must reload
Expand Down
2 changes: 2 additions & 0 deletions app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class Attachment < File
AttachmentPwd = Rails.env.test? ? Rails.root.join('tmp', 'attachments') : Rails.root.join('attachments')
FileUtils.mkdir_p(AttachmentPwd) unless File.exists?(AttachmentPwd)

SCREENSHOT_REGEX = /\!((https?:\/\/.+)|((\/pro)?\/projects\/(\d+)\/nodes\/(\d+)\/attachments\/(.+?)(\(.*\))?))\!/i

# -- Class Methods ---------------------------------------------------------

def self.all(*args)
Expand Down
10 changes: 9 additions & 1 deletion spec/features/evidence_moving_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ def create_node(label, parent = nil)
click_move_evidence
end

let(:current_evidence) { @evidence = create(:evidence, node: @node_5) }
let(:content) { "#[Description]#\nTest Evidence\n" }
let(:current_evidence) { @evidence = create(:evidence, content: content, node: @node_5) }

describe 'moving an evidence to a different node' do
let(:attachment) { create(:attachment, node: @node_5) }
let(:content) { "#[Description]#\n!/projects/#{current_project.id}/nodes/#{@node_5.id}/attachments/#{attachment.filename}!\n" }

before do
within('#modal_move_evidence') do
click_link @node_1.label
Expand All @@ -46,6 +50,10 @@ def create_node(label, parent = nil)
it 'should redirect to evidence show path' do
expect(current_path).to eq(project_node_evidence_path(current_project, @node_1, current_evidence))
end

it 'should update the attachment reference to the new node' do
expect(current_evidence.reload.content).to include("nodes/#{@node_1.id}")
end
end

describe 'moving a evidence to a similar node', js: true do
Expand Down
10 changes: 9 additions & 1 deletion spec/features/note_moving_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ def create_node(label, parent = nil)
click_move_note
end

let(:current_note) { @note = create(:note, node: @node_5) }
let(:text) { "#[Description]#\nTest Note\n" }
let(:current_note) { @note = create(:note, text: text, node: @node_5) }

describe 'moving a note to a different node' do
let(:attachment) { create(:attachment, node: @node_5) }
let(:text) { "#[Description]#\n!/projects/#{current_project.id}/nodes/#{@node_5.id}/attachments/#{attachment.filename}!\n" }

before do
within('#modal_move_note') do
click_link @node_1.label
Expand All @@ -47,6 +51,10 @@ def create_node(label, parent = nil)
it 'should redirect to note show path' do
expect(current_path).to eq(project_node_note_path(current_project, @node_1, current_note))
end

it 'should update the attachment reference to the new node' do
expect(current_note.reload.content).to include("nodes/#{@node_1.id}")
end
end

describe 'moving a note to a similar node' do
Expand Down

0 comments on commit f352a1c

Please sign in to comment.