OSDN Git Service

fix leaf view
[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 :author_id, :presence => true, :numericality => true, :existence => {:both => false}
10   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
11   
12   def supply_default
13     self.scroll_id = nil
14     self.panel_id = nil
15     self.t = nil
16   end
17   
18   def overwrite operators
19     return false unless operators.author
20     self.author_id = operators.author.id
21   end
22   
23   def self.public_list_order
24     'scroll_panels.updated_at desc'
25   end
26   
27   def self.public_list_where
28     'scrolls.visible > 0'
29   end
30   
31   def self.list_opt
32     {
33       :author => {}, 
34       :scroll => {
35         :author => {}
36       }, 
37       :panel => {
38         :author => {}, 
39         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
40         :speech_balloons =>{:balloon => {}, :speech => {}}
41       }
42     }
43   end
44   
45   def self.list_json_opt
46     {:include => {
47       :author => {}, 
48       :scroll => {
49         :author => {}
50       }, 
51       :panel => {
52         :author => {}, 
53         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
54         :speech_balloons =>{:balloon => {}, :speech => {}}
55       }
56     }}
57   end
58   
59   def self.show_opt
60     {:include => {
61       :author => {}, 
62       :scroll => {
63         :author => {}
64       }, 
65       :panel => {
66         :author => {}, 
67         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
68         :speech_balloons =>{:balloon => {}, :speech => {}}
69       }
70     }}
71   end
72   
73   def self.licensed_pictures scroll_panels
74     r = {}
75     scroll_panels.each do |scroll_panel|
76       r.merge!(scroll_panel.panel.licensed_pictures) if scroll_panel.panel
77     end
78     r
79   end
80   
81   def allow? operators
82     return nil if self.scroll_id == nil or self.panel_id == nil
83     self.scroll.own?(operators) and self.panel.usable?(operators)
84   end
85   
86   def store operators, old_t = nil
87     res = false
88     self.class.transaction do
89       case self.allow? operators
90       when true
91         self.rotate old_t
92       when false
93         raise ActiveRecord::Forbidden
94       else
95       end
96       res = self.save
97       raise ActiveRecord::Rollback unless res
98       res = self.class.validate_t(self.scroll_id) 
99       unless res
100         self.errors.add :t, 'unserialized'
101         raise ActiveRecord::Rollback 
102       end
103     end
104     res
105   end
106   
107 end