Class: AddOnServices::DuplicateAddOnService

Inherits:
Object
  • Object
show all
Includes:
DefaultErrorContainer, ImageHelper
Defined in:
app/services/add_on_services/duplicate_add_on_service.rb

Overview

Service to duplicate an add on This service will duplicate an add on and all its related records including agendas, pricing, kids prices, restaurant add ons, and add on menus

Examples:

old_add_on = AddOn.find(1)
service = AddOnServices::DuplicateAddOnService.new(old_add_on)
result = service.execute
if result.success?
  new_add_on = result.data
else
  puts result.message
end

Returns:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ImageHelper

#fix_image_url

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Constructor Details

#initialize(old_add_on) ⇒ DuplicateAddOnService

Returns a new instance of DuplicateAddOnService.



26
27
28
# File 'app/services/add_on_services/duplicate_add_on_service.rb', line 26

def initialize(old_add_on)
  @old_add_on = old_add_on
end

Instance Attribute Details

#new_add_onObject

Returns the value of attribute new_add_on.



24
25
26
# File 'app/services/add_on_services/duplicate_add_on_service.rb', line 24

def new_add_on
  @new_add_on
end

#old_add_onObject

Returns the value of attribute old_add_on.



24
25
26
# File 'app/services/add_on_services/duplicate_add_on_service.rb', line 24

def old_add_on
  @old_add_on
end

Instance Method Details

#dup_add_on_basic_infoObject



54
55
56
57
58
# File 'app/services/add_on_services/duplicate_add_on_service.rb', line 54

def dup_add_on_basic_info
  self.new_add_on = old_add_on.dup
  new_add_on.save!
  new_add_on
end

#dup_add_on_menusObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/services/add_on_services/duplicate_add_on_service.rb', line 90

def dup_add_on_menus
  old_add_on.add_on_menus.each do |old_add_on_menu|
    add_on_menu = old_add_on_menu.dup
    if old_add_on_menu.image.present? && old_add_on_menu.image.file.exists?
      req = Faraday.get(fix_image_url(old_add_on_menu.image_url))
      File.open("tmp/#{old_add_on_menu.id}.jpg", 'wb') { |fp| fp.write(req.body) }
      add_on_menu.image = File.open("tmp/#{old_add_on_menu.id}.jpg")
    else
      add_on_menu.image = ''
    end
    add_on_menu.add_on = new_add_on
    add_on_menu.save!
  end
end

#dup_agendasObject



60
61
62
63
64
65
66
# File 'app/services/add_on_services/duplicate_add_on_service.rb', line 60

def dup_agendas
  old_add_on.agendas.each do |old_agenda|
    agenda = old_agenda.dup
    agenda.add_on = new_add_on
    agenda.save!
  end
end

#dup_kids_pricesObject



74
75
76
77
78
79
80
# File 'app/services/add_on_services/duplicate_add_on_service.rb', line 74

def dup_kids_prices
  old_add_on.kids_prices.each do |old_kids_price|
    kids_price = old_kids_price.dup
    kids_price.add_on = new_add_on
    kids_price.save!
  end
end

#dup_pricingObject



68
69
70
71
72
# File 'app/services/add_on_services/duplicate_add_on_service.rb', line 68

def dup_pricing
  pricing = old_add_on.pricing.dup
  pricing.add_on = new_add_on
  pricing.save!
end

#dup_restaurant_add_onsObject



82
83
84
85
86
87
88
# File 'app/services/add_on_services/duplicate_add_on_service.rb', line 82

def dup_restaurant_add_ons
  old_add_on.restaurant_add_ons.each do |old_restaurant_add_on|
    restaurant_add_on = old_restaurant_add_on.dup
    restaurant_add_on.add_on = new_add_on
    restaurant_add_on.save!
  end
end

#executeObject

Executes the duplication of the add on

Returns a ServiceResult object with the following attributes:

  • data: The duplicated add on

  • errors: Any errors that occurred during the execution

  • message: A message indicating the result of the execution



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/add_on_services/duplicate_add_on_service.rb', line 36

def execute
  errors.clear
  return ServiceResult.new(errors: errors, message: 'Add on not found') if old_add_on.blank?

  ActiveRecord::Base.transaction do
    dup_add_on_basic_info
    dup_agendas
    dup_pricing
    dup_kids_prices
    dup_restaurant_add_ons
    dup_add_on_menus
  end
  ServiceResult.new(data: new_add_on)
rescue StandardError => e
  errors.add(:base, e.message)
  ServiceResult.new(errors: errors, message: e.message) || 'Failed to duplicate add on'
end