Module: AttachmentMagic::InstanceMethods

Defined in:
lib/attachment_magic.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



147
148
149
# File 'lib/attachment_magic.rb', line 147

def self.included(base)
  base.define_callbacks *[:after_attachment_saved] if base.respond_to?(:define_callbacks)
end

Instance Method Details

#content_type=(new_type) ⇒ Object

Sets the content type.



152
153
154
# File 'lib/attachment_magic.rb', line 152

def content_type=(new_type)
  write_attribute :content_type, new_type.to_s.strip
end

#copy_to_temp_file(file) ⇒ Object

Copies the given file to a randomly named Tempfile.



234
235
236
# File 'lib/attachment_magic.rb', line 234

def copy_to_temp_file(file)
  self.class.copy_to_temp_file file, random_tempfile_filename
end

#create_temp_fileObject

Stub for creating a temp file from the attachment data. This should be defined in the backend module.



244
# File 'lib/attachment_magic.rb', line 244

def create_temp_file() end

#detect_mimetype(file_data) ⇒ Object

Detects the mime-type if content_type is ‘application/octet-stream’



157
158
159
160
161
162
163
# File 'lib/attachment_magic.rb', line 157

def detect_mimetype(file_data)
  if file_data.content_type.strip == "application/octet-stream"
    return File.mime_type?(file_data.original_filename)
  else
    return file_data.content_type
  end
end

#filename=(new_name) ⇒ Object

Sanitizes a filename.



166
167
168
# File 'lib/attachment_magic.rb', line 166

def filename=(new_name)
  write_attribute :filename, sanitize_filename(new_name)
end

#save_attachment?Boolean

Returns true if the attachment data will be written to the storage system on the next save

Returns:

  • (Boolean)


171
172
173
# File 'lib/attachment_magic.rb', line 171

def save_attachment?
  File.file?(temp_path.class == String ? temp_path : temp_path.to_filename)
end

#set_temp_data(data) ⇒ Object

Writes the given data to a Tempfile and adds it to the collection of temp files.



229
230
231
# File 'lib/attachment_magic.rb', line 229

def set_temp_data(data)
  temp_paths.unshift write_to_temp_file data unless data.nil?
end

#temp_dataObject

Gets the data from the latest temp file. This will read the file into memory.



224
225
226
# File 'lib/attachment_magic.rb', line 224

def temp_data
  save_attachment? ? File.read(temp_path) : nil
end

#temp_pathObject

Gets the latest temp path from the collection of temp paths. While working with an attachment, multiple Tempfile objects may be created for various processing purposes (resizing, for example). An array of all the tempfile objects is stored so that the Tempfile instance is held on to until it’s not needed anymore. The collection is cleared after saving the attachment.



212
213
214
215
# File 'lib/attachment_magic.rb', line 212

def temp_path
  p = temp_paths.first
  p.respond_to?(:path) ? p.path : p.to_s
end

#temp_pathsObject

Gets an array of the currently used temp paths. Defaults to a copy of #full_filename.



218
219
220
221
# File 'lib/attachment_magic.rb', line 218

def temp_paths
  @temp_paths ||= (new_record? || !respond_to?(:full_filename) || !File.exist?(full_filename) ?
    [] : [copy_to_temp_file(full_filename)])
end

#uploaded_dataObject

nil placeholder in case this field is used in a form.



176
# File 'lib/attachment_magic.rb', line 176

def uploaded_data() nil; end

#uploaded_data=(file_data) ⇒ Object

This method handles the uploaded file object. If you set the field name to uploaded_data, you don’t need any special code in your controller.

<% form_for :attachment, :html => { :multipart => true } do |f| -%>
  <p><%= f.file_field :uploaded_data %></p>
  <p><%= submit_tag :Save %>
<% end -%>

@attachment = Attachment.create! params[:attachment]

TODO: Allow it to work with Merb tempfiles too.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/attachment_magic.rb', line 189

def uploaded_data=(file_data)
  if file_data.respond_to?(:content_type)
    return nil if file_data.size == 0
    self.content_type = detect_mimetype(file_data)
    self.filename     = file_data.original_filename if respond_to?(:filename)
  else
    return nil if file_data.blank? || file_data['size'] == 0
    self.content_type = file_data['content_type']
    self.filename =  file_data['filename']
    file_data = file_data['tempfile']
  end
  if file_data.is_a?(StringIO)
    file_data.rewind
    set_temp_data file_data.read
  else
    self.temp_paths.unshift file_data.tempfile.path
  end
end

#write_to_temp_file(data) ⇒ Object

Writes the given file to a randomly named Tempfile.



239
240
241
# File 'lib/attachment_magic.rb', line 239

def write_to_temp_file(data)
  self.class.write_to_temp_file data, random_tempfile_filename
end