OSDN Git Service

t#32046:
[pettanr/pettanr.git] / app / models / story.rb
index 7f646f8..b655b67 100644 (file)
@@ -1,3 +1,269 @@
 #ストーリー
 class Story < ActiveRecord::Base
+  belongs_to :author
+  has_many :story_sheets
+  belongs_to :comic
+  
+  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 :author_id, :presence => true, :numericality => true, :existence => {:both => false}
+  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.visible = 0 if self.visible.blank?
+    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.own?(roles)
+    self.visible > 0
+  end
+  
+  def self.default_page_size
+    25
+  end
+  
+  def self.max_page_size
+    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
+    page
+  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
+  end
+  
+  def self.list_where
+    'stories.visible > 0'
+  end
+  
+  def self.mylist_where au
+    ['stories.author_id = ?', au.id]
+  end
+  
+  def self.himlist_where au
+    ['stories.author_id = ? and stories.visible > 0', au.id]
+  end
+  
+  def self.list page = 1, page_size = self.default_page_size
+    Story.where(self.list_where()).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.mylist au, page = 1, page_size = Author.default_story_page_size
+    Story.where(self.mylist_where(au)).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.himlist au, page = 1, page_size = Author.default_story_page_size
+    Story.where(self.himlist_where(au)).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.list_paginate page = 1, page_size = self.default_page_size
+    Kaminari.paginate_array(Array.new(Story.where(self.list_where()).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.mylist_paginate au, page = 1, page_size = Author.default_story_page_size
+    Kaminari.paginate_array(Array.new(Story.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.himlist_paginate au, page = 1, page_size = Author.default_story_page_size
+    Kaminari.paginate_array(Array.new(Story.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.list_opt
+    {:author => {}, :story_sheets => {:sheet => {}, :author => {}} }
+  end
+  
+  def self.list_json_opt
+    {:include => {:author => {}, :story_sheets => {:include => {:sheet => {}, :author => {}}} }}
+  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
+  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
+  end
+  
+  def self.show_opt
+    {:include => {:author => {}, :story_sheets => {:sheet => {}, :author => {}} }}
+  end
+  
+  def self.show_json_opt
+    {:include => {:author => {}, :story_sheets => {:include => {:sheet => {}, :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])
+  end
+  
+  def update_shift old_t
+    if self.t > old_t
+      higher_shift old_t
+    else
+      lesser_shift old_t
+    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
+  end
+  
+  def allow?
+    return nil if self.comic_id == nil
+    self.comic.own?(self.author)
+  end
+  
+  def store old_t = nil
+    res = false
+    Story.transaction do
+      case self.allow?
+      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
+  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
+  end
+  
+  
 end