lib/loaders/spree/image_loader.rb in datashift_spree-0.2.0 vs lib/loaders/spree/image_loader.rb in datashift_spree-0.2.1

- old
+ new

@@ -7,45 +7,103 @@ #require 'paperclip/attachment_loader' module DataShift module SpreeHelper - - # TODO - extract this out of SpreeHelper to create a general paperclip loader + + # Version Helper - find the right class to attach Product images to + + def self.product_attachment_klazz + @product_attachment_klazz ||= if(DataShift::SpreeHelper::version.to_f > 1.0 ) + DataShift::SpreeHelper::get_spree_class('Variant' ) + else + DataShift::SpreeHelper::get_spree_class('Product' ) + end + end + + # 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 ImageLoader < SpreeBaseLoader - def initialize(image = nil, options = {}) + def initialize(options = {}) - opts = options.merge(:load => false) # Don't need operators and no table Spree::Image - - super( DataShift::SpreeHelper::get_spree_class('Image'), image, opts ) + super( DataShift::SpreeHelper::get_spree_class('Image'), nil, options ) + + unless(MethodDictionary.for?(@@product_klass)) + MethodDictionary.find_operators( @@product_klass ) + MethodDictionary.build_method_details( @@product_klass ) + end - puts "Attachment Class is #{ImageLoader::attachment_klazz}" if(@verbose) + 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 - def self.attachment_klazz - @attachment_klazz ||= if(DataShift::SpreeHelper::version.to_f > 1.0 ) - DataShift::SpreeHelper::get_spree_class('Variant' ) - else - DataShift::SpreeHelper::get_spree_class('Product' ) - end - @attachment_klazz + # Load object not an Image - need to look it up via Name or SKU + def reset( object = nil) + super(object) + + @load_object = nil end - def process() + 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 - if(current_value && @current_method_detail.operator ) - - # find the db record to assign our Image to - @load_object = get_record_by(@@product_klass, @current_method_detail.operator, current_value) - - @load_object = (SpreeHelper::version.to_f > 1) ? @load_object.master : @load_object + super(file_name, options) + end + + def self.acceptable_path_headers + @@path_headers ||= ['attachment', 'images', 'path'] + end + + # Called from associated perform_xxxx_load + + def process() - elsif(current_value && @current_method_detail.operator?('attachment') ) + # 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 && acceptable_path_headers.include?(operator) ) - add_images( @load_object ) + 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 - end - end + + end + end end \ No newline at end of file