From d43726504b5dbf23da780a3dd0d087c8781fd1ca Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Thu, 4 Oct 2018 19:59:26 +0200 Subject: [PATCH] Make #update_attributes parseable by humans As it is this is impossible to follow. --- app/models/spree/product_set.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/app/models/spree/product_set.rb b/app/models/spree/product_set.rb index 368be9a556..3a40ea635d 100644 --- a/app/models/spree/product_set.rb +++ b/app/models/spree/product_set.rb @@ -17,14 +17,20 @@ class Spree::ProductSet < ModelSet # variant.update_attributes( { price: xx.x } ) # def update_attributes(attributes) - attributes[:taxon_ids] = attributes[:taxon_ids].split(',') if attributes[:taxon_ids].present? - e = @collection.detect { |e| e.id.to_s == attributes[:id].to_s && !e.id.nil? } - if e.nil? + if attributes[:taxon_ids].present? + attributes[:taxon_ids] = attributes[:taxon_ids].split(',') + end + + found_model = @collection.find do |model| + model.id.to_s == attributes[:id].to_s && model.persisted? + end + + if found_model.nil? @klass.new(attributes).save unless @reject_if.andand.call(attributes) else - ( attributes.except(:id, :variants_attributes, :master_attributes).present? ? e.update_attributes(attributes.except(:id, :variants_attributes, :master_attributes)) : true) and - (attributes[:variants_attributes] ? update_variants_attributes(e, attributes[:variants_attributes]) : true ) and - (attributes[:master_attributes] ? update_variant(e, attributes[:master_attributes]) : true ) + ( attributes.except(:id, :variants_attributes, :master_attributes).present? ? found_model.update_attributes(attributes.except(:id, :variants_attributes, :master_attributes)) : true) and + (attributes[:variants_attributes] ? update_variants_attributes(found_model, attributes[:variants_attributes]) : true ) and + (attributes[:master_attributes] ? update_variant(found_model, attributes[:master_attributes]) : true ) end end