1 class SystemPicture < Peta::SystemResource
4 has_many :speech_balloon_templates
6 has_many :writing_formats
8 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
9 validates :width, :presence => true, :numericality => true, :natural_number => true
10 validates :height, :presence => true, :numericality => true, :natural_number => true
11 validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
12 validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
14 before_destroy :destroy_with_file
16 scope :find_index, -> do
21 PictureIO.system_picture_io.delete self.filename
31 "#{self.id}.#{self.ext}"
39 '/system_pictures/' + filename
43 {:src => self.url, :width => self.width, :height => self.height}
47 tw, th = PettanImager.thumbnail_size(self.width, self.height)
48 {:src => self.url, :width => tw, :height => th}
61 self.errors.add :base, 'illegal picture data'
65 SystemPicture.transaction do
68 res = PictureIO.system_picture_io.put(imager.binary, self.filename)
69 rescue PictureIO::Error
71 self.errors.add :base, I18n.t('picture_io.error')
72 raise ActiveRecord::Rollback
79 def self.store(imager)
80 attr = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
81 sp = SystemPicture.modify_object(imager.md5, attr, 'md5')
86 def restore(subdir = nil)
87 PictureIO.system_picture_io.get self.filename, subdir