OSDN Git Service

t#30328:add export
[pettanr/pettanr.git] / app / models / picture.rb
index a4b9e38..a64ce6e 100644 (file)
@@ -21,19 +21,32 @@ class Picture < ActiveRecord::Base
   end
   
   def overwrite rp
-    attr = {:revision => self.new_revision,:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize, 
+    attr = {:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize, 
       :original_picture_id => rp.original_picture_id, :license_id => rp.license_id, :artist_id => rp.artist_id, 
       :md5 => rp.md5, :artist_name => rp.artist_name, :classname => rp.classname, :credit => rp.credit, :settings => rp.settings
     }
     self.attributes = attr
+    self.revision = self.new_revision   #Do not move to attr. new_revision reffernces self.original_picture_id
   end
   
-  def own? author
-    return false
+  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
+  end
+  
+  def visible? ar
+    return true
   end
   
-  def visible? author
-    true
+  def showable? ar
+    return false unless ar.is_a?(Artist)
+    return true if self.own?(ar)
+    self.enable? and self.head?
   end
   
   def filename
@@ -53,6 +66,10 @@ class Picture < 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
@@ -61,11 +78,19 @@ class Picture < ActiveRecord::Base
     Picture.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).to_i + 1
   end
   
-  def head?
-    r = self.resource_picture
+  def enable?
+    r = self.head.resource_picture
     r ? true : false
   end
   
+  def head
+    Picture.find(:first, :conditions => ['original_picture_id = ?', self.original_picture_id], :order => 'pictures.revision desc')
+  end
+  
+  def head?
+    self == head
+  end
+  
   def to_gif?
     self.ext == 'png' and self.flag_gif_convert >= 0
   end
@@ -123,10 +148,38 @@ 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 credit_template
     "#{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