OSDN Git Service

t30350#:fix destroy in op, p, user
[pettanr/pettanr.git] / app / models / original_picture.rb
index 534e337..f55c06b 100644 (file)
@@ -8,11 +8,9 @@ class OriginalPicture < ActiveRecord::Base
   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 :artist_id, :presence => true, :numericality => true, :existence => true
+  validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
   
-  before_destroy :destroy_with_file
-  
   def supply_default
   end
   
@@ -21,10 +19,17 @@ class OriginalPicture < ActiveRecord::Base
   end
   
   def own? ar
-    self.artist_id == ar.id
+    if ar.is_a?(Author)
+      self.artist_id == ar.artist.id
+    elsif ar.is_a?(Artist)
+      self.artist_id == ar.id
+    else
+      false
+    end
   end
   
   def visible? ar
+    return true if ar.is_a?(Admin)
     self.own?(ar)
   end
   
@@ -41,10 +46,30 @@ class OriginalPicture < ActiveRecord::Base
   end
   
   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 unpublished?
+    self.pictures.empty?
+  end
+  
+  def stopped?
+    self.pictures.any? and self.resource_picture == nil
+  end
+  
+  def unlicensed?
+    self.pictures.any? and self.resource_picture and self.updated_at > self.pictures.first.head.updated_at
+  end
+  
+  def published?
+    self.pictures.any? and self.resource_picture and self.updated_at < self.pictures.first.head.updated_at
+  end
+  
   def self.default_page_size
     25
   end
@@ -110,24 +135,20 @@ class OriginalPicture < ActiveRecord::Base
     {:include => {:resource_picture => {}, :pictures => {}}}
   end
   
-  def destroy_with_file
-    PictureIO.original_picture_io.delete self.filename
-    self.resource_picture.destroy
-  end
-  
   def store(imager)
     unless imager
-      self.errors.add :base, 'illegal picture data'
+      self.errors.add :base, I18n.t('errors.invalid_image')
       return false
     end
     res = false
     self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
     OriginalPicture.transaction do
       if res = self.save
-        if res = PictureIO.original_picture_io.put(imager.binary, self.filename)
-          res = true
-        else
-          self.errors.add :base, 'original picture io does not work'
+        begin
+          res = PictureIO.original_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
@@ -139,6 +160,29 @@ class OriginalPicture < ActiveRecord::Base
     PictureIO.original_picture_io.get self.filename, subdir
   end
   
+  def destroy_with_resource_picture
+    res = false
+    OriginalPicture.transaction do
+      begin
+        PictureIO.original_picture_io.delete(self.filename) if PictureIO.original_picture_io.exist?(self.filename)
+      rescue PictureIO::Error
+        res = false
+        raise ActiveRecord::Rollback
+      end
+      if self.resource_picture
+        res = self.resource_picture.unpublish
+        raise ActiveRecord::Rollback unless res
+      end
+      self.pictures.each do |picture|
+        res = picture.unpublish
+        raise ActiveRecord::Rollback unless res
+      end
+      res = self.destroy
+      raise ActiveRecord::Rollback unless res
+    end
+    res
+  end
+  
   def self.export ar
     l = LicenseGroup.list
     op = OriginalPicture.list ar.id