2 class Panel < ActiveRecord::Base
4 belongs_to :resource_picture
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 :x, :numericality => {:allow_blank => true}
19 validates :y, :numericality => {:allow_blank => true}
20 validates :z, :numericality => {:allow_blank => true, :greater_than => 0}
21 validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
22 validates :publish, :presence => true, :numericality => true
24 before_validation :valid_encode
27 ['caption'].each do |a|
28 next if attributes[a] == nil
29 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
33 def self.each_element_class_names
34 Pettanr::Application.elements.each do |k, n|
39 def self.class_name_to_class k
43 def self.each_element_classes
44 self.each_element_class_names do |k|
45 e = self.class_name_to_class k
50 def elements_by_class_name class_name
51 self.__send__ class_name.tableize
60 self.author_id = au.id
64 roles = [roles] unless roles.respond_to?(:each)
65 au = Panel.get_author_from_roles roles
66 return false unless au
67 self.author_id == au.id
71 if MagicNumber['run_mode'] == 0
72 return false unless guest_role_check(roles)
74 return false unless reader_role_check(roles)
76 return true if self.own?(roles)
89 'panel' + self.tag_panel_id
93 self.new_record? ? '0' : self.id.to_s
101 'panels-' + tag_panel_id + '-' + f.to_s
104 def self.default_page_size
108 def self.max_page_size
112 def self.page prm = nil
118 def self.page_size prm = self.default_page_size
120 page_size = self.max_page_size if page_size > self.max_page_size
121 page_size = self.default_page_size if page_size < 1
129 def self.mylist_where au
130 ['panels.author_id = ?', au.id]
133 def self.himlist_where au
134 ['panels.author_id = ? and panels.publish > 0', au.id]
137 def self.list page = 1, page_size = self.default_page_size
138 Panel.where(self.list_where()).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
141 def self.mylist au, page = 1, page_size = Author.default_panel_page_size
142 Panel.where(self.mylist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
145 def self.himlist au, page = 1, page_size = Author.default_panel_page_size
146 Panel.where(self.himlist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
149 def self.list_paginate page = 1, page_size = self.default_page_size
150 Kaminari.paginate_array(Array.new(Panel.where(self.list_where()).count, nil)).page(page).per(page_size)
153 def self.mylist_paginate au, page = 1, page_size = Author.default_panel_page_size
154 Kaminari.paginate_array(Array.new(Panel.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
157 def self.himlist_paginate au, page = 1, page_size = Author.default_panel_page_size
158 Kaminari.paginate_array(Array.new(Panel.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
165 self.each_element_classes do |e|
166 r.merge!(e.list_opt_for_panel)
171 def self.show rid, roles
173 opt.merge!(Panel.show_opt)
174 res = Panel.find(rid, opt)
175 raise ActiveRecord::Forbidden unless res.visible?(roles)
179 def self.edit rid, au
181 opt.merge!(Panel.show_opt)
182 res = Panel.find(rid, opt)
183 raise ActiveRecord::Forbidden unless res.own?(au)
191 self.each_element_classes do |e|
192 r.merge!(e.show_opt_for_panel)
199 self.class.each_element_class_names do |k|
200 r += (self.elements_by_class_name(k) || [])
207 self.parts_element.each do |e|
215 self.parts_element.each do |e|
222 self.panel_elements.map {|e|
223 #(-_-;)<... kore wa hidoi
224 JSON.parse e.to_json({:include => e.class.json_opt_for_panel})
228 def panel_elements_as_json
229 self.to_json({:include => {:author => {}}, :methods => :elements})
232 def self.list_as_json_text ary
233 '[' + ary.map {|i| i.panel_elements_as_json }.join(',') + ']'
237 self.panel_elements.size
241 self.panel_elements.size + 1
245 panel_elements.map { |e|
251 panel_elements.map { |e|
256 def licensed_pictures
258 self.panel_elements.each do |elm|
259 next unless elm.class.has_picture?
260 r[elm.picture_id] = elm.picture unless r[elm.picture_id]
265 def self.visible_count
269 def self.collect_element_value elements, name
272 if o['_destroy'] or o[:_destroy]
281 def self.validate_serial ary, offset = 0
283 ary.compact.sort.each do |n|
284 break false unless n == i
287 ary.compact.size == i - offset
290 def self.validate_element_serial elements, name, offset = 0
291 Panel.validate_serial(Panel.collect_element_value(elements, name), offset)
294 def self.validate_elements_serial c
296 Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false
300 def validate_serial_list
302 self.class.each_element_class_names do |k|
303 l << self.elements_by_class_name(k)
306 {:elements => l, :name => :t, :offset => 0},
307 {:elements => l, :name => :z, :offset => 1}
311 # r1 = Panel.validate_elements_id validate_id_list
312 Panel.validate_elements_serial validate_serial_list
317 self.errors.add :base, I18n.t('errors.invalid_json')
320 self.attributes = attr
325 unless validate_child
327 self.errors.add :base, I18n.t('errors.invalid_t')
328 raise ActiveRecord::Rollback
334 def remove_element target, au
337 panel_attributes = {}
338 self.panel_elements.each do |elm|
339 attr = elm.attributes
341 attr['_destroy'] = true
349 panel_attributes[elm.class.to_s.tableize + '_attributes'] ||= {}
350 panel_attributes[elm.class.to_s.tableize + '_attributes'][elm.id] = attr
352 self.store(panel_attributes, au)
355 def destroy_with_elements
358 self.parts_element.each do |element|
359 raise ActiveRecord::Rollback unless element.destroy
361 raise ActiveRecord::Rollback unless self.destroy
368 attr = self.copy_attributes
369 attr.merge! PanelPicture.panelize(self.panel_pictures.map {|elm| elm.copy_attributes})
370 attr.merge! SpeechBalloon.panelize(self.speech_balloons.map {|elm| elm.copy_attributes})
371 attr.merge! GroundPicture.panelize(self.ground_pictures.map {|elm| elm.copy_attributes})
372 attr.merge! GroundColor.panelize(self.ground_colors.map {|elm| elm.copy_attributes})
381 r.delete 'created_at'
382 r.delete 'updated_at'
386 def self.panelize panel
387 attr = panel.attributes
394 def self.validate_id ary, pid
397 (v == pid or v == nil) ? nil : false
404 def self.validate_element_id elements, name, pid
405 Panel.validate_id(Panel.collect_element_value(elements, name), pid)
408 def self.validate_elements_id c
410 Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false
415 r = self.speech_balloons.map {|sb|
416 {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id}
418 r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id})