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
100 def tag_attributes column = nil, opt = {}
102 :id => self.field_tag_id(column), :panel_id => self.tag_panel_id
106 def select_tag_attributes(selected, column)
109 {:html => {:selected => selected}},
110 self.field_tag_attributes(column)
114 def field_tag_attributes column, opt = {}
115 self.tag_attributes(column).merge(
120 def tag_attr column = nil, opt = {}
121 self.tag_attributes(column, opt).to_attr
124 def field_tag_attr column, no_attr, opt = {}
125 self.field_tag_attributes(column, no_attr, opt).to_attr
128 def self.default_page_size
132 def self.max_page_size
136 def self.page prm = nil
142 def self.page_size prm = self.default_page_size
144 page_size = self.max_page_size if page_size > self.max_page_size
145 page_size = self.default_page_size if page_size < 1
153 def self.mylist_where au
154 ['panels.author_id = ?', au.id]
157 def self.himlist_where au
158 ['panels.author_id = ? and panels.publish > 0', au.id]
161 def self.list page = 1, page_size = self.default_page_size
162 Panel.where(self.list_where()).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
165 def self.mylist au, page = 1, page_size = Author.default_panel_page_size
166 Panel.where(self.mylist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
169 def self.himlist au, page = 1, page_size = Author.default_panel_page_size
170 Panel.where(self.himlist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
173 def self.list_paginate page = 1, page_size = self.default_page_size
174 Kaminari.paginate_array(Array.new(Panel.where(self.list_where()).count, nil)).page(page).per(page_size)
177 def self.mylist_paginate au, page = 1, page_size = Author.default_panel_page_size
178 Kaminari.paginate_array(Array.new(Panel.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
181 def self.himlist_paginate au, page = 1, page_size = Author.default_panel_page_size
182 Kaminari.paginate_array(Array.new(Panel.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
189 self.each_element_classes do |e|
190 r.merge!(e.list_opt_for_panel)
195 def self.show rid, roles
197 opt.merge!(Panel.show_opt)
198 res = Panel.find(rid, opt)
199 raise ActiveRecord::Forbidden unless res.visible?(roles)
203 def self.edit rid, au
205 opt.merge!(Panel.show_opt)
206 res = Panel.find(rid, opt)
207 raise ActiveRecord::Forbidden unless res.own?(au)
215 self.each_element_classes do |e|
216 r.merge!(e.show_opt_for_panel)
223 self.class.each_element_class_names do |k|
224 r += (self.elements_by_class_name(k) || [])
231 self.parts_element.each do |e|
239 self.parts_element.each do |e|
246 self.panel_elements.map {|e|
247 #(-_-;)<... kore wa hidoi
248 JSON.parse e.to_json({:include => e.class.json_opt_for_panel})
252 def panel_elements_as_json
253 self.to_json({:include => {:author => {}}, :methods => :elements})
256 def self.list_as_json_text ary
257 '[' + ary.map {|i| i.panel_elements_as_json }.join(',') + ']'
261 self.panel_elements.size
265 self.panel_elements.size + 1
269 panel_elements.map { |e|
275 panel_elements.map { |e|
280 def licensed_pictures
282 self.panel_elements.each do |elm|
283 next unless elm.class.has_picture?
284 r[elm.picture_id] = elm.picture unless r[elm.picture_id]
289 def self.visible_count
293 def self.collect_element_value elements, name
296 if o['_destroy'] or o[:_destroy]
305 def self.validate_serial ary, offset = 0
307 ary.compact.sort.each do |n|
308 break false unless n == i
311 ary.compact.size == i - offset
314 def self.validate_element_serial elements, name, offset = 0
315 Panel.validate_serial(Panel.collect_element_value(elements, name), offset)
318 def self.validate_elements_serial c
320 Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false
324 def validate_serial_list
326 self.class.each_element_class_names do |k|
327 l << self.elements_by_class_name(k)
330 {:elements => l, :name => :t, :offset => 0},
331 {:elements => l, :name => :z, :offset => 1}
335 # r1 = Panel.validate_elements_id validate_id_list
336 Panel.validate_elements_serial validate_serial_list
340 @new_element_index = 0
341 self.panel_elements.each do |elm|
343 elm.new_index = @new_element_index
344 @new_element_index += 1
351 self.errors.add :base, I18n.t('errors.invalid_json')
354 self.attributes = attr
358 self.panel_elements.each do |elm|
364 unless validate_child
366 self.errors.add :base, I18n.t('errors.invalid_t')
367 raise ActiveRecord::Rollback
373 def remove_element target, au
376 panel_attributes = {}
377 self.panel_elements.each do |elm|
378 attr = elm.attributes
380 attr['_destroy'] = true
388 panel_attributes[elm.class.to_s.tableize + '_attributes'] ||= {}
389 panel_attributes[elm.class.to_s.tableize + '_attributes'][elm.id] = attr
391 self.store(panel_attributes, au)
394 def destroy_with_elements
397 self.parts_element.each do |element|
398 raise ActiveRecord::Rollback unless element.destroy
400 raise ActiveRecord::Rollback unless self.destroy
407 attr = self.copy_attributes
408 Panel.each_element_class_names do |n|
409 attr.merge! Panel.class_name_to_class(n).panelize(self.elements_by_class_name(n).map {|elm| elm.copy_attributes})
418 r.delete 'created_at'
419 r.delete 'updated_at'
423 def self.panelize panel
424 attr = panel.attributes
431 def self.validate_id ary, pid
434 (v == pid or v == nil) ? nil : false
441 def self.validate_element_id elements, name, pid
442 Panel.validate_id(Panel.collect_element_value(elements, name), pid)
445 def self.validate_elements_id c
447 Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false
452 r = self.speech_balloons.map {|sb|
453 {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id}
455 r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id})