OSDN Git Service

fix: fetch fail
[pettanr/pettanr.git] / app / models / scroll.rb
1 class Scroll < Peta::Binder
2   load_manifest
3   has_many :scroll_panels
4   belongs_to :author
5   
6   has_many :panels, :through => :scroll_panels
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_panels, -> do
25     includes(scroll_panels: :panel)
26   end
27   
28   scope :find_by_panel, -> (panel_id) do 
29     with_panels.find_index.where(Panel.arel_table[:id].eq panel_id).references(:panel)
30   end
31   
32   # scope of find_play
33   def self.find_play(id)
34     ScrollPanel.find_play(id)
35   end
36   
37   # scope of find_private_play
38   def self.find_private_play(id, operators)
39     ScrollPanel.find_private_play(id, operators)
40   end
41   
42   def supply_default
43     self.visible = 0 if self.visible.blank?
44     self.author_id = nil
45   end
46   
47   def overwrite operators
48     return false unless operators.author
49     self.author_id = operators.author.id
50     super()
51   end
52   
53   def visible? operators
54     case super
55     when nil # super return
56       return true
57     when false
58       return false
59     else
60       self.visible > 0
61     end
62   end
63   
64   def self.public_list_where list
65     'scrolls.visible > 0'
66   end
67   
68   def self.show_opt
69     {:include => {:scroll_panels => {:panel => {}}, :author => {}}}
70   end
71   
72   def scenario
73     panels.map {|panel|
74       panel.scenario
75     }.join
76   end
77   
78   def plain_scenario
79     panels.map {|panel|
80       panel.plain_scenario
81     }.join
82   end
83   
84 end