OSDN Git Service

t#30278:asoc model existence both be false
[pettanr/pettanr.git] / app / models / resource_picture.rb
index 7bd555c..663c78b 100644 (file)
@@ -11,22 +11,12 @@ class ResourcePicture < ActiveRecord::Base
   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 :artist_id, :presence => true, :numericality => true, :existence => true
-  validates :license_id, :presence => true, :numericality => true, :existence => true
-  validates :original_picture_id, :presence => true, :numericality => true, :existence => true
+  validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
+  validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
+  validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :artist_name, :presence => true
   validates :classname, :presence => true, :length => {:maximum => 50}
-  validates :picture_id, :presence => true, :numericality => true, :existence => true
-  
-  before_destroy :destroy_with_file
-  
-  def destroy_with_file
-    PictureIO.resource_picture_io.delete self.filename
-    PictureIO.resource_picture_io.class.subdirs.each do |d|
-      next if d.empty?
-      PictureIO.resource_picture_io.delete(self.filename, d) if PictureIO.resource_picture_io.exist?(self.filename, d)
-    end
-  end
+  validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
   
   def supply_default
   end
@@ -38,11 +28,19 @@ class ResourcePicture < ActiveRecord::Base
     self.attributes = attr
   end
   
-  def own? author
-    return false
+  def own? ar
+    return false unless ar.is_a?(Artist)
+    ar.id == self.artist_id
   end
   
-  def visible? author
+  def visible? au
+    if au == nil
+      return false if MagicNumber['run_mode'] == 1
+    elsif au.is_a?(Author)
+      return true
+    else
+      return false
+    end
     true
   end
   
@@ -71,11 +69,15 @@ class ResourcePicture < ActiveRecord::Base
     ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
   end
   
-  def opt_img_tag
+  def tmb_opt_img_tag
     tw, th = PettanImager.thumbnail_size(self.width, self.height)
     {:src => self.url, :width => tw, :height => th}
   end
   
+  def opt_img_tag
+    {:src => self.url('full'), :width => self.width, :height => self.height}
+  end
+  
   def self.default_page_size
     25
   end
@@ -129,6 +131,14 @@ class ResourcePicture < ActiveRecord::Base
     {:include => {:license => {}, :artist => {}, :picture => {}} }
   end
   
+  def self.edit rid, ar
+    opt = {}
+    opt.merge!(self.show_opt)
+    r = ResourcePicture.find(rid, opt)
+    raise ActiveRecord::Forbidden unless r.own?(ar)
+    r
+  end
+  
   def new_picture imager
     pc = Picture.new
     pc.supply_default
@@ -186,6 +196,16 @@ class ResourcePicture < ActiveRecord::Base
     PictureIO.resource_picture_io.get self.filename, subdir
   end
   
+  def unpublish
+    r = true
+    ResourcePicture.transaction do
+      self.destroy
+      PictureIO.resource_picture_io.delete(self.filename)
+      PictureIO.resource_picture_io.delete(self.filename, 'full')
+    end
+    r
+  end
+  
   def self.visible_count
     ResourcePicture.count
   end
@@ -194,6 +214,10 @@ class ResourcePicture < ActiveRecord::Base
     "#{self.classname.tableize}/attributes/credit"
   end
   
+  def full_credit_template
+    "#{self.classname.tableize}/attributes/full_credit"
+  end
+  
   def credit_data
     begin
       @credit_data = JSON.parse(self.credit) unless @credit_data