OSDN Git Service

t#32402:add file prof
[pettanr/pettanr.git] / app / models / system_picture.rb
index cd4809b..1d090bc 100644 (file)
@@ -21,7 +21,10 @@ class SystemPicture < ActiveRecord::Base
   def overwrite
   end
   
-  def own? ad
+  def own? roles
+    roles = [roles] unless roles.respond_to?(:each)
+    ad = SystemPicture.get_admin_from_roles roles
+    return false unless ad
     true
   end
   
@@ -50,6 +53,10 @@ class SystemPicture < ActiveRecord::Base
     {:src => self.url, :width => tw, :height => th}
   end
   
+  def symbol_option
+    self.tmb_opt_img_tag
+  end
+  
   def self.default_page_size
     25
   end
@@ -71,12 +78,16 @@ class SystemPicture < ActiveRecord::Base
     page_size
   end
   
+  def self.list_where
+    nil
+  end
+  
   def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(self.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:order => 'system_pictures.updated_at desc'})
-    SystemPicture.find(:all, opt)
+    SystemPicture.where(self.list_where()).includes(SystemPicture.list_opt).order('system_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.list_paginate page = 1, page_size = self.default_page_size
+    Kaminari.paginate_array(Array.new(SystemPicture.where(self.list_where()).count, nil)).page(page).per(page_size)
   end
   
   def self.list_opt
@@ -111,10 +122,11 @@ class SystemPicture < ActiveRecord::Base
     res = false
     SystemPicture.transaction do
       if res = self.save
-        if res = PictureIO.system_picture_io.put(imager.binary, self.filename)
-          res = true
-        else
-          self.errors.add :base, 'system picture io does not work'
+        begin
+          res = PictureIO.system_picture_io.put(imager.binary, self.filename)
+        rescue PictureIO::Error
+          res = false
+          self.errors.add :base, I18n.t('picture_io.error')
           raise ActiveRecord::Rollback
         end
       end