Class: Restaurants::DuplicateServiceWorker

Inherits:
ApplicationWorker show all
Includes:
ImageHelper
Defined in:
app/workers/restaurants/duplicate_service_worker.rb

Overview

typed: ignore frozen_string_literal: true Responsible to duplicate existing restaurant and its relationships

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ImageHelper

#fix_image_url

Methods inherited from ApplicationWorker

unlimited_retry

Instance Attribute Details

#restaurantObject (readonly)

Returns the value of attribute restaurant.



7
8
9
# File 'app/workers/restaurants/duplicate_service_worker.rb', line 7

def restaurant
  @restaurant
end

Instance Method Details

#perform(restaurant_id) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/workers/restaurants/duplicate_service_worker.rb', line 10

def perform(restaurant_id)
  @original_restaurant = Restaurant.find(restaurant_id)
  @restaurant = original_restaurant.dup

  ActiveRecord::Base.transaction do
    restaurant.city_id = original_restaurant.city_id
    owner = dup_owner(original_restaurant)
    raise("Owner is nil") if owner.blank?
    restaurant.owner = owner
    restaurant.rank = Restaurant.count + 1
    restaurant.active = false
    restaurant.reviews_score = 0.0
    restaurant.reviews_count = 0
    restaurant.old_link = "#{restaurant.old_link}-#{Time.zone.now.to_i}"
    restaurant.slug = "#{restaurant.slug}-#{Time.zone.now.to_i}"
    restaurant.save!(validate: false)
    dup_inventory_template_groups(restaurant, original_restaurant)
    dup_restaurant_tags(restaurant, original_restaurant)
    dup_primary_tags(restaurant, original_restaurant)
    dup_restaurant_tag_groups(restaurant, original_restaurant)
    dup_updated_inventories(restaurant, original_restaurant)
    dup_restaurant_external(restaurant, original_restaurant)
    dup_seo(restaurant, original_restaurant)
    dup_pictures(restaurant, original_restaurant)
    dup_managers(restaurant, original_restaurant)
    recalculate_weighted_score(restaurant)
  end

  true
end