Class: Collavre::TopicBranchService

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre/topic_branch_service.rb

Defined Under Namespace

Classes: BranchError

Constant Summary collapse

MAX_BRANCH_COMMENTS =
100

Instance Method Summary collapse

Constructor Details

#initialize(creative:, user:, source_topic:) ⇒ TopicBranchService

Returns a new instance of TopicBranchService.



7
8
9
10
11
# File 'app/services/collavre/topic_branch_service.rb', line 7

def initialize(creative:, user:, source_topic:)
  @creative = creative
  @user = user
  @source_topic = source_topic
end

Instance Method Details

#call(comment_ids:) ⇒ Object

Creates a new topic with copies of the selected comments. Returns the new Topic.

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/services/collavre/topic_branch_service.rb', line 15

def call(comment_ids:)
  comment_ids = Array(comment_ids).map(&:presence).compact.map(&:to_i).first(MAX_BRANCH_COMMENTS)
  raise BranchError, I18n.t("collavre.comments.branch.no_selection") if comment_ids.empty?

  validate_permissions!
  originals = fetch_comments(comment_ids)

  Topic.transaction do
    @new_topic = create_branch_topic
    copy_comments(originals)
  end

  broadcast_topic_created

  @new_topic
end