OSDN Git Service

merge v04
[pettanr/pettanr.git] / app / models / panel.rb
index 7f0c878..56cd5b5 100644 (file)
@@ -1,49 +1,31 @@
 #コマ
 class Panel < ActiveRecord::Base
-  belongs_to :comic
   belongs_to :author
   belongs_to :resource_picture
 #  belongs_to :background_picture, :class_name => 'ResourcePicture'
+  has_many :stories
   has_many :panel_pictures, :dependent => :destroy
   has_many :speech_balloons, :dependent => :destroy
   accepts_nested_attributes_for :panel_pictures, :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 :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
+  def supply_default
+    self.border = 2
   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])
+  def overwrite au
+    return false unless au
+    self.author_id = au.id
   end
   
-  def self.find_t comic_id, t
-    Panel.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
-  end
-
   def self.collect_element_value elements, name, ex_nil = false
     e = elements.map {|e|
       e.map {|o|
@@ -112,35 +94,13 @@ class Panel < ActiveRecord::Base
   def store
     res = false
     Panel.transaction do
-      raise ActiveRecord::Rollback unless validate_child
-      f = nil
-      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 = Panel.new_t self.comic_id
+      unless validate_child
+        self.errors.add :base , 'invalid time'
+        raise ActiveRecord::Rollback
       end
       res = self.save
     end
-  end
-  
-  def move_to new_t
-    return true if self.t == new_t
-    if self.t > new_t
-      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.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
-    self.t = new_t
-    self.save
-  end
-  
-  def destroy_and_shorten
-    Panel.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
-    self.destroy
+    res
   end
   
   def self.default_page_size
@@ -174,13 +134,12 @@ class Panel < ActiveRecord::Base
   
   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})
+    opt.merge!({:conditions => 'panels.publish > 0', :order => 'panels.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 => {}}
       }, 
@@ -191,7 +150,6 @@ class Panel < ActiveRecord::Base
   
   def self.list_json_opt
     {:include => {
-      :comic => {:author => {}}, 
       :panel_pictures => {
         :resource_picture => {:artist => {}, :license => {}}
       }, 
@@ -206,9 +164,14 @@ class Panel < ActiveRecord::Base
     r
   end
   
+  def self.edit rid, au, opt = {}
+    r = Panel.find(rid, :include => self.show_include_opt(opt))
+    raise ActiveRecord::Forbidden unless r.own?(au)
+    r
+  end
+  
   def self.show_include_opt opt = {}
     res = {
-      :comic => :author, 
       :panel_pictures => {
         :resource_picture => {:artist => {}, :license => {}}
       }, 
@@ -221,7 +184,6 @@ class Panel < ActiveRecord::Base
   
   def self.show_json_include_opt
     {:include => {
-      :comic => {:author => {}}, 
       :panel_pictures => {
         :resource_picture => {:artist => {}, :license => {}}
       }, 
@@ -232,8 +194,7 @@ class Panel < ActiveRecord::Base
   
   def visible? au
     return false unless au
-    return false unless self.comic
-    self.comic.visible?(au) or self.author_id == au.id
+    self.own?(au) or self.publish?
   end
   
   def own? author
@@ -241,6 +202,14 @@ class Panel < ActiveRecord::Base
     self.author_id == author.id
   end
   
+  def usable? au
+    visible? au
+  end
+  
+  def publish?
+    self.publish > 0
+  end
+  
   def sort_by_time
     pe = []
     self.panel_pictures.each do |picture|