OSDN Git Service

fix: fetch fail
[pettanr/pettanr.git] / app / models / comic.rb
index 7802ba5..269f07b 100644 (file)
 #コミック
-class Comic < ActiveRecord::Base
-  has_many :stories
+class Comic < Peta::Binder
+  load_manifest
+  has_many :comic_stories
   belongs_to :author
   
   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}
   
-  def supply_default
-    self.visible = 0 if self.visible.blank?
-  end
-  
-  def overwrite au
-    return false unless au
-    self.author_id = au.id
-  end
-  
-  def own? au
-    return false unless au.is_a?(Author)
-    self.author_id == au.id
-  end
-  
-  def visible? au
-    if au == nil
-      return false if MagicNumber['run_mode'] == 1
-    elsif au.is_a?(Author)
-      return true if self.own?(au)
-    else
-      return false
-    end
-    self.visible > 0
-  end
-  
-  def disp_visible
-    visible == 1 ? 'O' : 'X'
-  end
-  
-  def self.default_page_size
-    25
+  scope :find_index, -> do
+    where(arel_table[:visible].gt 0)
   end
   
-  def self.max_page_size
-    100
+  scope :find_private, -> (operators) do 
+    where(author_id: operators.author.id)
   end
   
-  def self.default_panel_size
-    30
+  scope :find_by_author, -> (author_id) do 
+    find_index.where(author_id: author_id)
   end
   
-  def self.max_panel_size
-    200
+  scope :with_stories, -> do
+    includes(comic_stories: :story)
   end
   
-  def self.page prm = nil
-    page = prm.to_i
-    page = 1 if page < 1
-    page
+  scope :find_by_story, -> (story_id) do 
+    with_stories.find_index.where(Story.arel_table[:id].eq story_id).references(:story)
   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 of find_play
+  def self.find_play(id)
+    ComicStory.find_play(id)
   end
   
-  def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(Comic.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['visible > 0'], :order => 'updated_at desc'})
-    Comic.find(:all, opt)
+  # scope of find_private_play
+  def self.find_private_play(id, operators)
+    ComicStory.find_private_play(id, operators)
   end
   
-  def self.list_opt
-    {:include => {:stories => {:panel => {}}, :author => {}}}
+  def supply_default
+    self.visible = 0 if self.visible.blank?
+    self.author_id = nil
   end
   
-  def self.list_json_opt
-    {:include => {:stories => {:include => {:panel => {}}}, :author => {}}}
+  def overwrite operators
+    return false unless operators.author
+    self.author_id = operators.author.id
   end
   
-  def self.mylist au, page = 1, page_size = Author.default_comic_page_size
-    opt = {}
-    opt.merge!(Comic.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['comics.author_id = ?', au.id], :order => 'comics.updated_at desc'})
-    Comic.find(:all, opt)
+  def visible? operators
+    case super
+    when nil # super return
+      return true
+    when false
+      return false
+    else
+      self.visible > 0
+    end
   end
   
-  def self.show cid, au
-    opt = {}
-    opt.merge!(Comic.show_opt)
-    res = Comic.find(cid, opt)
-    raise ActiveRecord::Forbidden unless res.visible?(au)
-    res
+  def symbol_filename
   end
   
-  def self.edit cid, au
-    opt = {}
-    opt.merge!(Comic.show_opt)
-    res = Comic.find(cid, opt)
-    raise ActiveRecord::Forbidden unless res.own?(au)
-    res
+  def self.public_list_where list
+    'comics.visible > 0'
   end
   
   def self.show_opt
-    {:include => {:stories => {:panel => {}}, :author => {}}}
+    {:include => {:comic_stories => {:story => {}}, :author => {}}}
   end
   
-  def self.show_json_opt
-    {:include => {:stories => {:include => {:panel => {}}}, :author => {}}}
+  def scenario
+    panels.map {|panel|
+      panel.scenario
+    }.join
   end
   
-  def self.visible_count
-    Comic.count 'visible > 0'
+  def plain_scenario
+    panels.map {|panel|
+      panel.plain_scenario
+    }.join
   end
   
 end