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?
39 self.author_id = au.id
43 roles = [roles] unless roles.respond_to?(:each)
44 au = Panel.get_author_from_roles roles
45 return false unless au
46 self.author_id == au.id
50 if MagicNumber['run_mode'] == 0
51 return false unless guest_role_check(roles)
53 return false unless reader_role_check(roles)
55 return true if self.own?(roles)
68 'panel' + self.tag_panel_id
72 self.new_record? ? '0' : self.id.to_s
80 'panels-' + tag_panel_id + '-' + f.to_s
83 def self.default_page_size
87 def self.max_page_size
91 def self.page prm = nil
97 def self.page_size prm = self.default_page_size
99 page_size = self.max_page_size if page_size > self.max_page_size
100 page_size = self.default_page_size if page_size < 1
108 def self.mylist_where au
109 ['panels.author_id = ?', au.id]
112 def self.himlist_where au
113 ['panels.author_id = ? and panels.publish > 0', au.id]
116 def self.list page = 1, page_size = self.default_page_size
117 Panel.where(self.list_where()).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
120 def self.mylist au, page = 1, page_size = Author.default_panel_page_size
121 Panel.where(self.mylist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
124 def self.himlist au, page = 1, page_size = Author.default_panel_page_size
125 Panel.where(self.himlist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
128 def self.list_paginate page = 1, page_size = self.default_page_size
129 Kaminari.paginate_array(Array.new(Panel.where(self.list_where()).count, nil)).page(page).per(page_size)
132 def self.mylist_paginate au, page = 1, page_size = Author.default_panel_page_size
133 Kaminari.paginate_array(Array.new(Panel.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
136 def self.himlist_paginate au, page = 1, page_size = Author.default_panel_page_size
137 Kaminari.paginate_array(Array.new(Panel.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
143 :picture => {:artist => {}, :license => {}}
145 :speech_balloons => {:balloon => {}, :speech => {}},
146 :ground_pictures => {
147 :picture => {:artist => {}, :license => {}}
155 def self.show rid, roles
157 opt.merge!(Panel.show_opt)
158 res = Panel.find(rid, opt)
159 raise ActiveRecord::Forbidden unless res.visible?(roles)
163 def self.edit rid, au
165 opt.merge!(Panel.show_opt)
166 res = Panel.find(rid, opt)
167 raise ActiveRecord::Forbidden unless res.own?(au)
174 :picture => {:artist => {}, :license => {}}
176 :speech_balloons => {:balloon => {}, :speech => {}},
177 :ground_pictures => {
178 :picture => {:artist => {}, :license => {}}
187 ((self.panel_pictures || []) + (self.speech_balloons || []) + (self.ground_colors || []) + (self.ground_pictures || [])).compact
195 self.parts_element.each do |e|
203 self.parts_element.each do |e|
211 :picture => {:artist => {}, :license => {}}
213 'SpeechBalloon' => {:balloon => {}, :speech => {}},
215 :picture => {:artist => {}, :license => {}}
221 def self.elm_json_opt e
222 @@elm_json_opt[e.class.to_s]
226 self.panel_elements.map {|e|
227 #(-_-;)<... kore wa hidoi
228 JSON.parse e.to_json({:include => Panel.elm_json_opt(e)})
232 def panel_elements_as_json
233 self.to_json({:include => {:author => {}}, :methods => :elements})
236 def self.list_as_json_text ary
237 '[' + ary.map {|i| i.panel_elements_as_json }.join(',') + ']'
241 self.panel_elements.size
245 self.panel_elements.size + 1
249 panel_elements.map { |e|
255 panel_elements.map { |e|
260 def licensed_pictures
262 ((self.panel_pictures || []) + (self.ground_pictures || [])).compact.each do |elm|
263 r[elm.picture_id] = elm.picture unless r[elm.picture_id]
268 def self.visible_count
272 def self.collect_element_value elements, name
275 if o['_destroy'] or o[:_destroy]
284 def self.validate_serial ary, offset = 0
286 ary.compact.sort.each do |n|
287 break false unless n == i
290 ary.compact.size == i - offset
293 def self.validate_element_serial elements, name, offset = 0
294 Panel.validate_serial(Panel.collect_element_value(elements, name), offset)
297 def self.validate_elements_serial c
299 Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false
303 def validate_serial_list
305 {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :t, :offset => 0},
306 {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :z, :offset => 1}
310 # r1 = Panel.validate_elements_id validate_id_list
311 Panel.validate_elements_serial validate_serial_list
316 self.errors.add :base, I18n.t('errors.invalid_json')
319 self.attributes = attr
324 unless validate_child
326 self.errors.add :base, I18n.t('errors.invalid_t')
327 raise ActiveRecord::Rollback
333 def remove_element target, au
336 panel_attributes = {}
337 self.panel_elements.each do |elm|
338 attr = elm.attributes
340 attr['_destroy'] = true
348 panel_attributes[elm.class.to_s.tableize + '_attributes'] ||= {}
349 panel_attributes[elm.class.to_s.tableize + '_attributes'][elm.id] = attr
351 self.store(panel_attributes, au)
354 def destroy_with_elements
357 self.parts_element.each do |element|
358 raise ActiveRecord::Rollback unless element.destroy
360 raise ActiveRecord::Rollback unless self.destroy
367 attr = self.copy_attributes
368 attr.merge! PanelPicture.panelize(self.panel_pictures.map {|elm| elm.copy_attributes})
369 attr.merge! SpeechBalloon.panelize(self.speech_balloons.map {|elm| elm.copy_attributes})
370 attr.merge! GroundPicture.panelize(self.ground_pictures.map {|elm| elm.copy_attributes})
371 attr.merge! GroundColor.panelize(self.ground_colors.map {|elm| elm.copy_attributes})
380 r.delete 'created_at'
381 r.delete 'updated_at'
385 def self.panelize panel
386 attr = panel.attributes
393 def self.validate_id ary, pid
396 (v == pid or v == nil) ? nil : false
403 def self.validate_element_id elements, name, pid
404 Panel.validate_id(Panel.collect_element_value(elements, name), pid)
407 def self.validate_elements_id c
409 Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false
414 r = self.speech_balloons.map {|sb|
415 {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id}
417 r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id})