OSDN Git Service

merge v06sheet
[pettanr/pettanr.git] / app / models / story.rb
index cd67cc3..7fadd62 100644 (file)
@@ -1,30 +1,35 @@
 #ストーリー
 class Story < ActiveRecord::Base
-  belongs_to :author
-  belongs_to :panel
+  has_many :story_sheets
   belongs_to :comic
   
   validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
-  validates :panel_id, :presence => true, :numericality => true, :existence => {:both => false}
-  validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
+  validates :title, :presence => true, :length => {:maximum => 100}
+  validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
+  before_validation :valid_encode
+  
+  def valid_encode
+    ['title', 'description'].each do |a|
+      next if attributes[a] == nil
+      raise Pettanr::BadRequest unless attributes[a].valid_encoding?
+    end
+  end
   
   def supply_default
     self.comic_id = nil
-    self.panel_id = nil
+    self.visible = 0 if self.visible.blank?
     self.t = nil
   end
   
-  def overwrite au
-    return false unless au
-    self.author_id = au.id
+  def overwrite
   end
   
   def own? roles
     roles = [roles] unless roles.respond_to?(:each)
     au = Story.get_author_from_roles roles
     return false unless au
-    self.author_id == au.id
+    self.comic.author_id == au.id
   end
   
   def visible? roles
@@ -33,31 +38,12 @@ class Story < ActiveRecord::Base
     else
       return false unless reader_role_check(roles)
     end
-    return true if self.comic.own?(roles)
-    self.comic.visible? roles
-  end
-  
-  def self.default_panel_size
-    30
+    return true if self.own?(roles)
+    self.visible > 0
   end
   
-  def self.max_panel_size
-    200
-  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.panel_count cnt, prm = self.default_panel_size
-    count = prm.to_i
-    count = self.max_panel_size if count > self.max_panel_size
-    count = self.default_panel_size if count < 1
-    count
+  def disp_t
+    self.t + 1
   end
   
   def self.default_page_size
@@ -68,6 +54,14 @@ class Story < ActiveRecord::Base
     100
   end
   
+  def self.default_panel_size
+    30
+  end
+  
+  def self.max_panel_size
+    200
+  end
+  
   def self.page prm = nil
     page = prm.to_i
     page = 1 if page < 1
@@ -81,24 +75,16 @@ class Story < ActiveRecord::Base
     page_size
   end
   
-  def self.play_list_where cid
-    ['stories.comic_id = ?', cid]
-  end
-  
   def self.list_where
-    'comics.visible > 0'
+    'stories.visible > 0'
   end
   
   def self.mylist_where au
-    ['stories.author_id = ?', au.id]
+    ['comics.author_id = ?', au.id]
   end
   
   def self.himlist_where au
-    ['stories.author_id = ? and comics.visible > 0', au.id]
-  end
-  
-  def self.play_list comic, author, offset = 0, limit = Story.default_panel_size
-    Story.where(self.play_list_where(comic.id)).includes(Story.list_opt).order('stories.t').offset(offset).limit(limit)
+    ['comics.author_id = ? and stories.visible > 0', au.id]
   end
   
   def self.list page = 1, page_size = self.default_page_size
@@ -126,37 +112,17 @@ class Story < ActiveRecord::Base
   end
   
   def self.list_opt
-    {
-      :author => {}, 
-      :comic => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloon => {}, :speech => {}}
-      }
-    }
+    {:comic => {:author => {}} }
   end
   
   def self.list_json_opt
-    {:include => {
-      :author => {}, 
-      :comic => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloon => {}, :speech => {}}
-      }
-    }}
+    {:include => {:comic => {:include => {:author => {}}} }}
   end
   
   def self.show sid, roles
     opt = {}
     opt.merge!(Story.show_opt)
-    res = Story.find sid, opt
+    res = Story.find(sid, opt)
     raise ActiveRecord::Forbidden unless res.visible?(roles)
     res
   end
@@ -164,48 +130,33 @@ class Story < ActiveRecord::Base
   def self.edit sid, au
     opt = {}
     opt.merge!(Story.show_opt)
-    res = Story.find sid, opt
+    res = Story.find(sid, opt)
     raise ActiveRecord::Forbidden unless res.own?(au)
     res
   end
   
   def self.show_opt
-    {:include => {
-      :author => {}, 
-      :comic => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloon => {}, :speech => {}}
-      }
-    }}
-  end
-  
-  def elements
-    self.panel.elements
-  end
-  
-  def story_as_json au
-    panel_include = if self.panel and self.panel.visible?(au)
-      {:include => {:author => {}}, :methods => :elements}
-    else
-      {:include => {:author => {}}}
-    end
-    self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
+    {:include => {:comic => {:author => {}} }}
   end
   
-  def self.list_as_json_text ary, au
-    '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
+  def self.show_json_opt
+    {:include => {:comic => {:include => {:author => {}}} }}
   end
   
-  def self.licensed_pictures stories
-    r = {}
-    stories.each do |story|
-      r.merge!(story.panel.licensed_pictures) if story.panel
+  def self.visible_count
+    Story.count 'visible > 0'
+  end
+  
+  def destroy_with_story_sheet
+    res = false
+    Story.transaction do
+      self.story_sheets.each do |story_sheet|
+        raise ActiveRecord::Rollback unless story_sheet.destroy
+      end
+      raise ActiveRecord::Rollback unless self.destroy
+      res = true
     end
-    r
+    res
   end
   
   def self.new_t comic_id
@@ -278,15 +229,15 @@ class Story < ActiveRecord::Base
     end
   end
   
-  def allow?
-    return nil if self.comic_id == nil or self.panel_id == nil
-    self.comic.own?(self.author) and self.panel.usable?(self.author)
+  def allow? au
+    return nil if self.comic_id == nil
+    self.comic.own?(au)
   end
   
-  def store old_t = nil
+  def store au, old_t = nil
     res = false
     Story.transaction do
-      case self.allow?
+      case self.allow? au
       when true
         self.rotate old_t
       when false
@@ -308,7 +259,7 @@ class Story < ActiveRecord::Base
     res = false
     Story.transaction do
       Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
-      raise ActiveRecord::Rollback unless self.destroy
+      raise ActiveRecord::Rollback unless self.destroy_with_story_sheet
       res = true
     end
     res