lib/loaders/spree/product_loader.rb
def process(method_detail, value)
        begin


        ProductLoader.log_g "Process =" +  method_detail.name.to_s

        raise ProductLoadError.new("Cannot process #{value} NO details found to assign to") unless(method_detail)

        current_value, current_attribute_hash = @populator.prepare_data(method_detail, value)

        current_method_detail = method_detail


        logger.info "Processing value: [#{current_value}]"


        if(current_value && (current_method_detail.operator?('sku')))
          @sku_to_remember= get_each_assoc.first
          ProductLoader.log_g "SKU=" +  @sku_to_remember.to_s
        end

        if(current_value && (current_method_detail.operator?('slug')))

          #Post.includes(:votes)
          #Employee.
          #joins(:company => :addresses).
           #   where(:addresses => { :city => 'Porto Alegre' })


          spree=  Spree::Variant.joins(:product).where("spree_variants.product_id = spree_products.id and spree_variants.is_master = ? and spree_variants.sku = ? and spree_products.slug= ? ",'True',@sku_to_remember,get_each_assoc.first)

         #object_to_update =  @load_object_class.joins(:variants).find_by(:slug=> get_each_assoc.first,:variants => {:sku =>@load_object_class.master.sku })

         if spree.count > 0

           #ProductLoader.log_g
           ProductLoader.log_g "ID = "  + spree.first.attributes["product_id"].to_s
           object_to_update = Spree::Product.find(spree.first.attributes["product_id"])
           object_to_update.name = @load_object.name
           object_to_update.description = @load_object.description
           object_to_update.master.sku =@sku_to_remember
           object_to_update.shipping_category = Spree::ShippingCategory.find_by_name!("Default")

           @load_object = object_to_update
         end

        end

        if(current_value && (current_method_detail.operator?('stock')))
            add_items_on_hand
          return
        end



        if(current_value && (current_method_detail.name =='price_ru' || current_method_detail.name =='price_eu' ) )
          add_additional_prices current_method_detail
           return
        end

        if(current_value && (current_method_detail.name =='translation'))
          add_translation
          return

        end

        #HAI

        if(current_value && (current_method_detail.name =='taxons_ru'))
          # extract_ru_taxons
          return
        end

        # Special cases for Products, generally where a simple one stage lookup won't suffice
        # otherwise simply use default processing from base class
        if(current_value && (current_method_detail.operator?('variants') || current_method_detail.operator?('option_types')) )

          add_options_variants

        elsif(current_method_detail.operator?('taxons') && current_value)
          add_taxons

        elsif(current_method_detail.operator?('product_properties') && current_value)

          add_properties

        elsif(current_method_detail.operator?('images') && current_value)

          add_images( (SpreeHelper::version.to_f > 1) ? @load_object.master : @load_object )

        elsif(current_method_detail.operator?('variant_price') && current_value)
          puts "Newly Loaded Object Size #{@load_object.variants.size}"

          if(@load_object.variants.size > 0)

            if(current_value.to_s.include?(Delimiters::multi_assoc_delim))

              # Check if we processed Option Types and assign  per option
              values = current_value.to_s.split(Delimiters::multi_assoc_delim)

              if(@load_object.variants.size == values.size)
                @load_object.variants.each_with_index do |v, i|
                  v.price = values[i].to_f
                  v.save
                end
              else
                puts "WARNING: Price entries did not match number of Variants - None Set"
              end
            end

          else
            super
          end

        elsif(current_method_detail.operator?('variant_sku') && current_value)

          if(@load_object.variants.size > 0)

            if(current_value.to_s.include?(Delimiters::multi_assoc_delim))

              # Check if we processed Option Types and assign  per option
              values = current_value.to_s.split(Delimiters::multi_assoc_delim)

              if(@load_object.variants.size == values.size)
                @load_object.variants.each_with_index {|v, i| v.sku = values[i].to_s }
                @load_object.save
              else
                puts "WARNING: SKU entries did not match number of Variants - None Set"
              end
            end

          else
            super
          end

        elsif(current_value && (current_method_detail.operator?('count_on_hand') || current_method_detail.operator?('on_hand')) )

          # CURRENTLY BROKEN FOR Spree 2.2 - New Stock management :
          # http://guides.spreecommerce.com/developer/inventory.html

          logger.warn("NO STOCK SET - count_on_hand BROKEN - needs updating for new StockManagement in Spree >= 2.2")
          # return

          # Unless we can save here, in danger of count_on_hand getting wiped out.
          # If we set (on_hand or count_on_hand) on an unsaved object, during next subsequent save
          # looks like some validation code or something calls Variant.on_hand= with 0
          # If we save first, then our values seem to stick

          # TODO smart column ordering to ensure always valid - if we always make it very last column might not get wiped ?
          #
          save_if_new


          # Spree has some stock management stuff going on, so dont usually assign to column vut use
          # on_hand and on_hand=
          if(@load_object.variants.size > 0)

            if(current_value.to_s.include?(Delimiters::multi_assoc_delim))

              #puts "DEBUG: COUNT_ON_HAND PER VARIANT",current_value.is_a?(String),

              # Check if we processed Option Types and assign count per option
              values = current_value.to_s.split(Delimiters::multi_assoc_delim)

              if(@load_object.variants.size == values.size)
                @load_object.variants.each_with_index {|v, i| v.on_hand = values[i].to_i }
                @load_object.save
              else
                puts "WARNING: #{values.size} count on hand entries #{current_value} did not match #{@load_object.variants.size} variants - None Set"
              end
            end

            # Can only set count on hand on Product if no Variants exist, else model throws

          elsif(@load_object.variants.size == 0)
            if(current_value.to_s.include?(Delimiters::multi_assoc_delim))
              puts "WARNING: Multiple count_on_hand values specified but no Variants/OptionTypes created"
              load_object.on_hand = current_value.to_s.split(Delimiters::multi_assoc_delim).first.to_i
            else
              load_object.on_hand = current_value.to_i
            end
          end

        else
          super
        end
        rescue Exception => ex
          ProductLoader.log_g "Message =#{ex.message} backtrace = #{ex.backtrace} "
          throw ex
      end
      end
