OSDN Git Service

fix: suport yasapp oauth
[pettanr/pettanr.git] / app / models / picture.rb
index 0725faf..d147a1f 100644 (file)
@@ -3,8 +3,10 @@ class Picture < Peta::Content
   load_manifest
   belongs_to :original_picture
   belongs_to :license
+  belongs_to :system_picture
   belongs_to :artist
   has_one :resource_picture
+  has_one :resource_picture_picture
   
   validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :revision, :presence => true, :numericality => true
@@ -14,18 +16,39 @@ class Picture < Peta::Content
   validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
   validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
   validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
+  validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
-  validates :artist_name, :presence => true
-  validates :license_group_classname, :presence => true, :length => {:maximum => 50}
+  validates :license_group_module_name, :presence => true, :length => {:maximum => 50}
+  
+  scope :find_index, -> do
+    self
+  end
+  
+  scope :find_by_artist, -> (artist_id) do 
+    find_index.where(artist_id: artist_id)
+  end
+  
+  scope :find_by_md5, -> (md5) do 
+    find_index.where(md5: md5).order(updated_at: :desc)
+  end
+  
+  scope :find_by_md5_without_original_picture, -> (md5, original_picture_id) do 
+    self.find_by_md5(md5).where.not(original_picture_id: original_picture_id).order(updated_at: :desc)
+  end
+  
+  scope :find_by_original_picture, -> (original_picture_id) do 
+    find_index.where(original_picture_id: original_picture_id)
+  end
   
   def supply_default
   end
   
   def overwrite rp
     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, 
-      :license_group_classname => rp.license_group_classname, 
+      :original_picture_id => rp.original_picture_id, :license_id => rp.license_id, 
+      :system_picture_id => rp.system_picture_id, :artist_id => rp.artist_id, 
+      :md5 => rp.md5, 
+      :license_group_module_name => rp.license_group_module_name, 
       :license_group_settings => rp.license_group_settings, 
       :credit_picture_settings => rp.credit_picture_settings,
       :license_settings => rp.license_settings
@@ -78,12 +101,16 @@ class Picture < Peta::Content
     {:src => img, :width => tw, :height => th}
   end
   
+  def alt_name
+    self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']'
+  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
+    Picture.where(original_picture_id: self.original_picture_id).maximum(:revision).to_i + 1
   end
   
   def enable?
@@ -92,7 +119,7 @@ class Picture < Peta::Content
   end
   
   def self.head opid
-    Picture.find(:first, :conditions => ['original_picture_id = ?', opid], :order => 'pictures.revision desc')
+    Picture.where(original_picture_id: opid).order('pictures.revision desc').first
   end
   
   def head
@@ -111,21 +138,8 @@ class Picture < Peta::Content
     self.license_extend.reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
   end
   
-  def self.find_by_md5 md5
-    r = Picture.find :all, :conditions => ['pictures.md5 = ?', md5], :order => 'pictures.updated_at desc'
-  end
-  
-  def self.list_by_md5 md5, opid = nil
-    cond = if opid.blank?
-      ['pictures.md5 = ?', md5]
-    else
-      ['pictures.md5 = ? and pictures.original_picture_id <> ?', md5, opid]
-    end
-    r = Picture.find :all, :conditions => cond, :order => 'pictures.updated_at desc'
-  end
-  
   def self.exist_by_md5 md5, opid
-    Picture.list_by_md5(md5, opid).empty? ? false : true
+    Picture.find_by_md5(md5, opid).empty? ? false : true
   end
   
   def self.list_by_original_picture_where original_picture_id
@@ -175,15 +189,9 @@ class Picture < Peta::Content
   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)
+    pictures = Picture.includes(:artist).where('artists.author_id is not null')
+    pictures = pictures.where(['pictures.updated_at >= ?', dt]) if dt
+    pictures.order('pictures.updated_at desc').references(:artist)
   end
   
   def self.list_as_json_text ary
@@ -199,17 +207,18 @@ class Picture < Peta::Content
   end
   
   def unpublish
+    self.boosts 'post'
     imager = PettanImager.load(File.open(Rails.root + 'app/assets/images/error.png', 'rb').read)
     return false unless imager
     self.store imager
   end
   
   def credit_template
-    "#{self.license_group_classname.tableize}/attributes/credit"
+    "#{self.license_group_module_name.tableize}/attributes/credit"
   end
   
   def full_credit_template
-    "#{self.license_group_classname.tableize}/attributes/full_credit"
+    "#{self.license_group_module_name.tableize}/attributes/full_credit"
   end
   
 end