OSDN Git Service

fix panel test
[pettanr/pettanr.git] / app / models / panel.rb
index 888d7b5..0254dd4 100644 (file)
@@ -1,13 +1,43 @@
 class Panel < ActiveRecord::Base
   belongs_to :comic
   belongs_to :author
+  belongs_to :resource_picture
+#  belongs_to :background_picture, :class_name => 'ResourcePicture'
   has_many :panel_pictures, :dependent => :destroy
-  has_many :balloons, :dependent => :destroy
+  has_many :speech_balloons, :dependent => :destroy
   accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
-  accepts_nested_attributes_for :balloons, :allow_destroy => true
+  accepts_nested_attributes_for :speech_balloons, :allow_destroy => true
 
+#  validates :comic_id, :presence => true, :numericality => true, :existence => true, :uniqueness => {:scope => :t} 
+  validates :resource_picture_id, :numericality => {:allow_blank => true}
+  validates :width, :presence => true, :numericality => true, :natural_number => true
+  validates :height, :presence => true, :numericality => true, :natural_number => true
+  validates :border, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
+  validates :x, :numericality => {:allow_blank => true}
+  validates :y, :numericality => {:allow_blank => true}
+  validates :z, :numericality => {:allow_blank => true, :greater_than => 0}
+  validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
+  validates :author_id, :presence => true, :numericality => true, :existence => true
+  
+  def supply_default au
+    return false unless au
+    c = self.comic_id ? Comic.show(self.comic_id, au) : nil
+    if c
+      self.width = c.width if self.width.blank?
+      self.height = c.height if self.height.blank?
+      self.t = Panel.new_t(c.id) if self.t.blank? and self.new_record?
+    end
+    self.border = 0 if self.border.blank?
+    self.author_id = au.id
+  end
+  
+  def self.new_t comic_id
+    r = Panel.max_t(comic_id)
+    r.blank? ? 0 : r.to_i + 1
+  end
+  
   def self.max_t comic_id
-    Panel.maximum(:t, :conditions => ['comic_id = ?', comic_id]).to_i
+    Panel.maximum(:t, :conditions => ['comic_id = ?', comic_id])
   end
   
   def self.find_t comic_id, t
@@ -16,14 +46,13 @@ class Panel < ActiveRecord::Base
 
   #更新する時にPanelIDをチェックしとかないと勝手に所属先を変えられるゾ!?
 
-  def vdt_save
+  def store
     f = nil
-    max_t = Panel.max_t self.comic_id
     f = Panel.find_t(self.comic_id, self.t) if self.t
     if f
       Panel.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
     else
-      self.t = max_t + 1
+      self.t = Panel.new_t self.comic_id
     end
     self.save
   end
@@ -34,7 +63,7 @@ class Panel < ActiveRecord::Base
       Panel.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, new_t, self.t])
     else
       nf = Panel.find_t(self.comic_id, new_t)
-      max_t = Panel.max_t self.comic_id
+      max_t = Panel.max_t.to_i self.comic_id
       new_t = max_t if new_t > max_t
       Panel.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, self.t, new_t])
     end
@@ -47,9 +76,140 @@ class Panel < ActiveRecord::Base
     self.destroy
   end
   
+  def self.default_page_size
+    25
+  end
+  
+  def self.max_page_size
+    100
+  end
+  
+  def self.page prm = nil
+    page = prm.to_i
+    page = 1 if page < 1
+    page
+  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
+  end
+  
+  def self.offset cnt, prm = nil
+    offset = prm.to_i
+    offset = cnt - 1 if offset >= cnt
+    offset = cnt - offset.abs if offset < 0
+    offset = 0 if offset < 0
+    offset
+  end
+  
+  def self.list opt = {}, page = 1, page_size = self.default_page_size
+    opt.merge!(self.list_opt) unless opt[:include]
+    opt.merge!({:order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
+    Panel.find(:all, opt)
+  end
+  
+  def self.list_opt
+    {:include => {
+      :comic => :author, 
+      :panel_pictures => {
+        :resource_picture => {:artist => {}, :license => {}}
+      }, 
+      :speech_balloons => {:balloons => {}, :speeches => {}},
+      :author => {}
+    }}
+  end
+  
+  def self.list_json_opt
+    {:include => {
+      :comic => {:author => {}}, 
+      :panel_pictures => {
+        :resource_picture => {:artist => {}, :license => {}}
+      }, 
+      :speech_balloons => {:balloons => {}, :speeches => {}},
+      :author => {}
+    }}
+  end
+  
+  def self.show rid, au, opt = {}
+    r = Panel.find(rid, :include => self.show_include_opt(opt))
+    raise ActiveRecord::Forbidden unless r.visible?(au)
+    r
+  end
+  
+  def self.show_include_opt opt = {}
+    res = {
+      :comic => :author, 
+      :panel_pictures => {
+        :resource_picture => {:artist => {}, :license => {}}
+      }, 
+      :speech_balloons => {:balloons => {}, :speeches => {}},
+      :author => {}
+    }
+    res.merge!(opt[:include]) if opt[:include]
+    res
+  end
+  
+  def self.show_json_include_opt
+    {:include => {
+      :comic => {:author => {}}, 
+      :panel_pictures => {
+        :resource_picture => {:artist => {}, :license => {}}
+      }, 
+      :speech_balloons => {:balloons => {}, :speeches => {}},
+      :author => {}
+    }}
+  end
+  
+  def visible? au
+    return false unless au
+    return false unless self.comic
+    self.comic.visible?(au) or self.author_id == au.id
+  end
+  
   def own? author
     return false unless author
     self.author_id == author.id
   end
   
+  def sort_by_time
+    pe = []
+    self.panel_pictures.each do |picture|
+      pe[picture.t] = picture
+    end
+    self.balloons.each do |balloon|
+      pe[balloon.t] = balloon
+    end
+    pe
+  end
+  
+  def each_element
+    self.sort_by_time.each do |e|
+      yield e
+    end
+  end
+  
+  def panel_elements
+    res = []
+    self.each_element do |elm|
+      if elm.kind_of?(PanelPicture)
+        res[elm.t] = elm.to_json({:include => :resource_picture})
+      end
+      if elm.kind_of?(Balloon)
+        res[elm.t] = elm.to_json({:include => :speeches})
+      end
+    end
+    res
+  end
+  
+  def to_json_play
+    self.to_json :methods => :panel_elements
+  end
+  
+  def self.visible_count
+    Panel.count
+  end
+  
 end