# Over ride base class process with some Spree::Product specifics
#
# What process a value string from a column, assigning value(s) to correct association on Product.
# Method map represents a column from a file and it's correlated Product association.
# Value string which may contain multiple values for a collection (has_many) association.
#
Over ride base class process with some Spree::Product specifics
What process a value string from a column, assigning value(s) to correct association on Product.
Method map represents a column from a file and it's correlated Product association.
Value string which may contain multiple values for a collection (has_many) association.
Over ride base class process with some Spree :: Product specifics What process a value string from a column assigning value ( s ) to correct association on Product . Method map represents a column from a file and it ' s correlated Product association . Value string which may contain multiple values for a collection ( has_many ) association .
def process(method_detail, value)
        begin
        ProductLoader.log_g "Process =" +  method_detail.name.to_s
        raise ProductLoadError.new("Cannot process {value} NO details found to assign to") unless(method_detail)
        current_value, current_attribute_hash = @populator.prepare_data(method_detail, value)
        current_method_detail = method_detail
        logger.info "Processing value: [{current_value}]"
        if(current_value && (current_method_detail.operator?('sku')))
          @sku_to_remember= get_each_assoc.first
          ProductLoader.log_g "SKU=" +  @sku_to_remember.to_s
        end
        if(current_value && (current_method_detail.operator?('slug')))
          spree=  Spree::Variant.joins(:product).where("spree_variants.product_id = spree_products.id and spree_variants.is_master = ? and spree_variants.sku = ? and spree_products.slug= ? ",'True',@sku_to_remember,get_each_assoc.first)
         if spree.count > 0
           ProductLoader.log_g "ID = "  + spree.first.attributes["product_id"].to_s
           object_to_update = Spree::Product.find(spree.first.attributes["product_id"])
           object_to_update.name = @load_object.name
           object_to_update.description = @load_object.description
           object_to_update.master.sku =@sku_to_remember
           object_to_update.shipping_category = Spree::ShippingCategory.find_by_name!("Default")
           @load_object = object_to_update
         end
        end
        if(current_value && (current_method_detail.operator?('stock')))
            add_items_on_hand
          return
        end
        if(current_value && (current_method_detail.name =='price_ru' || current_method_detail.name =='price_eu' ) )
          add_additional_prices current_method_detail
           return
        end
        if(current_value && (current_method_detail.name =='translation'))
          add_translation
          return
        end
        if(current_value && (current_method_detail.name =='taxons_ru'))
          return
        end
        if(current_value && (current_method_detail.operator?('variants') || current_method_detail.operator?('option_types')) )
          add_options_variants
        elsif(current_method_detail.operator?('taxons') && current_value)
          add_taxons
        elsif(current_method_detail.operator?('product_properties') && current_value)
          add_properties
        elsif(current_method_detail.operator?('images') && current_value)
          add_images( (SpreeHelper::version.to_f > 1) ? @load_object.master : @load_object )
        elsif(current_method_detail.operator?('variant_price') && current_value)
          puts "Newly Loaded Object Size {@load_object.variants.size}"
          if(@load_object.variants.size > 0)
            if(current_value.to_s.include?(Delimiters::multi_assoc_delim))
              values = current_value.to_s.split(Delimiters::multi_assoc_delim)
              if(@load_object.variants.size == values.size)
                @load_object.variants.each_with_index do |v, i|
                  v.price = values[i].to_f
                  v.save
                end
              else
                puts "WARNING: Price entries did not match number of Variants - None Set"
              end
            end
          else
            super
          end
        elsif(current_method_detail.operator?('variant_sku') && current_value)
          if(@load_object.variants.size > 0)
            if(current_value.to_s.include?(Delimiters::multi_assoc_delim))
              values = current_value.to_s.split(Delimiters::multi_assoc_delim)
              if(@load_object.variants.size == values.size)
                @load_object.variants.each_with_index {|v, i| v.sku = values[i].to_s }
                @load_object.save
              else
                puts "WARNING: SKU entries did not match number of Variants - None Set"
              end
            end
          else
            super
          end
        elsif(current_value && (current_method_detail.operator?('count_on_hand') || current_method_detail.operator?('on_hand')) )
          logger.warn("NO STOCK SET - count_on_hand BROKEN - needs updating for new StockManagement in Spree >= 2.2")
          save_if_new
           Spree has some stock management stuff going on, so dont usually assign to column vut use
           on_hand and on_hand=
          if(@load_object.variants.size > 0)
            if(current_value.to_s.include?(Delimiters::multi_assoc_delim))
              puts "DEBUG: COUNT_ON_HAND PER VARIANT",current_value.is_a?(String),
               Check if we processed Option Types and assign count per option
              values = current_value.to_s.split(Delimiters::multi_assoc_delim)
              if(@load_object.variants.size == values.size)
                @load_object.variants.each_with_index {|v, i| v.on_hand = values[i].to_i }
                @load_object.save
              else
                puts "WARNING: {values.size} count on hand entries {current_value} did not match {@load_object.variants.size} variants - None Set"
              end
            end
             Can only set count on hand on Product if no Variants exist, else model throws
          elsif(@load_object.variants.size == 0)
            if(current_value.to_s.include?(Delimiters::multi_assoc_delim))
              puts "WARNING: Multiple count_on_hand values specified but no Variants/OptionTypes created"
              load_object.on_hand = current_value.to_s.split(Delimiters::multi_assoc_delim).first.to_i
            else
              load_object.on_hand = current_value.to_i
            end
          end
        else
          super
        end
        rescue Exception => ex
          ProductLoader.log_g "Message ={ex.message} backtrace = {ex.backtrace} "
          throw ex
      end
      end
