Class: Spree::Products::PrepareNestedAttributes

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree/products/prepare_nested_attributes.rb

Instance Method Summary collapse

Constructor Details

#initialize(product, store, params, ability) ⇒ PrepareNestedAttributes

Returns a new instance of PrepareNestedAttributes.



4
5
6
7
8
9
# File 'app/services/spree/products/prepare_nested_attributes.rb', line 4

def initialize(product, store, params, ability)
  @product = product
  @store = store
  @params = params
  @ability = ability
end

Instance Method Details

#callObject



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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/services/spree/products/prepare_nested_attributes.rb', line 11

def call
  if params[:variants_attributes]
    params[:variants_attributes].each do |key, variant_params|
      existing_variant = variant_params[:id].presence && @product.variants.find_by(id: variant_params[:id])
      variants_to_remove.delete(variant_params[:id]) if variant_params[:id].present?

      if can_update_prices?
        # If the variant price is nil then mark it for destruction
        variant_params[:prices_attributes]&.each do |price_key, price_params|
          variant_params[:prices_attributes][price_key]['_destroy'] = '1' if price_params[:amount].blank?
        end
      else
        variant_params.delete(:prices_attributes)
      end

      variant_params[:option_value_variants_attributes] = update_option_value_variants(variant_params.delete(:options), existing_variant)

      variant_params.delete(:stock_items_attributes) unless can_update_stock_items?

      params[:variants_attributes].delete(key) if variant_params.blank?
    end
    params[:variants_attributes] = params[:variants_attributes].merge(removed_variants_attributes)

    params[:product_option_types_attributes] = product_option_types_params.merge(removed_product_option_types_attributes)
  elsif params[:master_attributes]
    params[:master_attributes].delete(:stock_items_attributes) unless can_update_stock_items?

    if can_update_prices?
      # If the master price is nil then mark it for destruction
      params.dig(:master_attributes, :prices_attributes)&.each do |price_key, price_params|
        params[:master_attributes][:prices_attributes][price_key]['_destroy'] = '1' if price_params[:amount].blank?
      end
    else
      params[:master_attributes].delete(:prices_attributes)
    end
  end

  # mark resource properties to be removed
  # when value is left blank
  if params[:product_properties_attributes].present?
    params[:product_properties_attributes].each do |key, product_property_params|
      next unless product_property_params[:id].present?
      next if product_property_params[:value].present?

      # https://api.rubyonrails.org/v7.1.3.4/classes/ActiveRecord/NestedAttributes/ClassMethods.html
      params[:product_properties_attributes][key]['_destroy'] = '1'
    end
  end

  # ensure there is at least one store
  params[:store_ids] = [store.id] if params[:store_ids].blank?

  # Add empty list for option_type_ids and mark variants as removed if there are no variants and options
  if params[:variants_attributes].blank? && variants_to_remove.any? && !params.key?(:option_type_ids)
    params[:option_type_ids] = []
    params[:variants_attributes] = {}

    variants_to_remove.each_with_index do |variant_id, index|
      params[:variants_attributes][index.to_s] = { id: variant_id, _destroy: '1' }
    end

    params[:variants_attributes].permit!
  end

  params
end