Class: Versions::SharedAttachment

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

Instance Method Summary collapse

Instance Method Details

#fileObject



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

def file
  File.new(filepath)
end

#file=(file) ⇒ Object



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

def file=(file)
  @file = file
  self[:filename] = get_filename(file)
end

#filepathObject



25
26
27
28
29
# File 'lib/versions/shared_attachment.rb', line 25

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


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

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