1 class SheetPanel < Peta::Element
6 accepts_nested_attributes_for :panel, :allow_destroy => true
8 validates :sheet_id, :numericality => {:allow_blank => true}
9 validates :panel_id, :numericality => {:allow_blank => true}
10 validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
11 validates :x, :presence => true, :numericality => true
12 validates :y, :presence => true, :numericality => true
13 validates :z, :presence => true, :numericality => {:greater_than => 0}
14 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
20 self.t = self.sheet.new_t
21 self.z = self.sheet.new_z
30 def overwrite operators
31 return false unless operators.author
32 self.author_id = operators.author.id
35 def visible? operators
36 return false unless super
37 self.owner_model.visible? operators
45 'sheet_panels.updated_at desc'
48 def self.play_list_where cid
49 ['sheet_panels.sheet_id = ?', cid]
52 def self.play_list sheet, author
53 SheetPanel.where(self.play_list_where(sheet.id)).includes(SheetPanel.list_opt).order('sheet_panels.t')
64 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
65 :speech_balloons =>{:balloon => {}, :speech => {}}
70 def self.list_json_opt
78 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
79 :speech_balloons =>{:balloon => {}, :speech => {}}
92 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
93 :speech_balloons =>{:balloon => {}, :speech => {}}
102 def sheet_panel_as_json au
103 panel_include = if self.panel and self.panel.visible?(au)
104 {:include => {:author => {}}, :methods => :elements}
106 {:include => {:author => {}}}
108 self.to_json({:include => {:sheet => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
111 def self.list_as_json_text ary, au
112 '[' + ary.map {|i| i.sheet_panel_as_json(au) }.join(',') + ']'
115 def licensed_pictures
117 self.panel.licensed_pictures
123 def self.panelize elements_attributes
124 elements_attributes = [elements_attributes] unless elements_attributes.is_a?(Array)
127 elements_attributes.each do |element_attributes|
128 hash[self.to_s.tableize + '_attributes'] ||= {}
129 n = if element_attributes['id']
130 element_attributes['id'].to_s
135 hash[self.to_s.tableize + '_attributes'][n] = element_attributes
144 r.delete 'panel_id' # create panel
145 r.delete 'created_at'
146 r.delete 'updated_at'
151 if self.panel and self.panel.publish? == false
160 {'panel_attributes' => self.panel.copy}
166 def self.new_t sheet_id
167 r = SheetPanel.max_t(sheet_id)
168 r.blank? ? 0 : r.to_i + 1
171 def self.max_t sheet_id
172 SheetPanel.maximum(:t, :conditions => ['sheet_id = ?', sheet_id])
175 def self.find_t sheet_id, t
176 SheetPanel.find(:first, :conditions => ['sheet_id = ? and t = ?', sheet_id, t])
179 def self.collect_t sheet_panel
180 r = SheetPanel.find(:all, :conditions => ['sheet_id = ?', sheet_panel.sheet_id], :order => 't')
186 ary.compact.sort.each do |t|
187 break false unless t == i
190 ary.compact.size == i
193 def self.validate_t sheet_panel
194 SheetPanel.serial?(SheetPanel.collect_t(sheet_panel))
198 SheetPanel.update_all('t = t + 1', ['sheet_id = ? and t >= ?', self.sheet_id, self.t])
201 def lesser_shift old_t
202 self.t = 0 if self.t < 0
203 SheetPanel.update_all('t = t + 1', ['sheet_id = ? and (t >= ? and t < ?)', self.sheet_id, self.t, old_t])
206 def higher_shift old_t
207 nf = SheetPanel.find_t(self.sheet_id, self.t)
208 max_t = SheetPanel.max_t(self.sheet_id).to_i
209 self.t = max_t if self.t > max_t
210 SheetPanel.update_all('t = t - 1', ['sheet_id = ? and (t > ? and t <= ?)', self.sheet_id, old_t, self.t])
213 def update_shift old_t
221 def rotate old_t = nil
224 self.t = SheetPanel.new_t self.sheet_id
231 self.update_shift old_t
237 return nil if self.sheet_id == nil or self.panel_id == nil
238 self.sheet.own?(operators) and self.panel.usable?(operators)
241 def store operators, old_t = nil
243 SheetPanel.transaction do
244 case self.allow? operators
248 raise ActiveRecord::Forbidden
252 raise ActiveRecord::Rollback unless res
253 res = SheetPanel.validate_t(self)
255 self.errors.add :t, 'unserialized'
256 raise ActiveRecord::Rollback
262 def destroy_and_shorten
264 SheetPanel.transaction do
265 SheetPanel.update_all('t = t - 1', ['sheet_id = ? and (t > ?)', self.sheet_id, self.t])
266 raise ActiveRecord::Rollback unless self.destroy