Module: Grip

Defined in:
lib/grip.rb

Defined Under Namespace

Modules: ClassMethods Classes: InvalidFileException

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/grip.rb', line 7

def self.included(base)
  base.extend Grip::ClassMethods
  base.class_eval do
    after_save      :save_attachments
    before_destroy  :destroy_attached_files
  end
end

Instance Method Details

#destroy_attached_filesObject



111
112
113
114
115
116
117
118
119
120
# File 'lib/grip.rb', line 111

def destroy_attached_files
  self.class.attachment_definitions.each do |name, attachment|
    GridFS::GridStore.unlink(self.class.database, self["#{name}_path"])
    unless attachment[:versions].nil?
      attachment[:versions].each do |v,dim|
        GridFS::GridStore.unlink(self.class.database, self["#{name}_#{v}_path"])
      end
    end
  end unless self.class.attachment_definitions.nil?
end

#save_attachmentsObject

Roll through attachment definitions and check if they are a File or Tempfile. Both types are nescessary for file uploads to work properly. Each file checks for a <attr_name>_process callback for pre-processing before save.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/grip.rb', line 79

def save_attachments
  self.class.attachment_definitions.each do |definition|
    attr_name, opts = definition
    
    GridFS::GridStore.open(self.class.database, self["#{attr_name}_path"], 'w', :content_type => self["#{attr_name}_content_type"]) do |f|
      f.write opts[:file].read
    end
    
    unless opts[:versions].nil?
      opts[:versions].each do |version,dimensions|
        
        tmp   = Tempfile.new("#{attr_name}_#{version}")
        image = Miso::Image.new(opts[:file].path)
        
        image.crop(dimensions[:width], dimensions[:height])  if opts[:crop]
        image.fit(dimensions[:width], dimensions[:height])   unless opts[:crop]
        
        image.write(tmp.path)
        
        # update <name>_<version> attrs and assign the file
        send("#{attr_name}_#{version}=", tmp)
        
        GridFS::GridStore.open(self.class.database, self["#{attr_name}_#{version}_path"], 'w', :content_type => self["#{attr_name}_content_type"]) do |f|
          f.write tmp.read
        end
        
      end
      save_to_collection
    end
  end unless self.class.attachment_definitions.nil?
end

#update_attachment_attributes!(name, file, is_version = false) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/grip.rb', line 61

def update_attachment_attributes! name, file, is_version = false
  raise Grip::InvalidFileException unless (file.is_a?(File) || file.is_a?(Tempfile) || file.is_a?(Miso::Image))
  
  self["#{name}_size"]         = File.size(file) 
  self["#{name}_name"]         = File.basename(file.path)
  
  unless is_version
    self['_id']                  = Mongo::ObjectID.new if _id.blank?
    self["#{name}_content_type"] = file.content_type rescue MIME::Types.type_for(self["#{name}_name"]).to_s
    self["#{name}_path"]         = "#{self.class.to_s.underscore}/#{name}/#{_id}"
  else
    self["#{name}_path"]         = "#{self.class.to_s.underscore}/#{name}/#{_id}"
  end
end