OSDN Git Service

098e0d063c2160255d5fec91c5f07f0f704244df
[pettanr/pettanr.git] / app / models / speech_balloon.rb
1 class SpeechBalloon < Peta::Element
2   load_manifest
3   has_one :balloon, :dependent => :destroy
4   has_one :speech, :dependent => :destroy
5   belongs_to :speech_balloon_template
6   belongs_to :panel
7   
8   accepts_nested_attributes_for :balloon
9   accepts_nested_attributes_for :speech
10   
11   validates :panel_id, :numericality => {:allow_blank => true}
12   validates :speech_balloon_template_id, :presence => true, :numericality => true, :existence => {:both => false}
13   validates :classname, :presence => true, :length => {:maximum => 50}
14   validates :z, :presence => true, :numericality => {:greater_than => 0}
15   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
16   validates :settings, :extend_speech_balloon => true
17   
18   def self.list_opt_for_panel
19     {
20       :speech_balloons => {:balloon => {}, :speech => {}}
21     }
22   end
23   
24   def self.show_opt_for_panel
25     {
26       :speech_balloons => {:balloon => {}, :speech => {}}
27     }
28   end
29   
30   def self.json_opt_for_panel
31     {
32       :balloon => {}, :speech => {}
33     }
34   end
35   
36   def self.has_picture?
37     false
38   end
39   
40   def supply_default
41     if self.panel
42       self.t = self.panel.new_t 
43       self.z = self.panel.new_z 
44     end
45   end
46   
47   def overwrite pid
48     self.panel_id = pid
49   end
50   
51   def visible? operators
52     return false unless super
53     self.owner_model.visible? operators
54   end
55   
56   def symbol_option
57     self.speech_balloon_template.symbol_option
58   end
59   
60   def boost
61     self.extend self.speech_balloon_template.engine_speech_balloon_module
62     if self.balloon
63     else
64       self.new_balloon = self.build_balloon 
65       self.new_speech = self.build_speech 
66     end
67     self.balloon.extend self.speech_balloon_template.engine_balloon_module
68     self.speech.extend self.speech_balloon_template.engine_speech_module
69   return false
70     self.balloon.new_parent = self
71     self.speech.new_parent = self
72     self.extend_speech_balloon = if self.settings.blank?
73       {}
74     else
75       JSON.parse(self.settings)
76     end
77     self.balloon.extend_balloon = if self.balloon.settings.blank?
78       {}
79     else
80       JSON.parse(self.get_balloon.settings)
81     end
82     self.speech.extend_speech = if self.speech.settings.blank?
83       {}
84     else
85       JSON.parse(self.get_speech.settings)
86     end
87   end
88   
89   def self.fold_extend_settings params
90     speech_balloon_settings = params[:speech_balloon][:settings]
91     if speech_balloon_settings.is_a?(Hash)
92       params[:speech_balloon][:settings] = speech_balloon_settings.to_json
93     end
94     balloon_settings = params[:speech_balloon][:balloon_attributes][:settings]
95     if balloon_settings.is_a?(Hash)
96       params[:speech_balloon][:balloon_attributes][:settings] = balloon_settings.to_json
97     end
98     speech_settings = params[:speech_balloon][:speech_attributes][:settings]
99     if speech_settings.is_a?(Hash)
100       params[:speech_balloon][:speech_attributes][:settings] = speech_settings.to_json
101     end
102   end
103   
104   def tag_element_type
105     'speech_balloon'
106   end
107   
108   def self.list_where
109     'panels.publish > 0'
110   end
111   
112   def self.list_order
113     'speech_balloons.updated_at desc'
114   end
115   
116   def self.list_json_opt
117     {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
118   end
119   
120   def self.show_opt
121     {:include => {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
122   end
123   
124   def self.show_json_opt
125     {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
126   end
127   
128   def store operators, attr
129     if self.new_record?
130       sb = self.panel.speech_balloons.build
131       sb.attributes = attr
132     else
133       self.panel.speech_balloons.each do |speech_balloon|
134         next unless speech_balloon == self
135         attr.delete 'id'
136         speech_balloon.attributes = attr
137         break
138       end
139     end
140     self.panel.store({}, operators)
141   end
142   
143   def remove au
144     self.panel.remove_element(self, au)
145   end
146   
147   def parts
148     @parts ||= [self.balloon, self.speech]
149   end
150   
151   def engine_path_name 
152     self.speech_balloon_template.engine_name + '/'
153   end
154   
155   def path_name with_engine = false
156     (with_engine ? self.engine_path_name : '') + 
157       self.class.path_name
158   end
159   
160   def form_template with_engine = false
161     self.path_name(true) + '/form'
162   end
163   
164   def speech_form_template with_engine = false
165     self.engine_path_name + 'speeches/form'
166   end
167   
168   def balloon_form_template with_engine = false
169     self.engine_path_name + 'balloons/form'
170   end
171   
172   def scenario_template with_engine = false
173     self.path_name(true) + '/scenario'
174   end
175   
176   def element_face_template with_engine = false
177     self.path_name(false) + '/element_face'
178   end
179   
180   def scenario
181     ERB::Util.html_escape(self.caption.to_s) + self.balloon.scenario.to_s + 
182     self.speech.scenario.to_s
183   end
184   
185   def plain_scenario
186     self.caption.to_s + self.balloon.plain_scenario.to_s + 
187     self.speech.plain_scenario.to_s
188   end
189   
190 end