OSDN Git Service

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