OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[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.list_where
28     'scrolls.visible > 0'
29   end
30   
31   def self.by_author_list_includes
32     {
33       :scroll => {
34         :author => {}
35       }
36     }
37   end
38   
39   def self.play_list_where cid
40     ['scroll_panels.scroll_id = ?', cid]
41   end
42   
43   def self.play_list scroll, author, offset = 0, limit = ScrollPanel.default_panel_size
44     ScrollPanel.where(self.play_list_where(scroll.id)).includes(ScrollPanel.list_opt).order('scroll_panels.t').offset(offset).limit(limit)
45   end
46   
47   def self.list_opt
48     {
49       :author => {}, 
50       :scroll => {
51         :author => {}
52       }, 
53       :panel => {
54         :author => {}, 
55         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
56         :speech_balloons =>{:balloon => {}, :speech => {}}
57       }
58     }
59   end
60   
61   def self.list_json_opt
62     {:include => {
63       :author => {}, 
64       :scroll => {
65         :author => {}
66       }, 
67       :panel => {
68         :author => {}, 
69         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
70         :speech_balloons =>{:balloon => {}, :speech => {}}
71       }
72     }}
73   end
74   
75   def self.show_opt
76     {:include => {
77       :author => {}, 
78       :scroll => {
79         :author => {}
80       }, 
81       :panel => {
82         :author => {}, 
83         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
84         :speech_balloons =>{:balloon => {}, :speech => {}}
85       }
86     }}
87   end
88   
89   def elements
90     self.panel.elements
91   end
92   
93   def scroll_panel_as_json au
94     panel_include = if self.panel and self.panel.visible?(au)
95       {:include => {:author => {}}, :methods => :elements}
96     else
97       {:include => {:author => {}}}
98     end
99     self.to_json({:include => {:scroll => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
100   end
101   
102   def self.list_as_json_text ary, au
103     '[' + ary.map {|i| i.scroll_panel_as_json(au) }.join(',') + ']'
104   end
105   
106   def self.licensed_pictures scroll_panels
107     r = {}
108     scroll_panels.each do |scroll_panel|
109       r.merge!(scroll_panel.panel.licensed_pictures) if scroll_panel.panel
110     end
111     r
112   end
113   
114   def allow? operators
115     return nil if self.scroll_id == nil or self.panel_id == nil
116     self.scroll.own?(operators) and self.panel.usable?(operators)
117   end
118   
119   def store operators, old_t = nil
120     res = false
121     self.class.transaction do
122       case self.allow? operators
123       when true
124         self.rotate old_t
125       when false
126         raise ActiveRecord::Forbidden
127       else
128       end
129       res = self.save
130       raise ActiveRecord::Rollback unless res
131       res = self.class.validate_t(self.scroll_id) 
132       unless res
133         self.errors.add :t, 'unserialized'
134         raise ActiveRecord::Rollback 
135       end
136     end
137     res
138   end
139   
140 end