1 class SystemPicture < ActiveRecord::Base
3 has_many :balloon_templates
6 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
7 validates :width, :presence => true, :numericality => true, :natural_number => true
8 validates :height, :presence => true, :numericality => true, :natural_number => true
9 validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
10 validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
12 before_destroy :destroy_with_file
15 PictureIO.system_picture_io.delete self.filename
25 roles = [roles] unless roles.respond_to?(:each)
26 ad = SystemPicture.get_admin_from_roles roles
27 return false unless ad
36 "#{self.id}.#{self.ext}"
44 '/system_pictures/' + filename
48 {:src => self.url, :width => self.width, :height => self.height}
52 tw, th = PettanImager.thumbnail_size(self.width, self.height)
53 {:src => self.url, :width => tw, :height => th}
60 def self.default_page_size
64 def self.max_page_size
68 def self.page prm = nil
74 def self.page_size prm = self.default_page_size
76 page_size = self.max_page_size if page_size > self.max_page_size
77 page_size = self.default_page_size if page_size < 1
85 def self.list page = 1, page_size = self.default_page_size
86 SystemPicture.where(self.list_where()).includes(SystemPicture.list_opt).order('system_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
89 def self.list_paginate page = 1, page_size = self.default_page_size
90 Kaminari.paginate_array(Array.new(SystemPicture.where(self.list_where()).count, nil)).page(page).per(page_size)
97 def self.list_json_opt
101 def self.show sid, ad
103 opt.merge!(self.show_opt)
104 res = SystemPicture.find(sid, opt)
105 raise ActiveRecord::Forbidden unless res.visible?(ad)
113 def self.show_json_opt
119 self.errors.add :base, 'illegal picture data'
123 SystemPicture.transaction do
126 res = PictureIO.system_picture_io.put(imager.binary, self.filename)
127 rescue PictureIO::Error
129 self.errors.add :base, I18n.t('picture_io.error')
130 raise ActiveRecord::Rollback
137 def self.store(imager)
138 attr = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
139 sp = SystemPicture.modify_object(imager.md5, attr, 'md5')
140 res = sp.store imager
144 def restore(subdir = nil)
145 PictureIO.system_picture_io.get self.filename, subdir