OSDN Git Service

m
[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 symbol_option
76     self.speech_balloon_template.symbol_option
77   end
78   
79   def new_balloon
80     @new_balloon
81   end
82   
83   def new_balloon= v
84     @new_balloon = v
85   end
86   
87   def get_balloon
88     self.balloon || @new_balloon
89   end
90   
91   def new_speech
92     @new_speech
93   end
94   
95   def new_speech= v
96     @new_speech = v
97   end
98   
99   def get_speech
100     self.speech || @new_speech
101   end
102   
103   def boost
104     self.extend self.speech_balloon_template.engine_speech_balloon_module
105     if self.balloon
106     else
107       self.new_balloon = self.build_balloon 
108       self.new_speech = self.build_speech 
109     end
110     self.balloon.extend self.speech_balloon_template.engine_balloon_module
111     self.speech.extend self.speech_balloon_template.engine_speech_module
112     self.balloon.new_parent = self
113     self.speech.new_parent = self
114     self.extend_speech_balloon = if self.settings.blank?
115       {}
116     else
117       JSON.parse(self.settings)
118     end
119     self.balloon.extend_balloon = if self.balloon.settings.blank?
120       {}
121     else
122       JSON.parse(self.get_balloon.settings)
123     end
124     self.speech.extend_speech = if self.speech.settings.blank?
125       {}
126     else
127       JSON.parse(self.get_speech.settings)
128     end
129   end
130   
131   def self.fold_extend_settings params
132     speech_balloon_settings = params[:speech_balloon][:settings]
133     if speech_balloon_settings.is_a?(Hash)
134       params[:speech_balloon][:settings] = speech_balloon_settings.to_json
135     end
136     balloon_settings = params[:speech_balloon][:balloon_attributes][:settings]
137     if balloon_settings.is_a?(Hash)
138       params[:speech_balloon][:balloon_attributes][:settings] = balloon_settings.to_json
139     end
140     speech_settings = params[:speech_balloon][:speech_attributes][:settings]
141     if speech_settings.is_a?(Hash)
142       params[:speech_balloon][:speech_attributes][:settings] = speech_settings.to_json
143     end
144   end
145   
146   def tag_element_type
147     'speech_balloon'
148   end
149   
150   def self.default_page_size
151     25
152   end
153   
154   def self.max_page_size
155     100
156   end
157   
158   def self.page prm = nil
159     page = prm.to_i
160     page = 1 if page < 1
161     page
162   end
163   
164   def self.page_size prm = self.default_page_size
165     page_size = prm.to_i
166     page_size = self.max_page_size if page_size > self.max_page_size
167     page_size = self.default_page_size if page_size < 1
168     page_size
169   end
170   
171   def self.list_where
172     'panels.publish > 0'
173   end
174   
175   def self.mylist_where au
176     ['panels.author_id = ?', au.id]
177   end
178   
179   def self.himlist_where au
180     ['panels.author_id = ? and panels.publish > 0', au.id]
181   end
182   
183   def self.list page = 1, page_size = self.default_page_size
184     SpeechBalloon.where(self.list_where()).includes(SpeechBalloon.list_opt).order('speech_balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
185   end
186   
187   def self.mylist au, page = 1, page_size = Author.default_speech_balloon_page_size
188     SpeechBalloon.where(self.mylist_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.himlist au, page = 1, page_size = Author.default_speech_balloon_page_size
192     SpeechBalloon.where(self.himlist_where(au)).includes(SpeechBalloon.list_opt).order('speech_balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
193   end
194   
195   def self.list_paginate page = 1, page_size = self.default_page_size
196     Kaminari.paginate_array(Array.new(SpeechBalloon.where(self.list_where()).includes(SpeechBalloon.list_opt).count, nil)).page(page).per(page_size)
197   end
198   
199   def self.mylist_paginate au, page = 1, page_size = Author.default_speech_balloon_page_size
200     Kaminari.paginate_array(Array.new(SpeechBalloon.where(self.mylist_where(au)).includes(SpeechBalloon.list_opt).count, nil)).page(page).per(page_size)
201   end
202   
203   def self.himlist_paginate au, page = 1, page_size = Author.default_speech_balloon_page_size
204     Kaminari.paginate_array(Array.new(SpeechBalloon.where(self.himlist_where(au)).includes(SpeechBalloon.list_opt).count, nil)).page(page).per(page_size)
205   end
206   
207   def self.list_opt
208     {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }
209   end
210   
211   def self.list_json_opt
212     {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
213   end
214   
215   def self.show cid, au
216     opt = {}
217     opt.merge!(SpeechBalloon.show_opt)
218     res = SpeechBalloon.find(cid, opt)
219     raise ActiveRecord::Forbidden unless res.visible?(au)
220     res
221   end
222   
223   def self.show_opt
224     {:include => {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
225   end
226   
227   def self.show_json_opt
228     {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
229   end
230   
231   def store au, attr
232     if self.new_record?
233       sb = self.panel.speech_balloons.build
234       sb.attributes = attr
235     else
236       self.panel.speech_balloons.each do |speech_balloon|
237         next unless speech_balloon == self
238         attr.delete 'id'
239         speech_balloon.attributes = attr
240         break
241       end
242     end
243     self.panel.store({}, au)
244   end
245   
246   def remove au
247     self.panel.remove_element(self, au)
248   end
249   
250   def parts
251     @parts ||= [self.balloon, self.speech]
252   end
253   
254   def engine_path_name 
255     self.speech_balloon_template.engine_name + '/'
256   end
257   
258   def path_name with_engine = false
259     (with_engine ? self.engine_path_name : '') + 
260       self.class.path_name
261   end
262   
263   def form_template with_engine = false
264     self.path_name(true) + '/form'
265   end
266   
267   def speech_form_template with_engine = false
268     self.engine_path_name + 'speeches/form'
269   end
270   
271   def balloon_form_template with_engine = false
272     self.engine_path_name + 'balloons/form'
273   end
274   
275   def scenario_template with_engine = false
276     self.path_name(true) + '/scenario'
277   end
278   
279   def element_face_template with_engine = false
280     self.path_name(false) + '/element_face'
281   end
282   
283   def scenario
284     ERB::Util.html_escape(self.caption.to_s) + self.balloon.scenario.to_s + 
285     self.speech.scenario.to_s
286   end
287   
288   def plain_scenario
289     self.caption.to_s + self.balloon.plain_scenario.to_s + 
290     self.speech.plain_scenario.to_s
291   end
292   
293 end