OSDN Git Service

t#31896:
[pettanr/pettanr.git] / app / models / speech_balloon.rb
1 class SpeechBalloon < ActiveRecord::Base
2   include Element
3   include ElementInspire
4   has_one :balloon, :dependent => :destroy
5   has_one :speech, :dependent => :destroy
6   belongs_to :speech_balloon_template
7   belongs_to :panel
8   
9   accepts_nested_attributes_for :balloon
10   accepts_nested_attributes_for :speech
11   
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
18   
19   before_validation :valid_encode
20   
21   def valid_encode
22     ['caption', 'classname', 'settings'].each do |a|
23       next if attributes[a] == nil
24       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
25     end
26   end
27   
28   def self.list_opt_for_panel
29     {
30       :speech_balloons => {:balloon => {}, :speech => {}}
31     }
32   end
33   
34   def self.show_opt_for_panel
35     {
36       :speech_balloons => {:balloon => {}, :speech => {}}
37     }
38   end
39   
40   def self.json_opt_for_panel
41     {
42       :balloon => {}, :speech => {}
43     }
44   end
45   
46   def self.has_picture?
47     false
48   end
49   
50   def extend_column
51     'classname'
52   end
53   
54   def supply_default
55     if self.panel
56       self.t = self.panel.new_t 
57       self.z = self.panel.new_z 
58     end
59   end
60   
61   def overwrite pid
62     self.panel_id = pid
63   end
64   
65   def visible? roles
66     if MagicNumber['run_mode'] == 0
67       return false unless guest_role_check(roles)
68     else
69       return false unless reader_role_check(roles)
70     end
71     return true if self.panel.own?(roles)
72     self.panel.visible? roles
73   end
74   
75   def new_balloon
76     @new_balloon
77   end
78   
79   def new_balloon= v
80     @new_balloon = v
81   end
82   
83   def get_balloon
84     self.balloon || @new_balloon
85   end
86   
87   def new_speech
88     @new_speech
89   end
90   
91   def new_speech= v
92     @new_speech = v
93   end
94   
95   def get_speech
96     self.speech || @new_speech
97   end
98   
99   def boost
100     self.extend self.speech_balloon_template.engine_speech_balloon_module
101     if self.balloon
102     else
103       self.new_balloon = self.build_balloon 
104       self.new_speech = self.build_speech 
105     end
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?
111       {}
112     else
113       JSON.parse(self.settings)
114     end
115     self.balloon.extend_balloon = if self.balloon.settings.blank?
116       {}
117     else
118       JSON.parse(self.get_balloon.settings)
119     end
120     self.speech.extend_speech = if self.speech.settings.blank?
121       {}
122     else
123       JSON.parse(self.get_speech.settings)
124     end
125   end
126   
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
131     end
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
135     end
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
139     end
140   end
141   
142   def tag_element_type
143     'speech_balloon'
144   end
145   
146   def self.default_page_size
147     25
148   end
149   
150   def self.max_page_size
151     100
152   end
153   
154   def self.page prm = nil
155     page = prm.to_i
156     page = 1 if page < 1
157     page
158   end
159   
160   def self.page_size prm = self.default_page_size
161     page_size = prm.to_i
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
164     page_size
165   end
166   
167   def self.list_where
168     'panels.publish > 0'
169   end
170   
171   def self.mylist_where au
172     ['panels.author_id = ?', au.id]
173   end
174   
175   def self.himlist_where au
176     ['panels.author_id = ? and panels.publish > 0', au.id]
177   end
178   
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)
181   end
182   
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)
185   end
186   
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)
189   end
190   
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)
193   end
194   
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)
197   end
198   
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)
201   end
202   
203   def self.list_opt
204     {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }
205   end
206   
207   def self.list_json_opt
208     {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
209   end
210   
211   def self.show cid, au
212     opt = {}
213     opt.merge!(SpeechBalloon.show_opt)
214     res = SpeechBalloon.find(cid, opt)
215     raise ActiveRecord::Forbidden unless res.visible?(au)
216     res
217   end
218   
219   def self.show_opt
220     {:include => {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
221   end
222   
223   def self.show_json_opt
224     {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
225   end
226   
227   def store au, attr
228     if self.new_record?
229       sb = self.panel.speech_balloons.build
230       sb.attributes = attr
231     else
232       self.panel.speech_balloons.each do |speech_balloon|
233         next unless speech_balloon == self
234         attr.delete 'id'
235         speech_balloon.attributes = attr
236         break
237       end
238     end
239     self.panel.store({}, au)
240   end
241   
242   def remove au
243     self.panel.remove_element(self, au)
244   end
245   
246   def parts
247     @parts ||= [self.balloon, self.speech]
248   end
249   
250   def engine_path_name 
251     self.speech_balloon_template.engine_name + '/'
252   end
253   
254   def path_name with_engine = false
255     (with_engine ? self.engine_path_name : '') + 
256       self.class.path_name
257   end
258   
259   def form_template with_engine = false
260     self.path_name(true) + '/form'
261   end
262   
263   def speech_form_template with_engine = false
264     self.engine_path_name + 'speeches/form'
265   end
266   
267   def balloon_form_template with_engine = false
268     self.engine_path_name + 'balloons/form'
269   end
270   
271   def scenario_template with_engine = false
272     self.path_name(true) + '/scenario'
273   end
274   
275   def element_face_template with_engine = false
276     self.path_name(false) + '/element_face'
277   end
278   
279   def scenario
280     ERB::Util.html_escape(self.caption.to_s) + self.balloon.scenario.to_s + 
281     self.speech.scenario.to_s
282   end
283   
284   def plain_scenario
285     self.caption.to_s + self.balloon.plain_scenario.to_s + 
286     self.speech.plain_scenario.to_s
287   end
288   
289 end