Module: DataShift::Paperclip

Includes:
Logging
Included in:
ImageLoading, AttachmentLoader
Defined in:
lib/loaders/paperclip/attachment_loader.rb,
lib/loaders/paperclip/datashift_paperclip.rb

Defined Under Namespace

Classes: AttachmentLoader

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logdir, #logdir=, #logger, #verbose

Instance Attribute Details

#attachmentObject

Returns the value of attribute attachment.



19
20
21
# File 'lib/loaders/paperclip/datashift_paperclip.rb', line 19

def attachment
  @attachment
end

Class Method Details

.get_files(path, options = {}) ⇒ Object

Get all files (based on file extensions) from supplied path. Options :

:glob : The glob to use to find files

> :recursive : Descend tree looking for files rather than just supplied path



26
27
28
29
30
31
32
# File 'lib/loaders/paperclip/datashift_paperclip.rb', line 26

def self.get_files(path, options = {})
  return [path] if File.file?(path)
  glob = options[:glob] ? options[:glob] : '*.*'
  glob = (options['recursive'] || options[:recursive]) ? "**/#{glob}" : glob

  Dir.glob("#{path}/#{glob}", File::FNM_CASEFOLD)
end

Instance Method Details

#create_paperclip_attachment(klass, attachment_path, options = {}) ⇒ Object

Note the paperclip attachment model defines the storage path via something like :

> :path => “:rails_root/public/blah/blahs/:id/:style/:basename.:extension”

Options

:attributes

  Pass through a hash of attributes to the Paperclip klass's initializer

:has_attached_file_name

  Paperclip attachment name defined with macro 'has_attached_file :name'

  This is usually called/defaults  :attachment

  e.g
    When : has_attached_file :avatar

    Give : {:has_attached_file_attribute => :avatar}

    When :  has_attached_file :icon

    Give : { :has_attached_file_attribute => :icon }


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/loaders/paperclip/datashift_paperclip.rb', line 75

def create_paperclip_attachment(klass, attachment_path, options = {})

  logger.info("Paperclip::create_paperclip_attachment on Class #{klass}")

  has_attached_file_attribute = options[:has_attached_file_name] ? options[:has_attached_file_name].to_sym : :attachment

  attachment_file = get_file(attachment_path)

  paperclip_attributes = { "#{has_attached_file_attribute}": attachment_file}

  paperclip_attributes.merge!(options[:attributes]) if options[:attributes]

  begin
    @attachment = klass.new(paperclip_attributes)
  rescue => e
    logger.error( e.backtrace)
    raise CreateAttachmentFailed.new("Failed [#{e.message}] creating PaperClip Attachment on #{klass} for [#{attachment_path}]")
  ensure
    attachment_file.close unless attachment_file.closed?
  end

  if @attachment.save
    logger.info("Success: Created Attachment #{@attachment.id} : #{@attachment.attachment_file_name}")

    @attachment
  else
    logger.error('Problem creating and saving Paperclip Attachment')
    logger.error(@attachment.errors.messages.inspect)
    raise CreateAttachmentFailed.new('PaperClip error - Problem saving Attachment')
  end
end

#get_file(attachment_path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/loaders/paperclip/datashift_paperclip.rb', line 34

def get_file( attachment_path )

  unless File.exist?(attachment_path) && File.readable?(attachment_path)
    logger.error("Cannot process Image from #{Dir.pwd}: Invalid Path #{attachment_path}")
    raise PathError.new("Cannot process Image : Invalid Path #{attachment_path}")
  end

  file = begin
    File.new(attachment_path, 'rb')
  rescue => e
    logger.error(e.inspect)
    raise PathError.new("ERROR : Failed to read image from #{attachment_path}")
  end

  file
end

#process_missing_records(missing_records) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/loaders/paperclip/attachment_loader.rb', line 187

def process_missing_records( missing_records )
  unless missing_records.empty?
    FileUtils.mkdir_p('MissingAttachmentRecords') unless File.directory?('MissingAttachmentRecords')

    puts "WARNING : #{missing_records.size} of #{loading_files_cache.size} files could not be attached to a #{load_object_class}"
    puts "For your convenience copying files with MISSING #{attach_to_klass} to : MissingAttachmentRecords"
    missing_records.each do |i|
      logger.info("Copying #{i} to MissingAttachmentRecords folder")
      FileUtils.cp( i, 'MissingAttachmentRecords') unless(configuration.dummy_run)
    end
  end
end