OSDN Git Service

t#30328:create op import ...and pull
[pettanr/pettanr.git] / app / models / picture.rb
index a64ce6e..bb0b1ca 100644 (file)
@@ -5,15 +5,15 @@ class Picture < ActiveRecord::Base
   belongs_to :artist
   has_one :resource_picture
   
-  validates :original_picture_id, :presence => true, :numericality => true, :existence => true
+  validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :revision, :presence => true, :numericality => true
   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 => {:minimum => 32, :maximum => 32}
-  validates :license_id, :presence => true, :numericality => true, :existence => true
-  validates :artist_id, :presence => true, :numericality => true, :existence => true
+  validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
+  validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :artist_name, :presence => true
   validates :classname, :presence => true, :length => {:maximum => 50}
   
@@ -43,9 +43,9 @@ class Picture < ActiveRecord::Base
     return true
   end
   
-  def showable? ar
-    return false unless ar.is_a?(Artist)
-    return true if self.own?(ar)
+  def showable? au = nil
+    return false unless self.original_picture
+    return true if self.own?(au)
     self.enable? and self.head?
   end
   
@@ -74,6 +74,15 @@ class Picture < ActiveRecord::Base
     {:src => self.url, :width => tw, :height => th}
   end
   
+  def tail_opt_img_tag img
+    {:src => img, :width => self.width, :height => self.height}
+  end
+  
+  def tail_tmb_opt_img_tag img
+    tw, th = PettanImager.thumbnail_size(self.width, self.height)
+    {:src => img, :width => tw, :height => th}
+  end
+  
   def new_revision
     Picture.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).to_i + 1
   end
@@ -83,8 +92,12 @@ class Picture < ActiveRecord::Base
     r ? true : false
   end
   
+  def self.head opid
+    Picture.find(:first, :conditions => ['original_picture_id = ?', opid], :order => 'pictures.revision desc')
+  end
+  
   def head
-    Picture.find(:first, :conditions => ['original_picture_id = ?', self.original_picture_id], :order => 'pictures.revision desc')
+    Picture.head(self.original_picture_id)
   end
   
   def head?
@@ -103,8 +116,13 @@ class Picture < ActiveRecord::Base
     r = Picture.find :all, :conditions => ['pictures.md5 = ?', md5], :order => 'pictures.updated_at desc'
   end
   
-  def self.list_by_md5 md5, opid
-    r = Picture.find :all, :conditions => ['pictures.md5 = ? and pictures.original_picture_id <> ?', md5, opid], :order => 'pictures.updated_at desc'
+  def self.list_by_md5 md5, opid = nil
+    cond = if opid.blank?
+      ['pictures.md5 = ?', md5]
+    else
+      ['pictures.md5 = ? and pictures.original_picture_id <> ?', md5, opid]
+    end
+    r = Picture.find :all, :conditions => cond, :order => 'pictures.updated_at desc'
   end
   
   def self.exist_by_md5 md5, opid
@@ -172,6 +190,12 @@ class Picture < ActiveRecord::Base
     self.to_json({:methods => :picture_data})
   end
   
+  def unpublish
+    imager = PettanImager.load(File.open(Rails.root + 'app/assets/images/error.png', 'rb').read)
+    return false unless imager
+    self.store imager
+  end
+  
   def credit_template
     "#{self.classname.tableize}/attributes/credit"
   end