2 class Panel < Peta::Content
5 has_many :scroll_panels
7 has_many :panel_pictures, :dependent => :destroy
8 has_many :speech_balloons, :dependent => :destroy
9 has_many :ground_pictures, :dependent => :destroy
10 has_many :ground_colors, :dependent => :destroy
11 accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
12 accepts_nested_attributes_for :speech_balloons, :allow_destroy => true
13 accepts_nested_attributes_for :ground_pictures, :allow_destroy => true
14 accepts_nested_attributes_for :ground_colors, :allow_destroy => true
16 validates :width, :presence => true, :numericality => true, :natural_number => true
17 validates :height, :presence => true, :numericality => true, :natural_number => true
18 validates :border, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
19 validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
20 validates :publish, :presence => true, :numericality => true
22 def self.each_element_class_names
23 Manifest.manifest.system_resources.elements.each do |k, n|
28 def self.class_name_to_class k
32 def self.each_element_classes
33 self.each_element_class_names do |k|
34 e = self.class_name_to_class k
39 def elements_by_class_name class_name
40 self.__send__ class_name.tableize
48 def overwrite operators
49 return false unless operators.author
50 self.author_id = operators.author.id
53 def visible? operators
55 return true if self.new_record?
60 self.visible? operators
69 'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px',
70 'border-style' => 'solid', 'border-width' => self.border.to_s + 'px',
71 'border-color' => 'black', 'background-color' => 'white'
75 # ground_picture element template
78 'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px'
87 'panels.updated_at desc'
94 self.each_element_classes do |e|
95 r.merge!(e.list_opt_for_panel)
104 self.each_element_classes do |e|
105 r.merge!(e.show_opt_for_panel)
112 self.class.each_element_class_names do |k|
113 r += (self.elements_by_class_name(k) || [])
120 self.parts_element.each do |e|
128 self.parts_element.each do |e|
135 self.panel_elements.map {|e|
136 #(-_-;)<... kore wa hidoi
137 JSON.parse e.to_json({:include => e.class.json_opt_for_panel})
141 def panel_elements_as_json
142 self.to_json({:include => {:author => {}}, :methods => :elements})
145 def self.list_as_json_text ary
146 '[' + ary.map {|i| i.panel_elements_as_json }.join(',') + ']'
150 self.panel_elements.size
154 self.panel_elements.size + 1
158 panel_elements.map { |e|
164 panel_elements.map { |e|
169 def licensed_pictures
171 self.panel_elements.each do |elm|
172 next unless elm.class.has_picture?
173 r[elm.picture_id] = elm.picture unless r[elm.picture_id]
178 def self.visible_count
182 def self.collect_element_value elements, name
185 if o['_destroy'] or o[:_destroy]
194 def self.validate_serial ary, offset = 0
196 ary.compact.sort.each do |n|
197 break false unless n == i
200 ary.compact.size == i - offset
203 def self.validate_element_serial elements, name, offset = 0
204 Panel.validate_serial(Panel.collect_element_value(elements, name), offset)
207 def self.validate_elements_serial c
209 Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false
213 def validate_serial_list
215 self.class.each_element_class_names do |k|
216 l << self.elements_by_class_name(k)
219 {:elements => l, :name => :t, :offset => 0},
220 {:elements => l, :name => :z, :offset => 1}
224 # r1 = Panel.validate_elements_id validate_id_list
225 Panel.validate_elements_serial validate_serial_list
228 def store attr, operators
230 self.errors.add :base, I18n.t('errors.invalid_json')
233 self.attributes = attr
234 self.overwrite operators
237 self.panel_elements.each do |elm|
243 unless validate_child
245 self.errors.add :base, I18n.t('errors.invalid_t')
246 raise ActiveRecord::Rollback
252 def remove_element target, operators
255 panel_attributes = {}
256 self.panel_elements.each do |elm|
257 attr = elm.attributes
259 attr['_destroy'] = true
267 panel_attributes[elm.class.to_s.tableize + '_attributes'] ||= {}
268 panel_attributes[elm.class.to_s.tableize + '_attributes'][elm.id] = attr
270 self.store(panel_attributes, operators)
273 def destroy_with_elements
276 self.parts_element.each do |element|
277 raise ActiveRecord::Rollback unless element.destroy
279 raise ActiveRecord::Rollback unless self.destroy
286 attr = self.copy_attributes
287 Panel.each_element_class_names do |n|
288 attr.merge! Panel.class_name_to_class(n).panelize(self.elements_by_class_name(n).map {|elm| elm.copy_attributes})
297 r.delete 'created_at'
298 r.delete 'updated_at'
302 def self.panelize panel
303 attr = panel.attributes
310 def self.validate_id ary, pid
313 (v == pid or v == nil) ? nil : false
320 def self.validate_element_id elements, name, pid
321 Panel.validate_id(Panel.collect_element_value(elements, name), pid)
324 def self.validate_elements_id c
326 Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false
331 r = self.speech_balloons.map {|sb|
332 {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id}
334 r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id})