OSDN Git Service

fic picture publishing
[pettanr/pettanr.git] / app / models / resource_picture.rb
index edc43be..c6975ef 100644 (file)
@@ -1,9 +1,10 @@
 #素材
-class ResourcePicture < ActiveRecord::Base
+class ResourcePicture < Peta::Content
+  load_manifest
   belongs_to :artist
   belongs_to :license
+  belongs_to :system_picture
   belongs_to :picture
-  has_many :panel_pictures
   belongs_to :original_picture
   
   validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
@@ -13,9 +14,10 @@ class ResourcePicture < ActiveRecord::Base
   validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
   validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
+  validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :artist_name, :presence => true
-  validates :classname, :presence => true, :length => {:maximum => 50}
+  validates :license_group_classname, :presence => true, :length => {:maximum => 50}
   validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
   
   def supply_default
@@ -28,20 +30,10 @@ class ResourcePicture < ActiveRecord::Base
     self.attributes = attr
   end
   
-  def own? ar
-    return false unless ar.is_a?(Artist)
-    ar.id == self.artist_id
-  end
-  
-  def visible? au
-    if au == nil
-      return false if MagicNumber['run_mode'] == 1
-    elsif au.is_a?(Author)
-      return true
-    else
-      return false
-    end
-    true
+  def visible? operators
+    # no super
+    # content model call to owner checker
+    self.user_visible? operators
   end
   
   def filename
@@ -57,15 +49,15 @@ class ResourcePicture < ActiveRecord::Base
   end
   
   def url subdir = nil
-    '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
+    '/resource_pictures/' + filename + (subdir.to_s.empty? ? '' : '?subdir=' + subdir.to_s)
   end
   
   def to_gif?
-    self.ext == 'png' and self.flag_gif_convert >= 0
+    self.ext == 'png' and self.license_extend.gif_convert >= 0
   end
   
   def thumbnail(imager)
-    tw, th = ResourcePicture.fix_size_both(MagicNumber['thumbnail_width'], MagicNumber['thumbnail_height'], rimg.columns, rimg.rows)
+    tw, th = ResourcePicture.fix_size_both(Manifest.manifest.magic_numbers['thumbnail_width'], Manifest.manifest.magic_numbers['thumbnail_height'], rimg.columns, rimg.rows)
     ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
   end
   
@@ -78,59 +70,30 @@ class ResourcePicture < ActiveRecord::Base
     {:src => self.url('full'), :width => self.width, :height => self.height}
   end
   
-  def self.default_page_size
-    25
-  end
-  
-  def self.max_page_size
-    100
+  def alt_name
+    self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']'
   end
   
-  def self.page prm = nil
-    page = prm.to_i
-    page = 1 if page < 1
-    page
+  def symbol_option
+    self.tmb_opt_img_tag
   end
   
-  def self.page_size prm = self.default_page_size
-    page_size = prm.to_i
-    page_size = self.max_page_size if page_size > self.max_page_size
-    page_size = self.default_page_size if page_size < 1
-    page_size
+  def self.list_order
+    'resource_pictures.updated_at desc'
   end
   
-  def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(self.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:order => 'updated_at desc'})
-    ResourcePicture.find(:all, opt)
+  def self.list_where
+    ''
   end
   
   def self.list_opt
-    {:include => {:license => {}, :artist => {}, :picture => {}} }
+    {:license => {}, :artist => {}, :picture => {} }
   end
   
   def self.list_json_opt
     {:include => {:license => {}, :artist => {}, :picture => {}} }
   end
   
-  def self.mylist ar, page = 1, page_size = Author.default_resource_picture_page_size
-    opt = {}
-    opt.merge!(ResourcePicture.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['resource_pictures.artist_id = ?', ar.id], :order => 'resource_pictures.updated_at desc'})
-    ResourcePicture.find(:all, opt)
-  end
-  
-  def self.show rid, au
-    opt = {}
-    opt.merge!(self.show_opt)
-    r = ResourcePicture.find(rid, opt)
-    raise ActiveRecord::Forbidden unless r.visible?(au)
-    r
-  end
-  
   def self.show_opt
     {:include => {:license => {}, :artist => {}, :picture => {}} }
   end
