Class: Workarea::BulkAction::ProductEdit

Inherits:
Workarea::BulkAction show all
Defined in:
app/models/workarea/bulk_action/product_edit.rb

Instance Method Summary collapse

Methods inherited from Workarea::BulkAction

#admin_query, #completed!, #completed?, #convert_query_to_ids, #perform!, #reset_to_default!

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

assert_valid_config!, async, disable, enable, inline, #run_callbacks

Methods included from Mongoid::Document

#embedded_children

Instance Method Details

#act_on!(product) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/workarea/bulk_action/product_edit.rb', line 20

def act_on!(product)
  Release.with_current(release_id) do
    apply_tags!(product)
    apply_filters!(product)
    apply_details!(product)
    apply_pricing!(product)
    apply_inventory!(product)

    product.update_attributes!(settings)
  end
end

#apply_details!(product) ⇒ Object



38
39
40
41
42
# File 'app/models/workarea/bulk_action/product_edit.rb', line 38

def apply_details!(product)
  HashUpdate
    .new(adds: add_details, removes: remove_details)
    .apply(product.details)
end

#apply_filters!(product) ⇒ Object



44
45
46
47
48
# File 'app/models/workarea/bulk_action/product_edit.rb', line 44

def apply_filters!(product)
  HashUpdate
    .new(adds: add_filters, removes: remove_filters)
    .apply(product.filters)
end

#apply_inventory!(product) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'app/models/workarea/bulk_action/product_edit.rb', line 72

def apply_inventory!(product)
  return unless inventory.present?

  collection = Inventory::Collection.new(product.skus)

  collection.records.each do |record|
    record.update_attributes!(inventory)
  end
end

#apply_pricing!(product) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/workarea/bulk_action/product_edit.rb', line 50

def apply_pricing!(product)
  return unless pricing.present?

  pricing_skus(product).each do |sku|
    sku_pricing = pricing.dup
    prices = sku_pricing.delete('prices') unless sku.prices.blank?
    sku.update_attributes!(sku_pricing)

    if prices.present?
      sku.prices.select(&:generic?).each do |price|
        price.update_attributes!(prices.first)
      end
    end
  end
end

#apply_tags!(product) ⇒ Object



32
33
34
35
36
# File 'app/models/workarea/bulk_action/product_edit.rb', line 32

def apply_tags!(product)
  TagUpdate
    .new(adds: add_tags, removes: remove_tags)
    .apply(product.tags)
end

#pricing_skus(product) ⇒ Object



66
67
68
69
70
# File 'app/models/workarea/bulk_action/product_edit.rb', line 66

def pricing_skus(product)
  existing = Pricing::Sku.in(id: product.skus).to_a
  missing_skus = product.skus - existing.map(&:id)
  existing + missing_skus.map { |sku| Pricing::Sku.new(id: sku) }
end