OSDN Git Service

t#30328:pull porting
[pettanr/pettanr.git] / app / models / original_picture.rb
index f55c06b..17eed3a 100644 (file)
@@ -15,22 +15,20 @@ class OriginalPicture < ActiveRecord::Base
   end
   
   def overwrite ar
+    self.uploaded_at = Time.now
     self.artist_id = ar.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 = OriginalPicture.get_artist_from_roles roles
+    return false unless ar
+    self.artist_id == ar.id
   end
   
-  def visible? ar
-    return true if ar.is_a?(Admin)
-    self.own?(ar)
+  def visible? roles
+    return true if self.admin_role_check roles
+    self.own?(roles)
   end
   
   def filename
@@ -55,19 +53,21 @@ class OriginalPicture < ActiveRecord::Base
   end
   
   def unpublished?
-    self.pictures.empty?
+    self.published_at == nil and self.stopped_at == nil
   end
   
   def stopped?
-    self.pictures.any? and self.resource_picture == nil
+    self.stopped_at != nil
   end
   
   def unlicensed?
-    self.pictures.any? and self.resource_picture and self.updated_at > self.pictures.first.head.updated_at
+    dt = self.published_at || self.stopped_at
+    return false unless dt
+    self.uploaded_at > dt
   end
   
   def published?
-    self.pictures.any? and self.resource_picture and self.updated_at < self.pictures.first.head.updated_at
+    self.published_at != nil
   end
   
   def self.default_page_size
@@ -111,11 +111,11 @@ class OriginalPicture < ActiveRecord::Base
     {:include => {:resource_picture => {}, :pictures => {}}}
   end
   
-  def self.show cid, ar
+  def self.show cid, roles
     opt = {}
     opt.merge!(self.show_opt)
     res = OriginalPicture.find(cid, opt)
-    raise ActiveRecord::Forbidden unless res.visible?(ar)
+    raise ActiveRecord::Forbidden unless res.visible?(roles)
     res
   end
   
@@ -160,6 +160,26 @@ class OriginalPicture < ActiveRecord::Base
     PictureIO.original_picture_io.get self.filename, subdir
   end
   
+  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
+  
   def destroy_with_resource_picture
     res = false
     OriginalPicture.transaction do
@@ -183,10 +203,4 @@ class OriginalPicture < ActiveRecord::Base
     res
   end
   
-  def self.export ar
-    l = LicenseGroup.list
-    op = OriginalPicture.list ar.id
-    {:license_groups => l, :original_pictures => op}
-  end
-  
 end