OSDN Git Service

fix manifest/system_resource
[pettanr/pettanr.git] / app / models / picture.rb
index 771a4a4..89b95fb 100644 (file)
@@ -1,5 +1,5 @@
 #実素材
-class Picture < ActiveRecord::Base
+class Picture < Pettanr::Content
   belongs_to :original_picture
   belongs_to :license
   belongs_to :artist
@@ -17,6 +17,14 @@ class Picture < ActiveRecord::Base
   validates :artist_name, :presence => true
   validates :classname, :presence => true, :length => {:maximum => 50}
   
+  def self.owner_type
+    :artist
+  end
+  
+  def self.valid_encode_columns
+    super + ['artist_name', 'classname', 'credit', 'settings']
+  end
+  
   def supply_default
   end
   
@@ -29,20 +37,13 @@ class Picture < ActiveRecord::Base
     self.revision = self.new_revision   #Do not move to attr. new_revision reffernces self.original_picture_id
   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
+  def visible? operators
     return true
   end
   
-  def showable? au = nil
+  def showable? operators = nil
     return false unless self.original_picture
-    return true if self.own?(au)
+    return true if self.own?(operators)
     self.enable? and self.head?
   end
   
@@ -80,6 +81,10 @@ class Picture < ActiveRecord::Base
     {:src => img, :width => tw, :height => th}
   end
   
+  def symbol_option
+    self.tmb_opt_img_tag
+  end
+  
   def new_revision
     Picture.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).to_i + 1
   end
@@ -89,8 +94,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?
@@ -122,11 +131,20 @@ class Picture < ActiveRecord::Base
     Picture.list_by_md5(md5, opid).empty? ? false : true
   end
   
-  def self.show rid, roles
-    opt = {}
-    r = Picture.find(rid, opt)
-    raise ActiveRecord::Forbidden unless r.visible?(roles)
-    r
+  def self.list_by_original_picture_where original_picture_id
+    ['pictures.original_picture_id = ?', original_picture_id]
+  end
+  
+  def self.list_by_original_picture original_picture_id, roles, page = 1, page_size = self.default_page_size
+    self.where(self.list_by_original_picture_where(original_picture_id)).includes(self.list_opt).order('pictures.revision desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.list_opt
+    {:license => {}, :artist => {}}
+  end
+  
+  def self.list_json_opt
+    {:include => {:license => {}, :artist => {}} }
   end
   
   def store(imager)
@@ -159,6 +177,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