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:



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

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



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

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:



112
113
114
# File 'lib/attacheable.rb', line 112

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

#filename=(value) ⇒ Object



190
191
192
193
# File 'lib/attacheable.rb', line 190

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



120
121
122
123
# File 'lib/attacheable.rb', line 120

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:



125
126
127
128
129
130
131
# File 'lib/attacheable.rb', line 125

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_sizeObject



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

def image_size
  [width.to_s, height.to_s] * 'x'
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



136
137
138
139
# File 'lib/attacheable.rb', line 136

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



178
179
180
181
182
183
184
# File 'lib/attacheable.rb', line 178

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)


172
173
174
# File 'lib/attacheable.rb', line 172

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