OSDN Git Service

fix: fetch fail
[pettanr/pettanr.git] / app / models / comic_story.rb
1 class ComicStory < Peta::Leaf
2   load_manifest
3   belongs_to :author
4   belongs_to :comic
5   belongs_to :story
6   
7   validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
8   validates :story_id, :presence => true, :numericality => true, :existence => {:both => false}
9   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
10   
11   scope :with_comic, -> do
12     includes(:comic)
13   end
14   
15   scope :find_index, -> do
16     with_comic.where(Comic.arel_table[:visible].gt 0).references(:comic)
17   end
18   
19   scope :find_private, -> (operators) do 
20     with_comic.where(Comic.arel_table[:author_id].eq operators.author.id).references(:comic)
21   end
22   
23   scope :by_comic, -> (comic_id) do 
24     where(comic_id: comic_id)
25   end
26   
27   scope :find_by_comic, -> (comic_id) do 
28     find_index.by_comic(comic_id)
29   end
30   
31   scope :find_by_story, -> (story_id) do 
32     find_index.where(story_id: story_id).references(:comic)
33   end
34   
35   scope :find_by_author, -> (author_id) do 
36     find_index.where(Comic.arel_table[:author_id].eq author_id).references(:comic)
37   end
38   
39   scope :find_play, -> (comic_id) do 
40     find_by_comic(comic_id)
41   end
42   
43   scope :find_private_play, -> (comic_id, operators) do 
44    find_private(operators).by_comic(comic_id)
45   end
46   
47   def supply_default
48     self.comic_id = nil
49     self.story_id = nil
50     self.t = nil
51   end
52   
53   def overwrite operators
54   end
55   
56   def disp_t
57     self.t + 1
58   end
59   
60   def disp_t_by_text
61     I18n.t('comic_stories.show.t', :t => self.disp_t)
62   end
63   
64   def title
65     self.disp_t_by_text + ':' + self.story.title
66   end
67   
68   def self.public_list_where list
69     'comics.visible > 0'
70   end
71   
72   def self.by_author_list_includes
73     {
74       :comic => {
75         :author => {}
76       }
77     }
78   end
79   
80   def self.show_opt
81     {:include => {
82       :comic => {
83         :author => {}
84       }
85     }}
86   end
87   
88   def allow? operators
89     return nil if self.story_id == nil or self.comic_id == nil
90     self.comic.own?(operators) and self.story.own?(operators)
91   end
92   
93 end