OSDN Git Service

v07
[pettanr/pettanr.git] / app / models / story.rb
index efb6666..cd02e7f 100644 (file)
 #ストーリー
 class Story < Peta::Binder
   load_manifest
+  has_many :comic_stories
   has_many :story_sheets
-  belongs_to :comic
+  belongs_to :author
   
-  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}
+  validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
   
-  def supply_default
-    self.comic_id = nil
-    self.visible = 0 if self.visible.blank?
-    self.t = nil
+  scope :find_index, -> do
+    where(arel_table[:visible].gt 0)
   end
   
-  def overwrite
+  scope :find_private, -> (operators) do 
+    where(author_id: operators.author.id)
   end
   
-  def visible? operators
-    return false unless super
-    self.owner_model.visible? operators
+  scope :find_by_author, -> (author_id) do 
+    find_index.where(author_id: author_id)
   end
   
-  def disp_t
-    self.t + 1
+  scope :with_comics, -> do
+    includes(comic_stories: :comic)
   end
   
-  def disp_t_by_text
-    I18n.t('stories.show.t', :t => self.disp_t)
+  scope :find_by_comic, -> (comic_id) do 
+    with_comics.find_index.where(Comic.arel_table[:id].eq comic_id).references(:comic)
   end
   
-  def title_with_t
-    self.disp_t_by_text  + ':' + self.title
+  scope :with_sheets, -> do
+    includes(story_sheets: :sheet)
   end
   
-  def story_sheets_count
-    StorySheet.where(['story_sheets.story_id = ?', self.id]).count
+  scope :find_by_sheet, -> (sheet_id) do 
+    with_sheets.find_index.where(Sheet.arel_table[:id].eq sheet_id).references(:sheet)
   end
   
-  def self.list_where
-    'stories.visible > 0'
+  # scope of find_play
+  def self.find_play(id)
+    StorySheet.find_play(id)
   end
   
-  def self.list_order
-    'stories.updated_at desc'
-  end
-  
-  def self.list_opt
-    {:comic => {:author => {}} }
-  end
-  
-  def self.by_author_list_includes
-    {
-      :comic => {
-        :author => {}
-      }
-    }
-  end
-  
-  def self.list_json_opt
-    {:include => {:comic => {:include => {:author => {}}} }}
-  end
-  
-  def self.show_opt
-    {:include => {:comic => {:author => {}} }}
+  # scope of find_private_play
+  def self.find_private_play(id, operators)
+    StorySheet.find_private_play(id, operators)
   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
-    res
-  end
-  
-  def self.new_t comic_id
-    r = Story.max_t(comic_id)
-    r.blank? ? 0 : r.to_i + 1
-  end
-  
-  def self.max_t comic_id
-    Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
-  end
-  
-  def self.find_t comic_id, t
-    Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
-  end
-  
-  def self.collect_t story
-    r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
-    r.map {|s| s.t}
-  end
-  
-  def self.serial? ary
-    i = 0
-    ary.compact.sort.each do |t|
-      break false unless t == i
-      i += 1
-    end
-    ary.compact.size == i
-  end
-  
-  def self.validate_t story
-    Story.serial?(Story.collect_t(story))
-  end
-  
-  def insert_shift
-    Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
-  end
-  
-  def lesser_shift old_t
-    self.t = 0 if self.t < 0
-    Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
-  end
-  
-  def higher_shift old_t
-    nf = Story.find_t(self.comic_id, self.t)
-    max_t = Story.max_t(self.comic_id).to_i
-    self.t = max_t if self.t > max_t
-    Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
+  def supply_default
+    self.visible = 0 if self.visible.blank?
+    self.author_id = nil
   end
   
-  def update_shift old_t
-    if self.t > old_t
-      higher_shift old_t
-    else
-      lesser_shift old_t
-    end
+  def overwrite operators
+    return false unless operators.author
+    self.author_id = operators.author.id
+    super()
   end
   
-  def rotate old_t = nil
-    if self.new_record?
-      if self.t.blank?
-        self.t = Story.new_t self.comic_id
-      else
-        self.insert_shift
-      end
+  def visible? operators
+    case super
+    when nil # super return
+      return true
+    when false
+      return false
     else
-      if self.t.blank?
-      else
-        self.update_shift old_t
-      end
+      self.visible > 0
     end
   end
   
-  def allow? operators
-    return nil if self.comic_id == nil
-    self.comic.own?(operators)
-  end
-  
-  def store operators, old_t = nil
-    res = false
-    Story.transaction do
-      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) 
-      unless res
-        self.errors.add :t, 'unserialized'
-        raise ActiveRecord::Rollback 
-      end
-    end
-    res
+  def self.public_list_where list
+    'stories.visible > 0'
   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_with_story_sheet
-      res = true
-    end
-    res
+  def self.show_opt
+    {:include => {:story_sheets => {:sheet => {}}, :author => {}}}
   end
   
-  
 end