OSDN Git Service

v07
[pettanr/pettanr.git] / app / models / story.rb
index 10b1a9e..cd02e7f 100644 (file)
 #ストーリー
-class Story < ActiveRecord::Base
+class Story < Peta::Binder
+  load_manifest
+  has_many :comic_stories
+  has_many :story_sheets
   belongs_to :author
-  belongs_to :panel
-  belongs_to :comic
   
-  validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
-  validates :panel_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 :author_id, :presence => true, :numericality => true, :existence => {:both => false}
-  validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
   
-  def supply_default
-    self.comic_id = nil
-    self.panel_id = nil
-    self.t = nil
-  end
-  
-  def overwrite au
-    return false unless au
-    self.author_id = au.id
-  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
-  end
-  
-  def visible? roles
-    if MagicNumber['run_mode'] == 0
-      return false unless guest_role_check(roles)
-    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
-  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
-  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)
-  end
-  
-  def self.list_opt
-    {:include => {
-      :author => {}, 
-      :comic => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloons => {}, :speeches => {}}
-      }
-    }}
-  end
-  
-  def self.list_json_opt
-    {:include => {
-      :author => {}, 
-      :comic => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloons => {}, :speeches => {}}
-      }
-    }}
-  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
+  scope :find_index, -> do
+    where(arel_table[:visible].gt 0)
   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
+  scope :find_private, -> (operators) do 
+    where(author_id: operators.author.id)
   end
   
-  def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(self.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['comics.visible > 0'], :order => 'stories.updated_at desc'})
-    Story.find(:all, opt)
+  scope :find_by_author, -> (author_id) do 
+    find_index.where(author_id: author_id)
   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)
+  scope :with_comics, -> do
+    includes(comic_stories: :comic)
   end
   
-  def self.show sid, roles
-    opt = {}
-    opt.merge!(Story.show_opt)
-    res = Story.find sid, opt
-    raise ActiveRecord::Forbidden unless res.visible?(roles)
-    res
+  scope :find_by_comic, -> (comic_id) do 
+    with_comics.find_index.where(Comic.arel_table[:id].eq comic_id).references(:comic)
   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
+  scope :with_sheets, -> do
+    includes(story_sheets: :sheet)
   end
   
-  def self.show_opt
-    {:include => {
-      :author => {}, 
-      :comic => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloons => {}, :speeches => {}}
-      }
-    }}
+  scope :find_by_sheet, -> (sheet_id) do 
+    with_sheets.find_index.where(Sheet.arel_table[:id].eq sheet_id).references(:sheet)
   end
   
-  def elements
-    self.panel.elements
+  # scope of find_play
+  def self.find_play(id)
+    StorySheet.find_play(id)
   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}})
+  # scope of find_private_play
+  def self.find_private_play(id, operators)
+    StorySheet.find_private_play(id, operators)
   end
   
-  def self.list_as_json_text ary, au
-    '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
-  end
-  
-  def self.licensed_pictures stories
-    r = {}
-    stories.each do |story|
-      r.merge!(story.panel.licensed_pictures) if story.panel
-    end
-    r
-  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])
+  def supply_default
+    self.visible = 0 if self.visible.blank?
+    self.author_id = nil
   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 overwrite operators
+    return false unless operators.author
+    self.author_id = operators.author.id
+    super()
   end
   
-  def update_shift old_t
-    if self.t > old_t
-      higher_shift old_t
+  def visible? operators
+    case super
+    when nil # super return
+      return true
+    when false
+      return false
     else
-      lesser_shift old_t
+      self.visible > 0
     end
   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
-    else
-      if self.t.blank?
-      else
-        self.update_shift old_t
-      end
-    end
+  def self.public_list_where list
+    'stories.visible > 0'
   end
   
-  def allow?
-    c = self.comic
-    pl = self.panel
-    c and c.own?(self.author) and pl and pl.usable?(self.author)
-  end
-  
-  def store old_t = nil
-    res = false
-    raise ActiveRecord::Forbidden unless self.allow?
-    Story.transaction do
-      self.rotate old_t
-      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
-  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
-      res = true
-    end
-    res
+  def self.show_opt
+    {:include => {:story_sheets => {:sheet => {}}, :author => {}}}
   end
   
-  
 end