Class: DataShift::SpreeHelper::ProductLoader

Inherits:
DataShift::SpreeBaseLoader show all
Defined in:
lib/loaders/spree/product_loader.rb

Instance Method Summary collapse

Methods inherited from DataShift::SpreeBaseLoader

#add_images

Constructor Details

#initialize(product = nil, options = {}) ⇒ ProductLoader

Options

:reload           : Force load of the method dictionary for object_class even if already loaded
:verbose          : Verboise logging and to STDOUT


22
23
24
25
26
27
28
29
30
31
# File 'lib/loaders/spree/product_loader.rb', line 22

def initialize(product = nil, options = {})

  opts = {:instance_methods => true}.merge( options )

  # depending on version get_product_class should return us right class, namespaced or not

  super( DataShift::SpreeHelper::get_product_class(), true, product, opts)

  raise "Failed to create Product for loading" unless @load_object
end

Instance Method Details

#perform_load(file_name, opts = {}) ⇒ Object

Options:

[:dummy]           : Perform a dummy run - attempt to load everything but then roll back


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/loaders/spree/product_loader.rb', line 36

def perform_load( file_name, opts = {} )
  options = opts.dup

  #puts "Product Loader -  Load Options", options.inspect

  # In >= 1.1.0 Image moved to master Variant from Product so no association called Images on Product anymore
  if(DataShift::SpreeHelper::version.to_f > 1 )
    options[:force_inclusion] = options[:force_inclusion] ? ([ *options[:force_inclusion]] + 'images') : ['images']
  end

  super(file_name, options)
end

#processObject

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.



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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/loaders/spree/product_loader.rb', line 55

def process()

  # 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

  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_value && (@current_method_detail.operator?('count_on_hand') || @current_method_detail.operator?('on_hand')) )


    # 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: Count on hand entries did not match number of 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
end