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
166 if o['_destroy'] or o[:_destroy]
175 def self.validate_serial ary, offset = 0
177 ary.compact.sort.each do |n|
178 break false unless n == i
181 ary.compact.size == i - offset
184 def self.validate_element_serial elements, name, offset = 0
185 Panel.validate_serial(Panel.collect_element_value(elements, name), offset)
188 def self.validate_elements_serial c
190 Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false
194 def validate_serial_list
196 {:elements => self.elements_items, :name => :t, :offset => 0},
197 {:elements => self.elements_items, :name => :z, :offset => 1}
201 # r1 = Panel.validate_elements_id validate_id_list
202 Panel.validate_elements_serial validate_serial_list
205 def store attr, operators
207 self.errors.add :base, I18n.t('errors.invalid_json')
210 self.attributes = attr
211 self.overwrite operators
214 self.panel_elements.each do |elm|
219 unless validate_child
221 self.errors.add :base, I18n.t('errors.invalid_t')
222 raise ActiveRecord::Rollback
228 def remove_element target, operators
231 panel_attributes = {}
232 self.panel_elements.each do |elm|
233 attr = elm.attributes
235 attr['_destroy'] = true
243 panel_attributes[elm.class.to_s.tableize + '_attributes'] ||= {}
244 panel_attributes[elm.class.to_s.tableize + '_attributes'][elm.id] = attr
246 self.store(panel_attributes, operators)
249 def destroy_with_elements
252 self.parts_element.each do |element|
253 raise ActiveRecord::Rollback unless element.destroy
255 raise ActiveRecord::Rollback unless self.destroy
261 def self.panelize panel
262 attr = panel.attributes
269 def self.validate_id ary, pid
272 (v == pid or v == nil) ? nil : false
279 def self.validate_element_id elements, name, pid
280 Panel.validate_id(Panel.collect_element_value(elements, name), pid)
283 def self.validate_elements_id c
285 Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false
290 r = self.speech_balloons.map {|sb|
291 {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id}
293 r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id})