OSDN Git Service

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