OSDN Git Service

t#30328:add export
[pettanr/pettanr.git] / app / models / original_picture.rb
index 534e337..110987a 100644 (file)
@@ -21,10 +21,17 @@ class OriginalPicture < ActiveRecord::Base
   end
   
   def own? ar
-    self.artist_id == ar.id
+    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 if ar.is_a?(Admin)
     self.own?(ar)
   end
   
@@ -41,6 +48,10 @@ class OriginalPicture < 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
@@ -139,10 +150,24 @@ class OriginalPicture < ActiveRecord::Base
     PictureIO.original_picture_io.get self.filename, subdir
   end
   
-  def self.export ar
-    l = LicenseGroup.list
-    op = OriginalPicture.list ar.id
-    {:license_groups => l, :original_pictures => op}
+  def self.export(dt = nil)
+    opt = {}
+    cond = if dt
+      ['artists.author_id is not null and original_pictures.updated_at >= ?', dt]
+    else
+      'artists.author_id is not null'
+    end
+    opt.merge!({:conditions => cond}) 
+    opt.merge!({:include => {:resource_picture => {}, :artist => {}}, :order => 'original_pictures.id'})
+    OriginalPicture.find(:all, opt)
+  end
+  
+  def list_as_json_with_resource_picture
+    self.to_json({:include => {:resource_picture => {:methods => :picture_data}}})
+  end
+  
+  def self.list_as_json_text ary
+    '[' + ary.map {|i| i.list_as_json_with_resource_picture }.join(',') + ']'
   end
   
 end