OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / app / models / picture.rb
index b9d7da1..46a0de4 100644 (file)
@@ -17,6 +17,15 @@ class Picture < ActiveRecord::Base
   validates :artist_name, :presence => true
   validates :classname, :presence => true, :length => {:maximum => 50}
   
+  before_validation :valid_encode
+  
+  def valid_encode
+    ['artist_name', 'classname', 'credit', 'settings'].each do |a|
+      next if attributes[a] == nil
+      raise Pettanr::BadRequest unless attributes[a].valid_encoding?
+    end
+  end
+  
   def supply_default
   end
   
@@ -29,14 +38,11 @@ class Picture < ActiveRecord::Base
     self.revision = self.new_revision   #Do not move to attr. new_revision reffernces self.original_picture_id
   end
   
-  def own? ar
-    if ar.is_a?(Author)
-      self.artist_id == ar.artist.id
-    elsif ar.is_a?(Artist)
-      self.artist_id == ar.id
-    else
-      false
-    end
+  def own? roles
+    roles = [roles] unless roles.respond_to?(:each)
+    ar = Picture.get_artist_from_roles roles
+    return false unless ar
+    self.artist_id == ar.id
   end
   
   def visible? ar
@@ -44,6 +50,7 @@ class Picture < ActiveRecord::Base
   end
   
   def showable? au = nil
+    return false unless self.original_picture
     return true if self.own?(au)
     self.enable? and self.head?
   end
@@ -91,8 +98,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?
@@ -124,10 +135,10 @@ class Picture < ActiveRecord::Base
     Picture.list_by_md5(md5, opid).empty? ? false : true
   end
   
-  def self.show rid, au
+  def self.show rid, roles
     opt = {}
     r = Picture.find(rid, opt)
-    raise ActiveRecord::Forbidden unless r.visible?(au)
+    raise ActiveRecord::Forbidden unless r.visible?(roles)
     r
   end
   
@@ -161,6 +172,36 @@ class Picture < ActiveRecord::Base
     PictureIO.picture_io.get self.filename, subdir
   end
   
+  def self.export(dt = nil)
+    opt = {}
+    cond = if dt
+      ['artists.author_id is not null and pictures.updated_at >= ?', dt]
+    else
+      'artists.author_id is not null'
+    end
+    opt.merge!({:conditions => cond}) 
+    opt.merge!({:include => {:artist => {}}, :order => 'pictures.updated_at desc'})
+    Picture.find(:all, opt)
+  end
+  
+  def self.list_as_json_text ary
+    '[' + ary.map {|i| i.to_json_with_picture_data }.join(',') + ']'
+  end
+  
+  def picture_data
+    Base64.encode64(self.restore)
+  end
+  
+  def to_json_with_picture_data
+    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