Skip to content

Commit

Permalink
update screenshot references in evidence when nodes are merged
Browse files Browse the repository at this point in the history
  • Loading branch information
caitmich committed Sep 4, 2024
1 parent 518e8c8 commit aeb4ae9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
16 changes: 9 additions & 7 deletions app/controllers/concerns/attachments_copier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ def copy_attachments(record)

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

record.content = record.content.gsub(full_screenshot_path, new_path)
record.content = updated_record_content(record.content, full_screenshot_path, new_attachment)
end
end
end

def updated_record_content(record_content, full_screenshot_path, new_attachment)
new_path = full_screenshot_path.gsub(
/nodes\/[0-9]+\/attachments\/.+/,
"nodes/#{new_attachment.node_id}/attachments/#{new_attachment.url_encoded_filename}"
)
record_content.gsub(full_screenshot_path, new_path)
end
end
26 changes: 23 additions & 3 deletions app/services/nodes/merger.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class Nodes::Merger
include AttachmentsCopier

def self.call(target_node, source_node)
new(target_node, source_node).call
end
Expand Down Expand Up @@ -90,15 +92,33 @@ def update_properties
end

def copy_attachments
self.copied_attachments = []
self.copied_attachments = {}

source_node.attachments.each do |attachment|
copied_attachments << attachment.copy_to(target_node)
new_attachment = attachment.copy_to(target_node)
copied_attachments[attachment.filename] = new_attachment
end
update_attachment_references
end

def update_attachment_references
target_node.evidence_ids.each do |evidence_id|
evidence = Evidence.find(evidence_id)
evidence.content.scan(Attachment::SCREENSHOT_REGEX).each do |screenshot_path|
full_screenshot_path, _, _, _, _, node_id, original_filename, _ = screenshot_path
# skip if the attachment already references the new node
next if node_id == target_node.id

new_attachment = copied_attachments[original_filename]
new_content = updated_record_content(evidence.content, full_screenshot_path, new_attachment)

evidence.update_attribute('content', new_content)
end
end
end

def undo_attachments_copy
return unless copied_attachments&.any?
copied_attachments.each(&:delete)
copied_attachments.values.each(&:delete)
end
end
30 changes: 30 additions & 0 deletions spec/services/nodes/merger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@
FileUtils.rm_rf(Dir.glob(Attachment.pwd + '*'))
end

it 'updates evidence screenshot links to point to new node' do
attachment = create(:attachment, node: source_node)
evidence = create(:evidence,
content: "#[Description]#\n\n!/pro/projects/#{source_node.project_id}/nodes/#{source_node.id}/attachments/#{attachment.url_encoded_filename}!",
node: source_node
)
merge_nodes

expect(evidence.reload.content)
.to include("/nodes/#{target_node.id}/attachments/#{attachment.url_encoded_filename}")
expect(evidence.reload.content)
.not_to include("/nodes/#{source_node.id}/attachments/#{attachment.url_encoded_filename}")

FileUtils.rm_rf(Dir.glob(Attachment.pwd + '*'))
end

describe 'property merges' do
it 'merges basic properties together' do
source_node = create(:node, :with_properties)
Expand Down Expand Up @@ -223,6 +239,20 @@

FileUtils.rm_rf(Dir.glob(Attachment.pwd + '*'))
end

it 'does not update evidence screenshot links to point to new node' do
attachment = create(:attachment, node: source_node)
evidence = create(:evidence,
content: "#[Description]#\n\n!/pro/projects/#{source_node.project_id}/nodes/#{source_node.id}/attachments/#{attachment.url_encoded_filename}!",
node: source_node
)
merge_nodes

expect(evidence.content).to include("/nodes/#{source_node.id}/attachments/#{attachment.url_encoded_filename}")
expect(evidence.content).not_to include("/nodes/#{target_node.id}/attachments/#{attachment.url_encoded_filename}")

FileUtils.rm_rf(Dir.glob(Attachment.pwd + '*'))
end
end
end
end

0 comments on commit aeb4ae9

Please sign in to comment.