OSDN Git Service

import all source code
[pettanr/pettanr.git] / app / models / original_picture.rb
1 class OriginalPicture < ActiveRecord::Base
2   belongs_to :artist
3   belongs_to :lisence
4   has_one :resource_picture
5   
6   def validate
7     errors.add(:filesize, 'size over(1MB)') if self.filesize > 1000000
8   end
9   
10   def dext
11     self.ext.downcase
12   end
13   
14   def filename
15     "#{self.id}.#{self.dext}"
16   end
17   
18   def mime_type
19     "image/#{self.dext}"
20   end
21   
22   def url
23     '/original_pictures/' + filename
24   end
25   
26   def store(rimg)
27     bindata = rimg.to_blob
28     PictureIO.original_picture_io.put bindata, self.filename
29     res = if self.resource_picture
30       self.resource_picture.store rimg
31     else
32       ResourcePicture.store(rimg, self)
33     end
34     res
35   end
36   
37   def restore(subdir = nil)
38     PictureIO.original_picture_io.get self.filename, subdir
39   end
40   
41   def own? author
42     return false unless author
43     return false unless author.artist?
44     self.artist_id == author.artist.id
45   end
46   
47 end