2 class Panel < Peta::Root
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
27 def overwrite operators
28 return false unless operators.author
29 self.author_id = operators.author.id
32 def visible? operators
34 return true if self.new_record?
39 self.visible? operators
48 'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px',
49 'border-style' => 'solid', 'border-width' => self.border.to_s + 'px',
50 'border-color' => 'black', 'background-color' => 'white'
54 # ground_picture element template
57 'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px'
66 'panels.updated_at desc'
73 self.child_models.each do |child_model|
74 r.merge!(child_model.list_opt_for_panel)
83 self.child_models('panel').each do |child_model|
84 r.merge!(child_model.show_opt_for_panel)
95 self.parts_element.each do |e|
103 self.parts_element.each do |e|
110 self.panel_elements.map {|e|
111 #(-_-;)<... kore wa hidoi
112 JSON.parse e.to_json({:include => e.class.json_opt_for_panel})
116 def panel_elements_as_json
117 self.to_json({:include => {:author => {}}, :methods => :elements})
120 def self.list_as_json_text ary
121 '[' + ary.map {|i| i.panel_elements_as_json }.join(',') + ']'
125 self.panel_elements.size
129 self.panel_elements.size + 1
133 panel_elements.map { |e|
139 panel_elements.map { |e|
144 def licensed_pictures
146 self.panel_elements.each do |elm|
147 next unless elm.class.has_picture?
148 r[elm.picture_id] = elm.picture unless r[elm.picture_id]
153 def self.visible_count
157 def self.collect_element_value elements, name
160 if o['_destroy'] or o[:_destroy]
169 def self.validate_serial ary, offset = 0
171 ary.compact.sort.each do |n|
172 break false unless n == i
175 ary.compact.size == i - offset
178 def self.validate_element_serial elements, name, offset = 0
179 Panel.validate_serial(Panel.collect_element_value(elements, name), offset)
182 def self.validate_elements_serial c
184 Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false
188 def validate_serial_list
190 {:elements => self.elements_items, :name => :t, :offset => 0},
191 {:elements => self.elements_items, :name => :z, :offset => 1}
195 # r1 = Panel.validate_elements_id validate_id_list
196 Panel.validate_elements_serial validate_serial_list
199 def store attr, operators
201 self.errors.add :base, I18n.t('errors.invalid_json')
204 self.attributes = attr
205 self.overwrite operators
208 self.panel_elements.each do |elm|
214 unless validate_child
216 self.errors.add :base, I18n.t('errors.invalid_t')
217 raise ActiveRecord::Rollback
223 def remove_element target, operators
226 panel_attributes = {}
227 self.panel_elements.each do |elm|
228 attr = elm.attributes
230 attr['_destroy'] = true
238 panel_attributes[elm.class.to_s.tableize + '_attributes'] ||= {}
239 panel_attributes[elm.class.to_s.tableize + '_attributes'][elm.id] = attr
241 self.store(panel_attributes, operators)
244 def destroy_with_elements
247 self.parts_element.each do |element|
248 raise ActiveRecord::Rollback unless element.destroy
250 raise ActiveRecord::Rollback unless self.destroy
257 attr = self.copy_attributes
258 self.class.child_models('panel') do |child_model|
259 attr.merge! child_model.panelize(self.element_items(child_model).map {|elm| elm.copy_attributes})
268 r.delete 'created_at'
269 r.delete 'updated_at'
273 def self.panelize panel
274 attr = panel.attributes
281 def self.validate_id ary, pid
284 (v == pid or v == nil) ? nil : false
291 def self.validate_element_id elements, name, pid
292 Panel.validate_id(Panel.collect_element_value(elements, name), pid)
295 def self.validate_elements_id c
297 Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false
302 r = self.speech_balloons.map {|sb|
303 {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id}
305 r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id})