Class: Spree::Products::PrepareNestedAttributes
- Inherits:
-
Object
- Object
- Spree::Products::PrepareNestedAttributes
- 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.
Instance Attribute Summary collapse
-
#variants_to_discontinue ⇒ Object
readonly
Returns the value of attribute variants_to_discontinue.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(product, store, params, ability) ⇒ PrepareNestedAttributes
constructor
A new instance of PrepareNestedAttributes.
Constructor Details
#initialize(product, store, params, ability) ⇒ PrepareNestedAttributes
Returns a new instance of PrepareNestedAttributes.
22 23 24 25 26 27 28 |
# File 'app/services/spree/products/prepare_nested_attributes.rb', line 22 def initialize(product, store, params, ability) @product = product @store = store @params = params @ability = ability @variants_to_discontinue = [] end |
Instance Attribute Details
#variants_to_discontinue ⇒ Object (readonly)
Returns the value of attribute variants_to_discontinue.
20 21 22 |
# File 'app/services/spree/products/prepare_nested_attributes.rb', line 20 def variants_to_discontinue @variants_to_discontinue end |
Instance Method Details
#call ⇒ Object
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 |
# File 'app/services/spree/products/prepare_nested_attributes.rb', line 30 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? variant_params.delete(:price) # remove legacy price param if can_update_prices? backfill_price_ids!(variant_params, existing_variant) 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 # 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] = {} populate_variants_to_discontinue variant_ids_to_destroy.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 |