OSDN Git Service

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