Class: DataShift::SpreeHelper::ImageLoader

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

Overview

Very specific Image Loading for existing Products in Spree.

Requirements : A CSV or Excel file which has 2+ columns

1)  Identifies a Product via Name or SKU column
2+) The full path(s) to the Images to attach to Product from column 1

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DataShift::SpreeBaseLoader

#add_images

Constructor Details

#initialize(image = nil, options = {}) ⇒ ImageLoader

Returns a new instance of ImageLoader.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/loaders/spree/image_loader.rb', line 21

def initialize(image = nil, options = {})
  
  super( DataShift::SpreeHelper::get_spree_class('Image'), true, image, options )
   
  unless(MethodDictionary.for?(@@product_klass))
    MethodDictionary.find_operators( @@product_klass )
    MethodDictionary.build_method_details( @@product_klass )
  end
  
  unless(MethodDictionary.for?(@@variant_klass))
    MethodDictionary.find_operators( @@variant_klass )
    MethodDictionary.build_method_details( @@variant_klass )
  end
    
  puts "Attachment Class is #{SpreeHelper::product_attachment_klazz}" if(@verbose)
end

Class Method Details

.acceptable_path_headersObject



54
55
56
# File 'lib/loaders/spree/image_loader.rb', line 54

def self.acceptable_path_headers
  @@path_headers ||= ['attachment', 'images', 'path']
end

Instance Method Details

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



45
46
47
48
49
50
51
52
# File 'lib/loaders/spree/image_loader.rb', line 45

def perform_load( file_name, opts = {} )
  options = opts.dup
  
  # force inclusion means add headers to operator list even not present on Image
  options[:include_all] = true

  super(file_name, options)
end

#processObject

Called from associated perform_xxxx_load



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
# File 'lib/loaders/spree/image_loader.rb', line 60

def process()
  
  # TODO - current relies on correct order - i.e lookup column must come before attachment
  
  @@path_headers ||= ['attachment', 'images', 'path']
     
  operator = @current_method_detail.operator
  
  if(current_value && ImageLoader::acceptable_path_headers.include?(operator) )
   
    add_images( @load_object ) if(@load_object)
    
  elsif(current_value && @current_method_detail.operator )    
    
    # find the db record to assign our Image usually expect either SKU (Variant) or Name (product)
    if( MethodDictionary::find_method_detail_if_column(@@product_klass, operator) )
      @load_object = get_record_by(@@product_klass, operator, current_value)
      
    elsif( MethodDictionary::find_method_detail_if_column(@@variant_klass, operator) )
      puts "Find VARIANT with  #{operator} == #{current_value}"
      @load_object = get_record_by(@@variant_klass, operator, current_value)
    else
      raise "No Spree class can be searched for by #{operator}"
    end  
    
    unless(@load_object)
      puts "WARNING: Could not find a record where #{operator} == #{current_value}"
      return
    else
      puts "Image Attachment on record #{@load_object.inspect}"
    end
       
  end
  
end

#reset(object = nil) ⇒ Object

Load object not an Image - need to look it up via Name or SKU



39
40
41
42
43
# File 'lib/loaders/spree/image_loader.rb', line 39

def reset( object = nil)
  super(object)
  
  @load_object = nil
end