Module: Milton::Attachment::InstanceMethods

Defined in:
lib/milton/attachment.rb

Overview

These get mixed in to your model when you use Milton

Instance Method Summary collapse

Instance Method Details

#content_typeObject

Returns the content_type of this attachment, tries to determine it if hasn’t been determined yet or is not saved to the database



113
114
115
116
# File 'lib/milton/attachment.rb', line 113

def content_type
  return self[:content_type] unless self[:content_type].blank?
  self.content_type = attached_file.mime_type
end

#content_type=(type) ⇒ Object

Sets the content type to the given type



119
120
121
# File 'lib/milton/attachment.rb', line 119

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

#filename=(name) ⇒ Object

Sets the filename to the given filename (sanitizes the given filename as well)

TODO: change the filename on the underlying file system on save so as not to orphan the file



107
108
109
# File 'lib/milton/attachment.rb', line 107

def filename=(name)
  write_attribute :filename, Storage::StoredFile.sanitize_filename(name, self.class.milton_options)
end

#path(options = nil) ⇒ Object

The path to the file.



137
138
139
140
# File 'lib/milton/attachment.rb', line 137

def path(options=nil)
  return attached_file.path if options.nil?
  process(options).path
end

#public_path(options = {}, base = 'public') ⇒ Object

Simple helper, same as path except returns the directory from …/public/ on, i.e. for showing images in your views.

@asset.path        => /var/www/site/public/assets/000/000/001/313/milton.jpg
@asset.public_path => /assets/000/000/001/313/milton.jpg

Can send a different base path than public if you want to give the path from that base on, useful if you change your root path to somewhere else.



132
133
134
# File 'lib/milton/attachment.rb', line 132

def public_path(options={}, base='public')
  path(options).gsub(/.*?\/#{base}/, '')
end