Class: Spree::Products::PrepareNestedAttributes

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

Overview

Prepares nested attributes for product updates, handling multi-store scenarios and permissions.

This service ensures that when editing a product in one store, taxon associations from other stores are preserved. This prevents accidental data loss when a store admin updates product categories in their store without affecting other stores.

Examples:

service = Spree::Products::PrepareNestedAttributes.new(
  product,
  current_store,
  params,
  current_ability
)
prepared_params = service.call

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PrepareNestedAttributes.



20
21
22
23
24
25
# File 'app/services/spree/products/prepare_nested_attributes.rb', line 20

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

Instance Method Details

#callObject



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/services/spree/products/prepare_nested_attributes.rb', line 27

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?

  # Preserve taxon associations from other stores
  # Only merge taxon_ids from other stores if taxon_ids are being updated
  if params.key?(:taxon_ids)
    params[:taxon_ids] = merge_taxons_from_other_stores(params[:taxon_ids])
  end

  # 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