OSDN Git Service

fix: fetch fail
[pettanr/pettanr.git] / app / models / comic.rb
1 #コミック
2 class Comic < Peta::Binder
3   load_manifest
4   has_many :comic_stories
5   belongs_to :author
6   
7   validates :title, :presence => true, :length => {:maximum => 100}
8   validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
9   validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
10   
11   scope :find_index, -> do
12     where(arel_table[:visible].gt 0)
13   end
14   
15   scope :find_private, -> (operators) do 
16     where(author_id: operators.author.id)
17   end
18   
19   scope :find_by_author, -> (author_id) do 
20     find_index.where(author_id: author_id)
21   end
22   
23   scope :with_stories, -> do
24     includes(comic_stories: :story)
25   end
26   
27   scope :find_by_story, -> (story_id) do 
28     with_stories.find_index.where(Story.arel_table[:id].eq story_id).references(:story)
29   end
30   
31   # scope of find_play
32   def self.find_play(id)
33     ComicStory.find_play(id)
34   end
35   
36   # scope of find_private_play
37   def self.find_private_play(id, operators)
38     ComicStory.find_private_play(id, operators)
39   end
40   
41   def supply_default
42     self.visible = 0 if self.visible.blank?
43     self.author_id = nil
44   end
45   
46   def overwrite operators
47     return false unless operators.author
48     self.author_id = operators.author.id
49   end
50   
51   def visible? operators
52     case super
53     when nil # super return
54       return true
55     when false
56       return false
57     else
58       self.visible > 0
59     end
60   end
61   
62   def symbol_filename
63   end
64   
65   def self.public_list_where list
66     'comics.visible > 0'
67   end
68   
69   def self.show_opt
70     {:include => {:comic_stories => {:story => {}}, :author => {}}}
71   end
72   
73   def scenario
74     panels.map {|panel|
75       panel.scenario
76     }.join
77   end
78   
79   def plain_scenario
80     panels.map {|panel|
81       panel.plain_scenario
82     }.join
83   end
84   
85 end