@@ -139,18 +102,11 @@ class ResourcePicture < ActiveRecord::Base
     {:include => {:license => {}, :artist => {}, :picture => {}} }
   end
   
-  def self.edit rid, ar
-    opt = {}
-    opt.merge!(self.show_opt)
-    r = ResourcePicture.find(rid, opt)
-    raise ActiveRecord::Forbidden unless r.own?(ar)
-    r
-  end
-  
   def new_picture imager
     pc = Picture.new
     pc.supply_default
     pc.overwrite self
+    pc.boosts 'post'
     r = pc.store imager
     return pc if r
     self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
@@ -162,6 +118,9 @@ class ResourcePicture < ActiveRecord::Base
     res = false
     self.overwrite self.original_picture
     ResourcePicture.transaction do
+      self.original_picture.published_at = Time.now
+      self.original_picture.stopped_at = nil
+      raise ActiveRecord::Rollback unless self.original_picture.save
       pc = self.new_picture imager
       if pc
         self.picture_id = pc.id
@@ -196,7 +155,7 @@ class ResourcePicture < ActiveRecord::Base
   
   def store_picture(imager, fn)
     res = false
-    thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager
+    thumbnail_imager = self.license_extend.thumbnail >= 0 ? imager.to_thumbnail : imager
     return false unless thumbnail_imager
     begin
       PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
@@ -215,6 +174,9 @@ class ResourcePicture < ActiveRecord::Base
   def unpublish
     res = false
     ResourcePicture.transaction do
+      self.original_picture.published_at = nil
+      self.original_picture.stopped_at = Time.now
+      raise ActiveRecord::Rollback unless self.original_picture.save
       begin
         PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
         PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
@@ -237,86 +199,11 @@ class ResourcePicture < ActiveRecord::Base
   end
   
   def credit_template
-    "#{self.classname.tableize}/attributes/credit"
+    "#{self.license_group_classname.tableize}/attributes/credit"
   end
   
   def full_credit_template
-    "#{self.classname.tableize}/attributes/full_credit"
-  end
-  
-  def credit_data
-    begin
-      @credit_data = JSON.parse(self.credit) unless @credit_data
-    rescue 
-    end
-    @credit_data
-  end
-  
-  def flags
-    begin
-      @flags = JSON.parse(self.settings) unless @flags
-    rescue 
-    end
-    @flags
-  end
-  
-  def flags=(s)
-    @flags = s
-  end
-  
-  def flag_open
-    @flag_open = flags["open"] unless @flag_open
-    @flag_open
-  end
-  
-  def flag_commercial
-    @flag_commercial = flags["commercial"] unless @flag_commercial
-    @flag_commercial
-  end
-  
-  def flag_official
-    @flag_official = flags["official"] unless @flag_official
-    @flag_official
-  end
-  
-  def flag_attribution
-    @flag_attribution = flags["attribution"] unless @flag_attribution
-    @flag_attribution
-  end
-  
-  def flag_derive
-    @flag_derive = flags["derive"] unless @flag_derive
-    @flag_derive
-  end
-  
-  def flag_thumbnail
-    @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
-    @flag_thumbnail
-  end
-  
-  def flag_gif_convert
-    @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
-    @flag_gif_convert
-  end
-  
-  def flag_reverse
-    @flag_reverse = flags["reverse"] unless @flag_reverse
-    @flag_reverse
-  end
-  
-  def flag_resize
-    @flag_resize = flags["resize"] unless @flag_resize
-    @flag_resize
-  end
-  
-  def flag_sync_vh
-    @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
-    @flag_sync_vh
-  end
-  
-  def flag_overlap
-    @flag_overlap = flags["overlap"] unless @flag_overlap
-    @flag_overlap
+    "#{self.license_group_classname.tableize}/attributes/full_credit"
   end
   
 end