Class: Grip::Attachment

Inherits:
Object
  • Object
show all
Includes:
MongoMapper::Document
Defined in:
lib/grip/attachment.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_method(method_name, &block) ⇒ Object



44
45
46
47
48
# File 'lib/grip/attachment.rb', line 44

def self.create_method method_name, &block
  define_method(method_name) do |*args|
    yield *args
  end
end

Instance Method Details

#fileObject



36
37
38
# File 'lib/grip/attachment.rb', line 36

def file
  read_from_grid grid_key
end

#file=(new_file) ⇒ Object

Raises:



25
26
27
28
29
30
31
32
33
34
# File 'lib/grip/attachment.rb', line 25

def file=new_file
      return if new_file.nil? || (new_file.is_a?(String) && new_file.blank?)
  raise InvalidFile unless (new_file.is_a?(File) || new_file.is_a?(Tempfile))

  self.file_name    = new_file.is_a?(Tempfile) ? new_file.original_filename : File.basename(new_file.path)
  self.file_size    = File.size(new_file.path)
  self.content_type = MIME::Types.type_for(self.file_name)

  write_to_grid self, new_file
end

#grid_keyObject



40
41
42
# File 'lib/grip/attachment.rb', line 40

def grid_key
  "#{owner_type.pluralize}/#{owner_id}/#{name}/#{file_name}".downcase
end