1 class SpeechBalloon < ActiveRecord::Base
4 has_one :balloon, :dependent => :destroy
5 has_one :speech, :dependent => :destroy
6 belongs_to :speech_balloon_template
9 accepts_nested_attributes_for :balloon
10 accepts_nested_attributes_for :speech
12 validates :panel_id, :numericality => {:allow_blank => true}
13 validates :speech_balloon_template_id, :presence => true, :numericality => true, :existence => {:both => false}
14 validates :classname, :presence => true, :length => {:maximum => 50}
15 validates :z, :presence => true, :numericality => {:greater_than => 0}
16 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
17 validates :settings, :extend_speech_balloon => true
19 before_validation :valid_encode
22 ['caption', 'classname', 'settings'].each do |a|
23 next if attributes[a] == nil
24 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
28 def self.list_opt_for_panel
30 :speech_balloons => {:balloon => {}, :speech => {}}
34 def self.show_opt_for_panel
36 :speech_balloons => {:balloon => {}, :speech => {}}
40 def self.json_opt_for_panel
42 :balloon => {}, :speech => {}
56 self.t = self.panel.new_t
57 self.z = self.panel.new_z
66 if MagicNumber['run_mode'] == 0
67 return false unless guest_role_check(roles)
69 return false unless reader_role_check(roles)
71 return true if self.panel.own?(roles)
72 self.panel.visible? roles
84 self.balloon || @new_balloon
96 self.speech || @new_speech
100 self.extend self.speech_balloon_template.engine_speech_balloon_module
103 self.new_balloon = self.build_balloon
104 self.new_speech = self.build_speech
106 self.balloon.extend self.speech_balloon_template.engine_balloon_module
107 self.speech.extend self.speech_balloon_template.engine_speech_module
108 self.balloon.new_parent = self
109 self.speech.new_parent = self
110 self.extend_speech_balloon = if self.settings.blank?
113 JSON.parse(self.settings)
115 self.balloon.extend_balloon = if self.balloon.settings.blank?
118 JSON.parse(self.get_balloon.settings)
120 self.speech.extend_speech = if self.speech.settings.blank?
123 JSON.parse(self.get_speech.settings)
127 def self.fold_extend_settings params
128 speech_balloon_settings = params[:speech_balloon][:settings]
129 if speech_balloon_settings.is_a?(Hash)
130 params[:speech_balloon][:settings] = speech_balloon_settings.to_json
132 balloon_settings = params[:speech_balloon][:balloon_attributes][:settings]
133 if balloon_settings.is_a?(Hash)
134 params[:speech_balloon][:balloon_attributes][:settings] = balloon_settings.to_json
136 speech_settings = params[:speech_balloon][:speech_attributes][:settings]
137 if speech_settings.is_a?(Hash)
138 params[:speech_balloon][:speech_attributes][:settings] = speech_settings.to_json
146 def self.default_page_size
150 def self.max_page_size
154 def self.page prm = nil
160 def self.page_size prm = self.default_page_size
162 page_size = self.max_page_size if page_size > self.max_page_size
163 page_size = self.default_page_size if page_size < 1
171 def self.mylist_where au
172 ['panels.author_id = ?', au.id]
175 def self.himlist_where au
176 ['panels.author_id = ? and panels.publish > 0', au.id]
179 def self.list page = 1, page_size = self.default_page_size
180 SpeechBalloon.where(self.list_where()).includes(SpeechBalloon.list_opt).order('speech_balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
183 def self.mylist au, page = 1, page_size = Author.default_speech_balloon_page_size
184 SpeechBalloon.where(self.mylist_where(au)).includes(SpeechBalloon.list_opt).order('speech_balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
187 def self.himlist au, page = 1, page_size = Author.default_speech_balloon_page_size
188 SpeechBalloon.where(self.himlist_where(au)).includes(SpeechBalloon.list_opt).order('speech_balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
191 def self.list_paginate page = 1, page_size = self.default_page_size
192 Kaminari.paginate_array(Array.new(SpeechBalloon.where(self.list_where()).includes(SpeechBalloon.list_opt).count, nil)).page(page).per(page_size)
195 def self.mylist_paginate au, page = 1, page_size = Author.default_speech_balloon_page_size
196 Kaminari.paginate_array(Array.new(SpeechBalloon.where(self.mylist_where(au)).includes(SpeechBalloon.list_opt).count, nil)).page(page).per(page_size)
199 def self.himlist_paginate au, page = 1, page_size = Author.default_speech_balloon_page_size
200 Kaminari.paginate_array(Array.new(SpeechBalloon.where(self.himlist_where(au)).includes(SpeechBalloon.list_opt).count, nil)).page(page).per(page_size)
204 {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }
207 def self.list_json_opt
208 {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
211 def self.show cid, au
213 opt.merge!(SpeechBalloon.show_opt)
214 res = SpeechBalloon.find(cid, opt)
215 raise ActiveRecord::Forbidden unless res.visible?(au)
220 {:include => {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
223 def self.show_json_opt
224 {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
229 sb = self.panel.speech_balloons.build
232 self.panel.speech_balloons.each do |speech_balloon|
233 next unless speech_balloon == self
235 speech_balloon.attributes = attr
239 self.panel.store({}, au)
243 self.panel.remove_element(self, au)
247 @parts ||= [self.balloon, self.speech]
251 self.speech_balloon_template.engine_name + '/'
254 def path_name with_engine = false
255 (with_engine ? self.engine_path_name : '') +
259 def form_template with_engine = false
260 self.path_name(true) + '/form'
263 def speech_form_template with_engine = false
264 self.engine_path_name + 'speeches/form'
267 def balloon_form_template with_engine = false
268 self.engine_path_name + 'balloons/form'
271 def scenario_template with_engine = false
272 self.path_name(true) + '/scenario'
275 def element_face_template with_engine = false
276 self.path_name(false) + '/element_face'
280 ERB::Util.html_escape(self.caption.to_s) + self.balloon.scenario.to_s +
281 self.speech.scenario.to_s
285 self.caption.to_s + self.balloon.plain_scenario.to_s +
286 self.speech.plain_scenario.to_s