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
-
#attachment ⇒ Object
Returns the value of attribute attachment.
Class Method Summary collapse
-
.get_files(path, options = {}) ⇒ Object
Get all files (based on file extensions) from supplied path.
Instance Method Summary collapse
-
#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”.
- #get_file(attachment_path) ⇒ Object
- #process_missing_records(missing_records) ⇒ Object
Methods included from Logging
#logdir, #logdir=, #logger, #verbose
Instance Attribute Details
#attachment ⇒ Object
Returns the value of attribute attachment.
19 20 21 |
# File 'lib/loaders/paperclip/datashift_paperclip.rb', line 19 def @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, = {}) return [path] if File.file?(path) glob = [:glob] ? [:glob] : '*.*' glob = (['recursive'] || [: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 (klass, , = {}) logger.info("Paperclip::create_paperclip_attachment on Class #{klass}") has_attached_file_attribute = [:has_attached_file_name] ? [:has_attached_file_name].to_sym : :attachment = get_file() paperclip_attributes = { "#{has_attached_file_attribute}": } paperclip_attributes.merge!([:attributes]) if [:attributes] begin @attachment = klass.new(paperclip_attributes) rescue => e logger.error( e.backtrace) raise CreateAttachmentFailed.new("Failed [#{e.}] creating PaperClip Attachment on #{klass} for [#{}]") ensure .close unless .closed? end if @attachment.save logger.info("Success: Created Attachment #{@attachment.id} : #{@attachment.}") @attachment else logger.error('Problem creating and saving Paperclip Attachment') logger.error(@attachment.errors..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( ) unless File.exist?() && File.readable?() logger.error("Cannot process Image from #{Dir.pwd}: Invalid Path #{}") raise PathError.new("Cannot process Image : Invalid Path #{}") end file = begin File.new(, 'rb') rescue => e logger.error(e.inspect) raise PathError.new("ERROR : Failed to read image from #{}") 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 |