Class: Collavre::Creatives::CreateService

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

Overview

Handles creative creation including sequencing and tagging.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(creative_params:, user:, child_id: nil, before_id: nil, after_id: nil, tag_ids: nil) ⇒ CreateService

Returns a new instance of CreateService.



9
10
11
12
13
14
15
16
# File 'app/services/collavre/creatives/create_service.rb', line 9

def initialize(creative_params:, user:, child_id: nil, before_id: nil, after_id: nil, tag_ids: nil)
  @creative_params = creative_params
  @user = user
  @child_id = child_id
  @before_id = before_id
  @after_id = after_id
  @tag_ids = Array(tag_ids).compact
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/collavre/creatives/create_service.rb', line 18

def call
  creative = Creative.new(@creative_params)
  creative.user = creative.parent ? creative.parent.user : @user

  unless creative.save
    return Result.new(creative: creative, success?: false, errors: creative.errors.full_messages)
  end

  reparent_child(creative)
  insert_at_position(creative)
  apply_tags(creative)

  # Broadcast AFTER insert_at_position so sequence and previous_sibling are correct
  creative.reload # pick up resequenced values
  creative.broadcast_creative_created(after_id: @after_id)

  Result.new(creative: creative, success?: true, errors: [])
end