lib/loaders/spree/image_loader.rb in datashift_spree-0.1.0 vs lib/loaders/spree/image_loader.rb in datashift_spree-0.2.0
- old
+ new
@@ -6,69 +6,46 @@
require 'loader_base'
#require 'paperclip/attachment_loader'
module DataShift
-
module SpreeHelper
# TODO - extract this out of SpreeHelper to create a general paperclip loader
- class ImageLoader < LoaderBase
-
-# include DataShift::SpreeImageLoading
- include DataShift::CsvLoading
- include DataShift::ExcelLoading
-
+ class ImageLoader < SpreeBaseLoader
+
def initialize(image = nil, options = {})
opts = options.merge(:load => false) # Don't need operators and no table Spree::Image
super( DataShift::SpreeHelper::get_spree_class('Image'), image, opts )
- if(DataShift::SpreeHelper::version.to_f > 1.0 )
- @attachment_klazz = DataShift::SpreeHelper::get_spree_class('Variant' )
+ puts "Attachment Class is #{ImageLoader::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
- @attachment_klazz = DataShift::SpreeHelper::get_spree_class('Product' )
+ DataShift::SpreeHelper::get_spree_class('Product' )
end
-
- puts "Attachment Class is #{@attachment_klazz}" if(@verbose)
-
- raise "Failed to create Image for loading" unless @load_object
+ @attachment_klazz
end
def process()
- if(current_value && @current_method_detail.operator?('attachment') )
+ if(current_value && @current_method_detail.operator )
- # assign the image file data as an attachment
- @load_object.attachment = get_file(current_value)
-
- puts "Image attachment created : #{@load_object.inspect}"
-
- elsif(current_value && @current_method_detail.operator )
-
# find the db record to assign our Image to
- add_record( get_record_by(@attachment_klazz, @current_method_detail.operator, current_value) )
-
+ @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
+
+ elsif(current_value && @current_method_detail.operator?('attachment') )
+
+ add_images( @load_object )
end
-
+
end
-
- def add_record(record)
- if(record)
- if(SpreeHelper::version.to_f > 1 )
- @load_object.viewable = record
- else
- @load_object.viewable = record.product # SKU stored on Variant but we want it's master Product
- end
- @load_object.save
- puts "Image viewable set : #{record.inspect}"
-
- else
- puts "WARNING - Cannot set viewable - No matching record supplied"
- logger.error"Failed to find a matching record"
- end
- end
- end
-
+ end
end
end
\ No newline at end of file