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 self.public_list_where
40 'sheet_panels.updated_at desc'
43 def self.play_list_where cid
44 ['sheet_panels.sheet_id = ?', cid]
47 def self.play_list sheet, author
48 SheetPanel.where(self.play_list_where(sheet.id)).includes(SheetPanel.list_opt).order('sheet_panels.t')
51 def self.by_author_list_includes
67 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
68 :speech_balloons =>{:balloon => {}, :speech => {}}
73 def self.list_json_opt
81 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
82 :speech_balloons =>{:balloon => {}, :speech => {}}
95 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
96 :speech_balloons =>{:balloon => {}, :speech => {}}
105 def sheet_panel_as_json au
106 panel_include = if self.panel and self.panel.visible?(au)
107 {:include => {:author => {}}, :methods => :elements}
109 {:include => {:author => {}}}
111 self.to_json({:include => {:sheet => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
114 def self.list_as_json_text ary, au
115 '[' + ary.map {|i| i.sheet_panel_as_json(au) }.join(',') + ']'
118 def licensed_pictures
120 self.panel.licensed_pictures
126 def self.panelize elements_attributes
127 elements_attributes = [elements_attributes] unless elements_attributes.is_a?(Array)
130 elements_attributes.each do |element_attributes|
131 hash[self.to_s.tableize + '_attributes'] ||= {}
132 n = if element_attributes['id']
133 element_attributes['id'].to_s
138 hash[self.to_s.tableize + '_attributes'][n] = element_attributes
147 r.delete 'panel_id' # create panel
148 r.delete 'created_at'
149 r.delete 'updated_at'
154 if self.panel and self.panel.publish? == false
163 {'panel_attributes' => self.panel.copy}
169 def self.new_t sheet_id
170 r = SheetPanel.max_t(sheet_id)
171 r.blank? ? 0 : r.to_i + 1
174 def self.max_t sheet_id
175 SheetPanel.maximum(:t, :conditions => ['sheet_id = ?', sheet_id])
178 def self.find_t sheet_id, t
179 SheetPanel.find(:first, :conditions => ['sheet_id = ? and t = ?', sheet_id, t])
182 def self.collect_t sheet_panel
183 r = SheetPanel.find(:all, :conditions => ['sheet_id = ?', sheet_panel.sheet_id], :order => 't')
189 ary.compact.sort.each do |t|
190 break false unless t == i
193 ary.compact.size == i
196 def self.validate_t sheet_panel
197 SheetPanel.serial?(SheetPanel.collect_t(sheet_panel))
201 SheetPanel.update_all('t = t + 1', ['sheet_id = ? and t >= ?', self.sheet_id, self.t])
204 def lesser_shift old_t
205 self.t = 0 if self.t < 0
206 SheetPanel.update_all('t = t + 1', ['sheet_id = ? and (t >= ? and t < ?)', self.sheet_id, self.t, old_t])
209 def higher_shift old_t
210 nf = SheetPanel.find_t(self.sheet_id, self.t)
211 max_t = SheetPanel.max_t(self.sheet_id).to_i
212 self.t = max_t if self.t > max_t
213 SheetPanel.update_all('t = t - 1', ['sheet_id = ? and (t > ? and t <= ?)', self.sheet_id, old_t, self.t])
216 def update_shift old_t
224 def rotate old_t = nil
227 self.t = SheetPanel.new_t self.sheet_id
234 self.update_shift old_t
240 return nil if self.sheet_id == nil or self.panel_id == nil
241 self.sheet.own?(operators) and self.panel.usable?(operators)
244 def store operators, old_t = nil
246 SheetPanel.transaction do
247 case self.allow? operators
251 raise ActiveRecord::Forbidden
255 raise ActiveRecord::Rollback unless res
256 res = SheetPanel.validate_t(self)
258 self.errors.add :t, 'unserialized'
259 raise ActiveRecord::Rollback
265 def destroy_and_shorten
267 SheetPanel.transaction do
268 SheetPanel.update_all('t = t - 1', ['sheet_id = ? and (t > ?)', self.sheet_id, self.t])
269 raise ActiveRecord::Rollback unless self.destroy