Class: Collavre::Creatives::PlanTagger

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

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(plan_id:, creative_ids: [], user: nil) ⇒ PlanTagger

Returns a new instance of PlanTagger.



6
7
8
9
10
# File 'app/services/collavre/creatives/plan_tagger.rb', line 6

def initialize(plan_id:, creative_ids: [], user: nil)
  @plan = Plan.find_by(id: plan_id)
  @creative_ids = Array(creative_ids).map(&:presence).compact
  @user = user
end

Instance Method Details

#applyObject



12
13
14
15
16
17
18
19
20
# File 'app/services/collavre/creatives/plan_tagger.rb', line 12

def apply
  return failure("Please select a plan and at least one creative.") unless valid?

  creatives.find_each do |creative|
    creative.tags.find_or_create_by(label: plan, creative_id: creative.id)
  end

  success("Plan tags applied to selected creatives.")
end

#removeObject



22
23
24
25
26
27
28
29
30
31
# File 'app/services/collavre/creatives/plan_tagger.rb', line 22

def remove
  return failure("Please select a plan and at least one creative.") unless valid?

  creatives.find_each do |creative|
    tag = creative.tags.find_by(label: plan, creative_id: creative.id)
    tag&.destroy
  end

  success("Plan tag removed from selected creatives.")
end