Class: AddOnServices::DuplicateAddOnService
- Inherits:
-
Object
- Object
- AddOnServices::DuplicateAddOnService
- 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
Instance Attribute Summary collapse
-
#new_add_on ⇒ Object
Returns the value of attribute new_add_on.
-
#old_add_on ⇒ Object
Returns the value of attribute old_add_on.
Instance Method Summary collapse
- #dup_add_on_basic_info ⇒ Object
- #dup_add_on_menus ⇒ Object
- #dup_agendas ⇒ Object
- #dup_kids_prices ⇒ Object
- #dup_pricing ⇒ Object
- #dup_restaurant_add_ons ⇒ Object
-
#execute ⇒ Object
Executes the duplication of the add on.
-
#initialize(old_add_on) ⇒ DuplicateAddOnService
constructor
A new instance of DuplicateAddOnService.
Methods included from ImageHelper
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_on ⇒ Object
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_on ⇒ Object
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_info ⇒ Object
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_menus ⇒ Object
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 old_add_on..each do || = .dup if .image.present? && .image.file.exists? req = Faraday.get(fix_image_url(.image_url)) File.open("tmp/#{.id}.jpg", 'wb') { |fp| fp.write(req.body) } .image = File.open("tmp/#{.id}.jpg") else .image = '' end .add_on = new_add_on .save! end end |
#dup_agendas ⇒ Object
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_prices ⇒ Object
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_pricing ⇒ Object
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_ons ⇒ Object
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 |
#execute ⇒ Object
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 end ServiceResult.new(data: new_add_on) rescue StandardError => e errors.add(:base, e.) ServiceResult.new(errors: errors, message: e.) || 'Failed to duplicate add on' end |