2 class Panel < Peta::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 Manifest.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 = {})
90 {:html => {:selected => selected}},
91 self.field_tag_attributes(column, opt)
95 def field_tag_attributes column, no_attr, opt = {}
96 self.tag_attributes(column).merge(
101 def tag_attr column = nil, opt = {}
102 self.tag_attributes(column, opt).to_attr
105 def field_tag_attr column, no_attr, opt = {}
106 self.field_tag_attributes(column, no_attr, opt).to_attr
114 @render_count = render_count + 1
122 'panels.updated_at desc'
129 self.each_element_classes do |e|
130 r.merge!(e.list_opt_for_panel)
139 self.each_element_classes do |e|
140 r.merge!(e.show_opt_for_panel)
147 self.class.each_element_class_names do |k|
148 r += (self.elements_by_class_name(k) || [])
155 self.parts_element.each do |e|
163 self.parts_element.each do |e|
170 self.panel_elements.map {|e|
171 #(-_-;)<... kore wa hidoi
172 JSON.parse e.to_json({:include => e.class.json_opt_for_panel})
176 def panel_elements_as_json
177 self.to_json({:include => {:author => {}}, :methods => :elements})
180 def self.list_as_json_text ary
181 '[' + ary.map {|i| i.panel_elements_as_json }.join(',') + ']'
185 self.panel_elements.size
189 self.panel_elements.size + 1
193 panel_elements.map { |e|
199 panel_elements.map { |e|
204 def licensed_pictures
206 self.panel_elements.each do |elm|
207 next unless elm.class.has_picture?
208 r[elm.picture_id] = elm.picture unless r[elm.picture_id]
213 def self.visible_count
217 def self.collect_element_value elements, name
220 if o['_destroy'] or o[:_destroy]
229 def self.validate_serial ary, offset = 0
231 ary.compact.sort.each do |n|
232 break false unless n == i
235 ary.compact.size == i - offset
238 def self.validate_element_serial elements, name, offset = 0
239 Panel.validate_serial(Panel.collect_element_value(elements, name), offset)
242 def self.validate_elements_serial c
244 Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false
248 def validate_serial_list
250 self.class.each_element_class_names do |k|
251 l << self.elements_by_class_name(k)
254 {:elements => l, :name => :t, :offset => 0},
255 {:elements => l, :name => :z, :offset => 1}
259 # r1 = Panel.validate_elements_id validate_id_list
260 Panel.validate_elements_serial validate_serial_list
264 @new_element_index = 0
265 self.panel_elements.each do |elm|
267 elm.new_index = @new_element_index
268 @new_element_index += 1
273 def store attr, operators
275 self.errors.add :base, I18n.t('errors.invalid_json')
278 self.attributes = attr
279 self.overwrite operators
282 self.panel_elements.each do |elm|
288 unless validate_child
290 self.errors.add :base, I18n.t('errors.invalid_t')
291 raise ActiveRecord::Rollback
297 def remove_element target, operators
300 panel_attributes = {}
301 self.panel_elements.each do |elm|
302 attr = elm.attributes
304 attr['_destroy'] = true
312 panel_attributes[elm.class.to_s.tableize + '_attributes'] ||= {}
313 panel_attributes[elm.class.to_s.tableize + '_attributes'][elm.id] = attr
315 self.store(panel_attributes, operators)
318 def destroy_with_elements
321 self.parts_element.each do |element|
322 raise ActiveRecord::Rollback unless element.destroy
324 raise ActiveRecord::Rollback unless self.destroy
331 attr = self.copy_attributes
332 Panel.each_element_class_names do |n|
333 attr.merge! Panel.class_name_to_class(n).panelize(self.elements_by_class_name(n).map {|elm| elm.copy_attributes})
342 r.delete 'created_at'
343 r.delete 'updated_at'
347 def self.panelize panel
348 attr = panel.attributes
355 def self.validate_id ary, pid
358 (v == pid or v == nil) ? nil : false
365 def self.validate_element_id elements, name, pid
366 Panel.validate_id(Panel.collect_element_value(elements, name), pid)
369 def self.validate_elements_id c
371 Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false
376 r = self.speech_balloons.map {|sb|
377 {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id}
379 r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id})