Module: Attacheable

Includes:
FileNaming, Uploading
Defined in:
lib/attacheable/uploading.rb,
lib/attacheable/file_naming.rb,
lib/attacheable/photo_handler.rb,
lib/attacheable.rb

Defined Under Namespace

Modules: ClassMethods, FileNaming, Uploading Classes: PhotoHandler

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FileNaming

#attachment_basename, #attachment_extname, #base_path, #full_filename_with_creation, #full_filename_without_creation, #public_filename_without_creation, #sanitize_filename, #thumbnail_name_for

Methods included from Uploading

#accepts_file_type_for_upload?, #handle_uploaded_file, #identify_image_properties, #identify_uploaded_file_type, #prepare_merb_uploaded_file, #prepare_uploaded_file, #save_to_replicas, #save_to_storage

Class Method Details

.included(base) ⇒ Object

:nodoc:



65
66
67
68
69
70
# File 'lib/attacheable.rb', line 65

def self.included(base) #:nodoc:
  base.before_update :rename_file
  base.after_save :save_to_storage
  base.after_destroy :remove_files
  base.extend(ClassMethods)
end

.rootObject



73
74
75
76
77
# File 'lib/attacheable.rb', line 73

def self.root
  return RAILS_ROOT if defined?(RAILS_ROOT)
  return Merb.root if defined?(Merb)
  return File.dirname(__FILE__)+"/../.."
end

Instance Method Details

#attachment_optionsObject

:nodoc:



116
117
118
# File 'lib/attacheable.rb', line 116

def attachment_options #:nodoc:
  self.class.attachment_options
end

#destroy_thumbnails!(thumbnail = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/attacheable.rb', line 80

def destroy_thumbnails!(thumbnail = nil)
  return if filename.blank?
  if thumbnail
    FileUtils.rm_f(full_filename_without_creation(thumbnail))
  else
    to_remove = Dir["#{File.dirname(full_filename_without_creation)}/*"] - [full_filename_without_creation]
    FileUtils.rm_f(to_remove)
  end
end

#filename=(value) ⇒ Object



203
204
205
206
# File 'lib/attacheable.rb', line 203

def filename=(value)
  @old_filename = full_filename unless filename.nil? || @old_filename
  write_attribute :filename, sanitize_filename(value)
end

#full_filename(thumbnail = nil) ⇒ Object

Returns real path to original file if thumbnail is nil or path with thumbnail part inserted If options is set to true, this method will autogenerate thumbnail



124
125
126
127
# File 'lib/attacheable.rb', line 124

def full_filename(thumbnail = nil)
  return "" if filename.blank?
  attachment_options[:autocreate] ? full_filename_with_creation(thumbnail) : full_filename_without_creation(thumbnail)
end

#full_filename_by_path(path) ⇒ Object

:nodoc:



129
130
131
132
133
134
135
# File 'lib/attacheable.rb', line 129

def full_filename_by_path(path) #:nodoc:
  return if filename.blank?
  thumbnail = path.gsub(%r((^#{Regexp.escape(attachment_basename)}_)(\w+)(#{Regexp.escape(attachment_extname)})$), '\2')
  return unless thumbnail
  return unless attachment_options[:thumbnails][thumbnail.to_sym]
  full_filename_with_creation(thumbnail.to_sym)
end

#image_height(thumbnail = nil) ⇒ Object



149
150
151
# File 'lib/attacheable.rb', line 149

def image_height(thumbnail = nil)
  `identify -format "%w" "#{full_filename(thumbnail)}"`.to_i
end

#image_sizeObject



199
200
201
# File 'lib/attacheable.rb', line 199

def image_size
  [width.to_s, height.to_s] * 'x'
end

#image_width(thumbnail = nil) ⇒ Object



145
146
147
# File 'lib/attacheable.rb', line 145

def image_width(thumbnail = nil)
  `identify -format "%w" "#{full_filename(thumbnail)}"`.to_i
end

#public_filename(thumbnail = nil) ⇒ Object

Gets the public path to the file, visible to browser The optional thumbnail argument will output the thumbnail’s filename. If options is set to true, this method will autogenerate thumbnail



140
141
142
143
# File 'lib/attacheable.rb', line 140

def public_filename(thumbnail = nil)
  return "" if filename.blank?
  full_filename(thumbnail).gsub %r(^#{Regexp.escape(base_path)}), ''
end

#uploaded_data=(file_data) ⇒ Object

Main method, that accepts uploaded data



191
192
193
194
195
196
197
# File 'lib/attacheable.rb', line 191

def uploaded_data=(file_data)
  prepare_uploaded_file(file_data)
  file_type = identify_uploaded_file_type
  if accepts_file_type_for_upload?(file_type)
    handle_uploaded_file
  end
end

#valid_filetype?Boolean

:nodoc:

Returns:

  • (Boolean)


185
186
187
# File 'lib/attacheable.rb', line 185

def valid_filetype? #:nodoc:
  errors.add("uploaded_data", attachment_options[:validation_message]) if @save_new_attachment && !@valid_filetype
end