Sha256: 279182d2fd5a2250d324b997facb8a9bb646a77fb218697b819ef6178d18b348

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

# Copyright:: (c) Autotelik Media Ltd 2011
# Author ::   Tom Statter
# Date ::     Jan 2011
# License::   MIT. Free, Open Source.
#
require 'loader_base'

module DataShift

   module ImageLoading
 
    # Note the Spree Image model sets default storage path to
    # => :path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"

    def create_image(image_path, viewable_record = nil, options = {})

      image = Image.new
      
      unless File.exists?(image_path)
        puts "ERROR : Invalid Path"
        return image
      end

      alt = if(options[:alt])
        options[:alt]
      else
        (viewable_record and viewable_record.respond_to? :name) ? viewable_record.name : ""
      end
      
      image.alt = alt

      begin
        image.attachment = File.new(image_path, "r")
      rescue => e
        puts e.inspect
        puts "ERROR : Failed to read image #{image_path}"
        return image
      end

      image.attachment.reprocess!
      image.viewable =  viewable_record if viewable_record

      puts image.save ? "Success: Created Image: #{image.inspect}" : "ERROR : Problem saving to DB Image: #{image.inspect}"
    end
  end
  
  class ImageLoader < LoaderBase

    include DataShift::ImageLoading
        
    def initialize(image = nil)
      super( Image, image )
      raise "Failed to create Image for loading" unless @load_object
    end

    # Note the Spree Image model sets default storage path to
    # => :path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"

    def process( image_path, record = nil)
      @load_object = create_image(path, record)
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datashift-0.2.1 lib/loaders/spree/image_loader.rb