OSDN Git Service

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