OSDN Git Service

fix form
[pettanr/pettanr.git] / app / models / story.rb
index 46ce75e..a3894f2 100644 (file)
 #ストーリー
-class Story < ActiveRecord::Base
-  belongs_to :author
-  belongs_to :panel
+class Story < Peta::Content
+  has_many :story_sheets
   belongs_to :comic
   
-  validates :comic_id, :presence => true, :numericality => true, :existence => true
-  validates :panel_id, :presence => true, :numericality => true, :existence => true
-  validates :author_id, :presence => true, :numericality => true, :existence => true
+  validates :comic_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}
   
+  def self.owner_model
+    Comic
+  end
+  
+  def self.valid_encode_columns
+    super + ['title', 'description']
+  end
+  
   def supply_default
+    self.comic_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 visible? operators
+    return false unless super
+    self.owner_model.visible? operators
   end
   
-  def own? author
-    self.author_id == author.id
+  def disp_t
+    self.t + 1
   end
   
-  def self.default_panel_size
-    30
+  def disp_t_by_text
+    I18n.t('stories.show.t', :t => self.disp_t)
   end
   
-  def self.max_panel_size
-    200
+  def title_with_t
+    self.disp_t_by_text  + ':' + self.title
   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
+  def story_sheets_count
+    StorySheet.where(['story_sheets.story_id = ?', self.id]).count
   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 self.list_where
+    'stories.visible > 0'
   end
   
-  def self.play_list comic, author, offset = 0, limit = Story.default_panel_size
-    opt = {}
-    opt.merge!(Story.list_opt)
-    opt.merge!({:offset => offset, :limit => limit}) if limit > 0
-    opt.merge!({:conditions => ['stories.comic_id = ?', comic.id], :order => 'stories.t'})
-    Story.find(:all, opt)
+  def self.list_order
+    'stories.updated_at desc'
   end
   
   def self.list_opt
-    {:include => {
-      :author => {}, 
-      :comic => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloons => {}, :speeches => {}}
-      }
-    }}
+    {:comic => {:author => {}} }
   end
   
   def self.list_json_opt
-    {:include => {
-      :author => {}, 
-      :comic => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloons => {}, :speeches => {}}
-      }
-    }}
-  end
-  
-  def self.mylist au, page = 1, page_size = Author.default_story_page_size
-    opt = {}
-    opt.merge!(Story.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['stories.author_id = ?', au.id], :order => 'stories.updated_at desc'})
-    Story.find(:all, opt)
-  end
-  
-  def self.edit sid, au
-    opt = {}
-    opt.merge!(Story.show_opt)
-    res = Story.find sid, opt
-    raise ActiveRecord::Forbidden unless res.own?(au)
-    res
+    {:include => {:comic => {:include => {:author => {}}} }}
   end
   
   def self.show_opt
-    {:include => {
-      :author => {}, 
-      :comic => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloons => {}, :speeches => {}}
-      }
-    }}
-  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 => {}}}
+    {:include => {:comic => {:author => {}} }}
+  end
+  
+  def self.show_json_opt
+    {:include => {:comic => {:include => {:author => {}}} }}
+  end
+  
+  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
-    self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
+    res
   end
   
   def self.new_t comic_id
@@ -194,17 +156,21 @@ class Story < ActiveRecord::Base
     end
   end
   
-  def allow?
-    c = self.comic
-    pl = self.panel
-    c and c.own?(self.author) and pl and pl.usable?(self.author)
+  def allow? operators
+    return nil if self.comic_id == nil
+    self.comic.own?(operators)
   end
   
-  def store old_t = nil
+  def store operators, old_t = nil
     res = false
-    raise ActiveRecord::Forbidden unless self.allow?
     Story.transaction do
-      self.rotate old_t
+      case self.allow? operators
+      when true
+        self.rotate old_t
+      when false
+        raise ActiveRecord::Forbidden
+      else
+      end
       res = self.save
       raise ActiveRecord::Rollback unless res
       res = Story.validate_t(self) 
@@ -217,10 +183,13 @@ class Story < ActiveRecord::Base
   end
   
   def destroy_and_shorten
+    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
   end