OSDN Git Service

9d018318c8932a6dcc88ba7eba34189c579196c3
[pettanr/pettanr.git] / app / models / system_picture.rb
1 class SystemPicture < ActiveRecord::Base
2   has_many :balloons
3   has_many :balloon_templates
4   
5   before_destroy :destroy_with_file
6   
7   def validate
8     errors.add(:filesize, 'size over(1MB)') if self.filesize > 1000000
9   end
10   
11   def destroy_with_file
12     PictureIO.system_picture_io.delete self.filename
13   end
14   
15   def dext
16     self.ext.downcase
17   end
18   
19   def filename
20     "#{self.id}.#{self.dext}"
21   end
22   
23   def mime_type
24     "image/#{self.dext}"
25   end
26   
27   def url
28     '/system_pictures/' + filename
29   end
30   
31   def store(rimg)
32     bindata = rimg.to_blob
33     PictureIO.system_picture_io.put bindata, self.filename
34     true
35   end
36   
37   def restore(subdir = nil)
38     PictureIO.system_picture_io.get self.filename, subdir
39   end
40   
41 end