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 has_many :panel_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
15 accepts_nested_attributes_for :panel_colors, :allow_destroy => true
17 validates :width, :presence => true, :numericality => true, :natural_number => true
18 validates :height, :presence => true, :numericality => true, :natural_number => true
19 validates :border, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
20 validates :x, :numericality => {:allow_blank => true}
21 validates :y, :numericality => {:allow_blank => true}
22 validates :z, :numericality => {:allow_blank => true, :greater_than => 0}
23 validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
24 validates :publish, :presence => true, :numericality => true
32 self.author_id = au.id
36 return false unless au.is_a?(Author)
37 self.author_id == au.id
42 return false if MagicNumber['run_mode'] == 1
43 elsif au.is_a?(Author)
44 return true if self.own?(au)
59 def self.default_page_size
63 def self.max_page_size
67 def self.page prm = nil
73 def self.page_size prm = self.default_page_size
75 page_size = self.max_page_size if page_size > self.max_page_size
76 page_size = self.default_page_size if page_size < 1
80 def self.list page = 1, page_size = self.default_page_size
82 opt.merge!(self.list_opt)
83 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
84 opt.merge!({:conditions => 'panels.publish > 0', :order => 'panels.updated_at desc'})
91 :picture => {:artist => {}, :license => {}}
93 :speech_balloons => {:balloons => {}, :speeches => {}},
95 :picture => {:artist => {}, :license => {}}
106 def self.mylist au, page = 1, page_size = Author.default_panel_page_size
108 opt.merge!(self.list_opt)
109 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
110 opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'panels.updated_at desc'})
111 Panel.find(:all, opt)
114 def self.show rid, au
116 opt.merge!(Panel.show_opt)
117 res = Panel.find(rid, opt)
118 raise ActiveRecord::Forbidden unless res.visible?(au)
122 def self.edit rid, au
124 opt.merge!(Panel.show_opt)
125 res = Panel.find(rid, opt)
126 raise ActiveRecord::Forbidden unless res.own?(au)
133 :picture => {:artist => {}, :license => {}}
135 :speech_balloons => {:balloons => {}, :speeches => {}},
136 :ground_pictures => {
137 :picture => {:artist => {}, :license => {}}
149 ((self.panel_pictures || []) + (self.speech_balloons || [])).compact
154 self.parts_element.each do |e|
161 ((self.ground_colors || []) + (self.ground_pictures || []) + (self.panel_colors || [])).compact
170 :picture => {:artist => {}, :license => {}}
172 'SpeechBalloon' => {:balloons => {}, :speeches => {}},
174 :picture => {:artist => {}, :license => {}}
183 def self.elm_json_opt e
184 @@elm_json_opt[e.class.to_s]
188 self.panel_elements.map {|e|
189 #(-_-;)<... kore wa hidoi
190 JSON.parse e.to_json({:include => Panel.elm_json_opt(e)})
194 def panel_elements_as_json
195 self.to_json({:include => {:author => {}}, :methods => :elements})
198 def self.list_as_json_text ary
199 '[' + ary.map {|i| i.panel_elements_as_json }.join(',') + ']'
202 def licensed_pictures
204 ((self.panel_pictures || []) + (self.ground_pictures || [])).compact.each do |elm|
205 r[elm.picture_id] = elm.picture unless r[elm.picture_id]
210 def self.visible_count
214 def self.collect_element_value elements, name
222 def self.validate_t ary
224 ary.compact.sort.each do |t|
225 break false unless t == i
228 ary.compact.size == i
231 def self.validate_element_t elements, name
232 Panel.validate_t(Panel.collect_element_value(elements, name))
235 def self.validate_elements_t c
237 Panel.validate_element_t(conf[:elements], conf[:name]) ? nil : false
243 {:elements => [self.panel_pictures, self.speech_balloons], :name => :t}
247 # r1 = Panel.validate_elements_id validate_id_list
248 Panel.validate_elements_t validate_t_list
253 self.errors.add :base, I18n.t('errors.invalid_json')
256 self.attributes = attr
260 unless validate_child
261 self.errors.add :base, I18n.t('errors.invalid_t')
262 raise ActiveRecord::Rollback
269 def destroy_with_elements
272 self.panel_elements.each do |element|
273 raise ActiveRecord::Rollback unless element.destroy
275 raise ActiveRecord::Rollback unless self.destroy
282 def self.validate_id ary, pid
285 (v == pid or v == nil) ? nil : false
292 def self.validate_element_id elements, name, pid
293 Panel.validate_id(Panel.collect_element_value(elements, name), pid)
296 def self.validate_elements_id c
298 Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false
303 r = self.speech_balloons.map {|sb|
304 {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id}
306 r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id})