def process ( method_detail , value ) begin ProductLoader . log_g "Process =" + method_detail . name . to_s raise ProductLoadError . new ( "Cannot process #{value} NO details found to assign to" ) unless ( method_detail ) current_value , current_attribute_hash = @populator . prepare_data ( method_detail , value ) current_method_detail = method_detail logger . info "Processing value: [#{current_value}]" if ( current_value && ( current_method_detail . operator? ( 'sku' ) ) ) @sku_to_remember = get_each_assoc . first ProductLoader . log_g "SKU=" + @sku_to_remember . to_s end if ( current_value && ( current_method_detail . operator? ( 'slug' ) ) ) spree = Spree :: Variant . joins ( :product ) . where ( "spree_variants.product_id = spree_products.id and spree_variants.is_master = ? and spree_variants.sku = ? and spree_products.slug= ? " , 'True' , @sku_to_remember , get_each_assoc . first ) if spree . count > 0 ProductLoader . log_g "ID = " + spree . first . attributes [ "product_id" ] . to_s object_to_update = Spree :: Product . find ( spree . first . attributes [ "product_id" ] ) object_to_update . name = @load_object . name object_to_update . description = @load_object . description object_to_update . master . sku = @sku_to_remember object_to_update . shipping_category = Spree :: ShippingCategory . find_by_name! ( "Default" ) @load_object = object_to_update end end if ( current_value && ( current_method_detail . operator? ( 'stock' ) ) ) add_items_on_hand return end if ( current_value && ( current_method_detail . name == 'price_ru' || current_method_detail . name == 'price_eu' ) ) add_additional_prices current_method_detail return end if ( current_value && ( current_method_detail . name == 'translation' ) ) add_translation return end if ( current_value && ( current_method_detail . name == 'taxons_ru' ) ) return end if ( current_value && ( current_method_detail . operator? ( 'variants' ) || current_method_detail . operator? ( 'option_types' ) ) ) add_options_variants elsif ( current_method_detail . operator? ( 'taxons' ) && current_value ) add_taxons elsif ( current_method_detail . operator? ( 'product_properties' ) && current_value ) add_properties elsif ( current_method_detail . operator? ( 'images' ) && current_value ) add_images ( ( SpreeHelper :: version . to_f > 1 ) ? @load_object . master : @load_object ) elsif ( current_method_detail . operator? ( 'variant_price' ) && current_value ) puts "Newly Loaded Object Size #{@load_object.variants.size}" if ( @load_object . variants . size > 0 ) if ( current_value . to_s . include? ( Delimiters :: multi_assoc_delim ) ) values = current_value . to_s . split ( Delimiters :: multi_assoc_delim ) if ( @load_object . variants . size == values . size ) @load_object . variants . each_with_index do | v , i | v . price = values [ i ] . to_f v . save end else puts "WARNING: Price entries did not match number of Variants - None Set" end end else super end elsif ( current_method_detail . operator? ( 'variant_sku' ) && current_value ) if ( @load_object . variants . size > 0 ) if ( current_value . to_s . include? ( Delimiters :: multi_assoc_delim ) ) values = current_value . to_s . split ( Delimiters :: multi_assoc_delim ) if ( @load_object . variants . size == values . size ) @load_object . variants . each_with_index { | v , i | v . sku = values [ i ] . to_s } @load_object . save else puts "WARNING: SKU entries did not match number of Variants - None Set" end end else super end elsif ( current_value && ( current_method_detail . operator? ( 'count_on_hand' ) || current_method_detail . operator? ( 'on_hand' ) ) ) logger . warn ( "NO STOCK SET - count_on_hand BROKEN - needs updating for new StockManagement in Spree >= 2.2" ) save_if_new if ( @load_object . variants . size > 0 ) if ( current_value . to_s . include? ( Delimiters :: multi_assoc_delim ) ) values = current_value . to_s . split ( Delimiters :: multi_assoc_delim ) if ( @load_object . variants . size == values . size ) @load_object . variants . each_with_index { | v , i | v . on_hand = values [ i ] . to_i } @load_object . save else puts "WARNING: #{values.size} count on hand entries #{current_value} did not match #{@load_object.variants.size} variants - None Set" end end elsif ( @load_object . variants . size == 0 ) if ( current_value . to_s . include? ( Delimiters :: multi_assoc_delim ) ) puts "WARNING: Multiple count_on_hand values specified but no Variants/OptionTypes created" load_object . on_hand = current_value . to_s . split ( Delimiters :: multi_assoc_delim ) . first . to_i else load_object . on_hand = current_value . to_i end end else super end rescue Exception => ex ProductLoader . log_g "Message =#{ex.message} backtrace = #{ex.backtrace} " throw ex end end
Over ride base class process with some Spree::Product specifics
What process a value string from a column, assigning value(s) to correct association on Product.
Over ride base class process with some Spree :: Product specifics What process a value string from a column assigning value ( s ) to correct association on Product .
#Post.includes(:votes) #Employee. #joins(:company => :addresses). #   where(:addresses => { :city => 'Porto Alegre' }) #object_to_update =  @load_object_class.joins(:variants).find_by(:slug=> get_each_assoc.first,:variants => {:sku =>@load_object_class.master.sku }) #ProductLoader.log_g #HAI # extract_ru_taxons # Special cases for Products, generally where a simple one stage lookup won't suffice # otherwise simply use default processing from base class # Check if we processed Option Types and assign  per option # Check if we processed Option Types and assign  per option # CURRENTLY BROKEN FOR Spree 2.2 - New Stock management : # http://guides.spreecommerce.com/developer/inventory.html # return # Unless we can save here, in danger of count_on_hand getting wiped out. # If we set (on_hand or count_on_hand) on an unsaved object, during next subsequent save # looks like some validation code or something calls Variant.on_hand= with 0 # If we save first, then our values seem to stick # TODO smart column ordering to ensure always valid - if we always make it very last column might not get wiped ? # # Spree has some stock management stuff going on, so dont usually assign to column vut use # on_hand and on_hand= #puts "DEBUG: COUNT_ON_HAND PER VARIANT",current_value.is_a?(String), # Check if we processed Option Types and assign count per option # Can only set count on hand on Product if no Variants exist, else model throws
method_detail
value