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 when nil # super return
39 return true if self.new_record?
45 self.visible? operators
54 'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px',
55 'border-style' => 'solid', 'border-width' => self.border.to_s + 'px',
56 'border-color' => 'black', 'background-color' => 'white'
60 # ground_picture element template
63 'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px'
67 def self.public_list_where
72 'panels.updated_at desc'
79 self.child_models.each do |child_model|
80 r.merge!(child_model.list_opt_for_panel)
89 self.child_models.each do |child_model|
90 r.merge!(child_model.show_opt_for_panel)
101 self.parts_element.each do |e|
109 self.parts_element.each do |e|
116 self.panel_elements.map {|e|
117 #(-_-;)<... kore wa hidoi
118 JSON.parse e.to_json({:include => e.class.json_opt_for_panel})
122 def panel_elements_as_json
123 self.to_json({:include => {:author => {}}, :methods => :elements})
126 def self.list_as_json_text ary
127 '[' + ary.map {|i| i.panel_elements_as_json }.join(',') + ']'
131 self.panel_elements.size
135 self.panel_elements.size + 1
139 panel_elements.map { |e|
145 panel_elements.map { |e|
150 def licensed_pictures
152 self.panel_elements.each do |elm|
153 next unless elm.class.has_picture?
154 r[elm.picture_id] = elm.picture unless r[elm.picture_id]
159 def self.visible_count
163 def self.collect_element_value elements, name
165 if e['_destroy'] or e[:_destroy]
173 def self.validate_serial ary, offset = 0
175 ary.compact.sort.each do |n|
176 break false unless n == i
179 ary.compact.size == i - offset
182 def self.validate_element_serial elements, name, offset = 0
183 Panel.validate_serial(Panel.collect_element_value(elements, name), offset)
186 def self.validate_elements_serial c
188 Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false
192 def validate_serial_list
194 {:elements => self.elements_items, :name => :t, :offset => 0},
195 {:elements => self.elements_items, :name => :z, :offset => 1}
199 # r1 = Panel.validate_elements_id validate_id_list
200 Panel.validate_elements_serial validate_serial_list
203 def store attr, operators
205 self.errors.add :base, I18n.t('errors.invalid_json')
208 self.attributes = attr
209 self.overwrite operators
215 unless validate_child
217 self.errors.add :base, I18n.t('errors.invalid_t')
218 raise ActiveRecord::Rollback
224 def remove_element target, operators
227 panel_attributes = {}
228 self.panel_elements.each do |elm|
229 attr = elm.attributes
231 attr['_destroy'] = true
239 panel_attributes[elm.class.to_s.tableize + '_attributes'] ||= {}
240 panel_attributes[elm.class.to_s.tableize + '_attributes'][elm.id] = attr
242 self.store(panel_attributes, operators)
245 def destroy_with_elements
248 self.parts_element.each do |element|
249 raise ActiveRecord::Rollback unless element.destroy
251 raise ActiveRecord::Rollback unless self.destroy
257 def self.panelize panel
258 attr = panel.attributes
265 def self.validate_id ary, pid
268 (v == pid or v == nil) ? nil : false
275 def self.validate_element_id elements, name, pid
276 Panel.validate_id(Panel.collect_element_value(elements, name), pid)
279 def self.validate_elements_id c
281 Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false
286 r = self.speech_balloons.map {|sb|
287 {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id}
289 r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id})