OSDN Git Service

rails upgrade to 4
[pettanr/pettanr.git] / app / models / scroll_panel.rb
1 class ScrollPanel < Peta::Leaf
2   load_manifest
3   belongs_to :author
4   belongs_to :panel
5   belongs_to :scroll
6   
7   validates :scroll_id, :presence => true, :numericality => true, :existence => {:both => false}
8   validates :panel_id, :presence => true, :numericality => true, :existence => {:both => false}
9   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
10   
11   scope :with_scroll, -> do
12     includes(:scroll)
13   end
14   
15   scope :find_index, -> do
16     with_scroll.where(Scroll.arel_table[:visible].gt 0).references(:scroll)
17   end
18   
19   scope :find_private, -> (operators) do 
20     with_scroll.where(Scroll.arel_table[:author_id].eq operators.author.id).references(:scroll)
21   end
22   
23   scope :find_by_scroll, -> (scroll_id) do 
24     find_index.where(scroll_id: scroll_id).references(:scroll)
25   end
26   
27   scope :find_by_panel, -> (panel_id) do 
28     find_index.where(panel_id: panel_id).references(:scroll)
29   end
30   
31   scope :find_by_author, -> (author_id) do 
32     find_index.where(Scroll.arel_table[:author_id].eq author_id).references(:scroll)
33   end
34   
35   def supply_default
36     self.scroll_id = nil
37     self.panel_id = nil
38     self.t = nil
39   end
40   
41   def overwrite operators
42   end
43   
44   def self.public_list_where list
45     'scrolls.visible > 0'
46   end
47   
48   def self.by_author_list_includes
49     [:scroll => :author]
50   end
51   
52   def self.show_opt
53     {:include => {
54       :scroll => {
55         :author => {}
56       }, 
57     }}
58   end
59   
60   def self.licensed_pictures scroll_panels
61     Panel.licensed_pictures scroll_panels.select {|sp| sp.panel }.map {|sp| sp.panel }
62   end
63   
64   def allow? operators
65     return nil if self.scroll_id == nil or self.panel_id == nil
66     self.scroll.own?(operators) and self.panel.usable?(operators)
67   end
68   
69 end