OSDN Git Service

t#30328:pull porting
[pettanr/pettanr.git] / app / models / picture.rb
index 771a4a4..fb85714 100644 (file)
@@ -89,8 +89,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?
@@ -159,6 +163,30 @@ 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