Class: Docsmith::Comments::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/docsmith/comments/migrator.rb

Overview

Migrates top-level comments from one version to another. Document-level comments are copied as-is. Range-anchored comments are re-anchored using Anchor.migrate; orphaned comments fire the :comment_orphaned event.

Class Method Summary collapse

Class Method Details

.migrate!(document, from:, to:) ⇒ void

This method returns an undefined value.

Parameters:

  • document (Docsmith::Document)
  • from (Integer)

    source version_number

  • to (Integer)

    target version_number



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/docsmith/comments/migrator.rb', line 15

def migrate!(document, from:, to:)
  from_version = Docsmith::DocumentVersion.find_by!(document: document, version_number: from)
  to_version   = Docsmith::DocumentVersion.find_by!(document: document, version_number: to)
  new_content  = to_version.content.to_s

  from_version.comments.top_level.each do |comment|
    new_anchor_data = migrate_anchor(comment, new_content)

    new_comment = Comment.create!(
      version:          to_version,
      author_type:      comment.author_type,
      author_id:        comment.author_id,
      body:             comment.body,
      anchor_type:      comment.anchor_type,
      anchor_data:      new_anchor_data,
      resolved:         comment.resolved,
      resolved_by_type: comment.resolved_by_type,
      resolved_by_id:   comment.resolved_by_id,
      resolved_at:      comment.resolved_at
    )

    if orphaned?(comment, new_anchor_data)
      Events::Notifier.instrument(:comment_orphaned,
        record:   document.subject || document,
        document: document,
        version:  to_version,
        author:   nil,
        comment:  new_comment
      )
    end
  end
end