OSDN Git Service

upgrade rails 3.2
[pettanr/pettanr.git] / app / models / system_picture.rb
index db857b7..8b25d19 100644 (file)
@@ -1,12 +1,15 @@
-class SystemPicture < ActiveRecord::Base
+class SystemPicture < Peta::SystemResource
+  load_manifest
   has_many :balloons
-  has_many :balloon_templates
+  has_many :speech_balloon_templates
+  has_many :licenses
+  has_many :writing_formats
   
   validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
   validates :width, :presence => true, :numericality => true, :natural_number => true
   validates :height, :presence => true, :numericality => true, :natural_number => true
   validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
-  validates :md5, :presence => true, :length => {:maximum => 32}
+  validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
   
   before_destroy :destroy_with_file
   
@@ -14,53 +17,65 @@ class SystemPicture < ActiveRecord::Base
     PictureIO.system_picture_io.delete self.filename
   end
   
-  def dext
-    self.ext.downcase
+  def supply_default
+  end
+  
+  def overwrite
   end
   
   def filename
-    "#{self.id}.#{self.dext}"
+    "#{self.id}.#{self.ext}"
   end
   
   def mime_type
-    "image/#{self.dext}"
+    "image/#{self.ext}"
   end
   
   def url
     '/system_pictures/' + filename
   end
   
-  def data_to_mgk picture_data
-    begin
-      mgk = Magick::Image.from_blob(picture_data).shift
-    rescue 
-      self.errors.add :base, 'magick failed'
-      return false
-    end
-    mgk
+  def opt_img_tag
+    {:src => self.url, :width => self.width, :height => self.height}
+  end
+  
+  def tmb_opt_img_tag
+    tw, th = PettanImager.thumbnail_size(self.width, self.height)
+    {:src => self.url, :width => tw, :height => th}
   end
   
-  def store(picture_data)
-    mgk = data_to_mgk picture_data
-    return false unless mgk
+  def symbol_option
+    self.tmb_opt_img_tag
+  end
+  
+  def self.show_opt
+    {}
+  end
+  
+  def store(imager)
+    unless imager
+      self.errors.add :base, 'illegal picture data'
+      return false
+    end
     res = false
-    self.attributes = {:ext => mgk.format.downcase, :width => mgk.columns, :height => mgk.rows, 
-      :filesize => mgk.filesize
-    }
-    self.md5 = Digest::MD5.hexdigest(picture_data) unless self.md5
     SystemPicture.transaction do
       if res = self.save
-        res = PictureIO.system_picture_io.put(picture_data, self.filename)
+        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
     end
     res
   end
   
-  def self.store(picture_data)
-    md5 = Digest::MD5.hexdigest(picture_data)
-    sp = SystemPicture.find_by_md5(md5)
-    sp = SystemPicture.new() unless sp
-    res = sp.store picture_data
+  def self.store(imager)
+    attr = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
+    sp = SystemPicture.modify_object(imager.md5, attr, 'md5')
+    res = sp.store imager
     res ? sp : nil
   end