Class: Versions::SharedAttachment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/versions/shared_attachment.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.filepath(id, filename) ⇒ Object



9
10
11
12
# File 'lib/versions/shared_attachment.rb', line 9

def self.filepath(id, filename)
  digest = ::Digest::SHA1.hexdigest(id.to_s)
  "#{digest[0..0]}/#{digest[1..1]}/#{filename}"
end

Instance Method Details

#fileObject



35
36
37
# File 'lib/versions/shared_attachment.rb', line 35

def file
  File.new(filepath)
end

#file=(file) ⇒ Object



21
22
23
24
# File 'lib/versions/shared_attachment.rb', line 21

def file=(file)
  @file = file
  self.filename = get_filename(file)
end

#filename=(name) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/versions/shared_attachment.rb', line 26

def filename=(name)
  fname = name.gsub(/[^a-zA-Z\-_0-9\.]/,'')
  if fname[0..0] == '.'
    # Forbid names starting with a dot
    fname = Digest::SHA1.hexdigest(Time.now.to_i.to_s)[0..6]
  end
  self[:filename] = fname
end

#filepathObject



39
40
41
# File 'lib/versions/shared_attachment.rb', line 39

def filepath
  @filepath ||= self.class.filepath(self[:id], filename)
end


14
15
16
17
18
19
# File 'lib/versions/shared_attachment.rb', line 14

def unlink(model)
  link_count = model.class.count(:conditions => ["attachment_id = ? AND id != ?", self.id, model.id])
  if link_count == 0
    destroy
  end
end