Class: Docsmith::Comments::Manager

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

Overview

Service object for creating and resolving comments on document versions.

Class Method Summary collapse

Class Method Details

.add!(document, version_number:, body:, author:, anchor: nil, parent: nil) ⇒ Comments::Comment

Adds a comment to a specific version of a document.

Parameters:

  • document (Docsmith::Document)
  • version_number (Integer)
  • body (String)
  • author (Object)

    polymorphic author record

  • anchor (Hash, nil) (defaults to: nil)

    { start_offset:, end_offset: } for inline range comments

  • parent (Comments::Comment, nil) (defaults to: nil)

    parent comment for threading

Returns:

Raises:

  • (ActiveRecord::RecordNotFound)

    if version_number does not exist



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
47
48
49
# File 'lib/docsmith/comments/manager.rb', line 18

def add!(document, version_number:, body:, author:, anchor: nil, parent: nil)
  version = Docsmith::DocumentVersion.find_by!(document: document, version_number: version_number)

  anchor_type = anchor ? "range" : "document"
  anchor_data = if anchor
                  Anchor.build(version.content.to_s,
                               start_offset: anchor[:start_offset],
                               end_offset:   anchor[:end_offset])
                else
                  {}
                end

  comment = Comment.create!(
    version:     version,
    author:      author,
    body:        body,
    anchor_type: anchor_type,
    anchor_data: anchor_data,
    parent:      parent,
    resolved:    false
  )

  Events::Notifier.instrument(:comment_added,
    record:   document.subject || document,
    document: document,
    version:  version,
    author:   author,
    comment:  comment
  )

  comment
end

.resolve!(comment, by:) ⇒ Comments::Comment

Marks a comment as resolved.

Parameters:

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/docsmith/comments/manager.rb', line 56

def resolve!(comment, by:)
  comment.update!(resolved: true, resolved_by: by, resolved_at: Time.current)

  document = comment.version.document
  Events::Notifier.instrument(:comment_resolved,
    record:   document.subject || document,
    document: document,
    version:  comment.version,
    author:   by,
    comment:  comment
  )

  comment
end