OSDN Git Service

fix form
[pettanr/pettanr.git] / app / models / scroll_panel.rb
1 class ScrollPanel < Peta::Content
2   belongs_to :author
3   belongs_to :panel
4   belongs_to :scroll
5   
6   validates :scroll_id, :presence => true, :numericality => true, :existence => {:both => false}
7   validates :panel_id, :presence => true, :numericality => true, :existence => {:both => false}
8   validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
9   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
10   
11   def self.owner_model
12     Scroll
13   end
14   
15   def supply_default
16     self.scroll_id = nil
17     self.panel_id = nil
18     self.t = nil
19   end
20   
21   def overwrite operators
22     return false unless operators.author
23     self.author_id = operators.author.id
24   end
25   
26   def visible? operators
27     return false unless super
28     self.owner_model.visible? operators
29   end
30   
31   def self.list_order
32     'scroll_panels.updated_at desc'
33   end
34   
35   def self.list_where
36     'scrolls.visible > 0'
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 self.new_t scroll_id
115     r = ScrollPanel.max_t(scroll_id)
116     r.blank? ? 0 : r.to_i + 1
117   end
118   
119   def self.max_t scroll_id
120     ScrollPanel.maximum(:t, :conditions => ['scroll_id = ?', scroll_id])
121   end
122   
123   def self.find_t scroll_id, t
124     ScrollPanel.find(:first, :conditions => ['scroll_id = ? and t = ?', scroll_id, t])
125   end
126   
127   def self.collect_t scroll_panel
128     r = ScrollPanel.find(:all, :conditions => ['scroll_id = ?', scroll_panel.scroll_id], :order => 't')
129     r.map {|sp| sp.t}
130   end
131   
132   def self.serial? ary
133     i = 0
134     ary.compact.sort.each do |t|
135       break false unless t == i
136       i += 1
137     end
138     ary.compact.size == i
139   end
140   
141   def self.validate_t scroll_panel
142     ScrollPanel.serial?(ScrollPanel.collect_t(scroll_panel))
143   end
144   
145   def insert_shift
146     ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and t >= ?', self.scroll_id, self.t])
147   end
148   
149   def lesser_shift old_t
150     self.t = 0 if self.t < 0
151     ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and (t >= ? and t < ?)', self.scroll_id, self.t, old_t])
152   end
153   
154   def higher_shift old_t
155     nf = ScrollPanel.find_t(self.scroll_id, self.t)
156     max_t = ScrollPanel.max_t(self.scroll_id).to_i
157     self.t = max_t if self.t > max_t
158     ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ? and t <= ?)', self.scroll_id, old_t, self.t])
159   end
160   
161   def update_shift old_t
162     if self.t > old_t
163       higher_shift old_t
164     else
165       lesser_shift old_t
166     end
167   end
168   
169   def rotate old_t = nil
170     if self.new_record?
171       if self.t.blank?
172         self.t = ScrollPanel.new_t self.scroll_id
173       else
174         self.insert_shift
175       end
176     else
177       if self.t.blank?
178       else
179         self.update_shift old_t
180       end
181     end
182   end
183   
184   def allow? operators
185     return nil if self.scroll_id == nil or self.panel_id == nil
186     self.scroll.own?(operators) and self.panel.usable?(operators)
187   end
188   
189   def store operators, old_t = nil
190     res = false
191     ScrollPanel.transaction do
192       case self.allow? operators
193       when true
194         self.rotate old_t
195       when false
196         raise ActiveRecord::Forbidden
197       else
198       end
199       res = self.save
200       raise ActiveRecord::Rollback unless res
201       res = ScrollPanel.validate_t(self) 
202       unless res
203         self.errors.add :t, 'unserialized'
204         raise ActiveRecord::Rollback 
205       end
206     end
207     res
208   end
209   
210   def destroy_and_shorten
211     res = false
212     ScrollPanel.transaction do
213       ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ?)', self.scroll_id, self.t])
214       raise ActiveRecord::Rollback unless self.destroy
215       res = true
216     end
217     res
218   end
219   
220 end