OSDN Git Service

rails upgrade to 4
[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 :find_play, -> (id) do 
33     with_panels.find_index.where(id: id).references(:scroll_panels)
34   end
35   
36   scope :find_private_play, -> (id, operators) do 
37     with_panels.find_private(operators).where(self.arel_table[:id].eq id).references(:scroll_panels)
38   end
39   
40   def supply_default
41     self.visible = 0 if self.visible.blank?
42     self.author_id = nil
43   end
44   
45   def overwrite operators
46     return false unless operators.author
47     self.author_id = operators.author.id
48     super()
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 self.public_list_where list
63     'scrolls.visible > 0'
64   end
65   
66   def self.show_opt
67     {:include => {:scroll_panels => {:panel => {}}, :author => {}}}
68   end
69   
70   def scenario
71     panels.map {|panel|
72       panel.scenario
73     }.join
74   end
75   
76   def plain_scenario
77     panels.map {|panel|
78       panel.plain_scenario
79     }.join
80   end
81   
82 end