Class: ARLoader::ImageLoader

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

Instance Attribute Summary

Attributes inherited from LoaderBase

#current_method_detail, #current_value, #failed_objects, #headers, #load_object, #load_object_class, #loaded_objects, #options

Instance Method Summary collapse

Methods inherited from LoaderBase

#abort_on_failure?, #contains_mandatory?, #failed_count, #find_and_process, #find_or_new, #load, #loaded_count, #missing_mandatory, #new_load_object, #reset, #save, set_multi_assoc_delim, set_multi_value_delim, set_name_value_delim

Constructor Details

#initialize(image = nil) ⇒ ImageLoader

Returns a new instance of ImageLoader.



12
13
14
15
# File 'lib/loaders/spree/image_loader.rb', line 12

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

Instance Method Details

#process(image_path, record = nil) ⇒ Object

Note the Spree Image model sets default storage path to

> :path => “:rails_root/public/assets/products/:id/:style/:basename.:extension”



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/loaders/spree/image_loader.rb', line 20

def process( image_path, record = nil)

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

  alt = (record and record.respond_to? :name) ? record.name : ""

  @load_object.alt = alt

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

  @load_object.attachment.reprocess!
  @load_object.viewable = record if record

  puts @load_object.save ? "Success: Uploaded Image: #{@load_object.inspect}" : "ERROR : Problem saving to DB Image: #{@load_object}"
end