2 class Panel < Pettanr::Content
4 has_many :scroll_panels
6 has_many :panel_pictures, :dependent => :destroy
7 has_many :speech_balloons, :dependent => :destroy
8 has_many :ground_pictures, :dependent => :destroy
9 has_many :ground_colors, :dependent => :destroy
10 accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
11 accepts_nested_attributes_for :speech_balloons, :allow_destroy => true
12 accepts_nested_attributes_for :ground_pictures, :allow_destroy => true
13 accepts_nested_attributes_for :ground_colors, :allow_destroy => true
15 validates :width, :presence => true, :numericality => true, :natural_number => true
16 validates :height, :presence => true, :numericality => true, :natural_number => true
17 validates :border, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
18 validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
19 validates :publish, :presence => true, :numericality => true
21 def self.valid_encode_columns
25 def self.each_element_class_names
26 Pettanr::Application.manifest.system_resources.elements.each do |k, n|
31 def self.class_name_to_class k
35 def self.each_element_classes
36 self.each_element_class_names do |k|
37 e = self.class_name_to_class k
42 def elements_by_class_name class_name
43 self.__send__ class_name.tableize
51 def overwrite operators
52 return false unless operators.author
53 self.author_id = operators.author.id
56 def visible? operators
58 return true if self.new_record?
63 self.visible? operators
71 'panel' + self.tag_panel_id + c.to_s
75 self.new_record? ? '0' : self.id.to_s
82 def tag_attributes column = nil, opt = {}
84 :id => self.field_tag_id(column), :panel_id => self.tag_panel_id
88 def select_tag_attributes(selected, column, opt = {})
91 {:html => {:selected => selected}},
92 self.field_tag_attributes(column, opt)
96 def field_tag_attributes column, opt = {}
97 self.tag_attributes(column).merge(
102 def tag_attr column = nil, opt = {}
103 self.tag_attributes(column, opt).to_attr
106 def field_tag_attr column, no_attr, opt = {}
107 self.field_tag_attributes(column, no_attr, opt).to_attr
115 @render_count = render_count + 1
123 'panels.updated_at desc'
130 self.each_element_classes do |e|
131 r.merge!(e.list_opt_for_panel)
140 self.each_element_classes do |e|
141 r.merge!(e.show_opt_for_panel)
148 self.class.each_element_class_names do |k|
149 r += (self.elements_by_class_name(k) || [])
156 self.parts_element.each do |e|
164 self.parts_element.each do |e|
171 self.panel_elements.map {|e|
172 #(-_-;)<... kore wa hidoi
173 JSON.parse e.to_json({:include => e.class.json_opt_for_panel})
177 def panel_elements_as_json
178 self.to_json({:include => {:author => {}}, :methods => :elements})
181 def self.list_as_json_text ary
182 '[' + ary.map {|i| i.panel_elements_as_json }.join(',') + ']'
186 self.panel_elements.size
190 self.panel_elements.size + 1
194 panel_elements.map { |e|
200 panel_elements.map { |e|
205 def licensed_pictures
207 self.panel_elements.each do |elm|
208 next unless elm.class.has_picture?
209 r[elm.picture_id] = elm.picture unless r[elm.picture_id]
214 def self.visible_count
218 def self.collect_element_value elements, name
221 if o['_destroy'] or o[:_destroy]
230 def self.validate_serial ary, offset = 0
232 ary.compact.sort.each do |n|
233 break false unless n == i
236 ary.compact.size == i - offset
239 def self.validate_element_serial elements, name, offset = 0
240 Panel.validate_serial(Panel.collect_element_value(elements, name), offset)
243 def self.validate_elements_serial c
245 Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false
249 def validate_serial_list
251 self.class.each_element_class_names do |k|
252 l << self.elements_by_class_name(k)
255 {:elements => l, :name => :t, :offset => 0},
256 {:elements => l, :name => :z, :offset => 1}
260 # r1 = Panel.validate_elements_id validate_id_list
261 Panel.validate_elements_serial validate_serial_list
265 @new_element_index = 0
266 self.panel_elements.each do |elm|
268 elm.new_index = @new_element_index
269 @new_element_index += 1
274 def store attr, operators
276 self.errors.add :base, I18n.t('errors.invalid_json')
279 self.attributes = attr
280 self.overwrite operators
283 self.panel_elements.each do |elm|
289 unless validate_child
291 self.errors.add :base, I18n.t('errors.invalid_t')
292 raise ActiveRecord::Rollback
298 def remove_element target, operators
301 panel_attributes = {}
302 self.panel_elements.each do |elm|
303 attr = elm.attributes
305 attr['_destroy'] = true
313 panel_attributes[elm.class.to_s.tableize + '_attributes'] ||= {}
314 panel_attributes[elm.class.to_s.tableize + '_attributes'][elm.id] = attr
316 self.store(panel_attributes, operators)
319 def destroy_with_elements
322 self.parts_element.each do |element|
323 raise ActiveRecord::Rollback unless element.destroy
325 raise ActiveRecord::Rollback unless self.destroy
332 attr = self.copy_attributes
333 Panel.each_element_class_names do |n|
334 attr.merge! Panel.class_name_to_class(n).panelize(self.elements_by_class_name(n).map {|elm| elm.copy_attributes})
343 r.delete 'created_at'
344 r.delete 'updated_at'
348 def self.panelize panel
349 attr = panel.attributes
356 def self.validate_id ary, pid
359 (v == pid or v == nil) ? nil : false
366 def self.validate_element_id elements, name, pid
367 Panel.validate_id(Panel.collect_element_value(elements, name), pid)
370 def self.validate_elements_id c
372 Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false
377 r = self.speech_balloons.map {|sb|
378 {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id}
380 r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id})