OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[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 :speech_balloon_template_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 :speech_balloon_template_settings, :boost => {:boost_name => :speech_balloon_template}
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 symbol_option
60     self.speech_balloon_template.symbol_option
61   end
62   
63   def tag_element_type
64     'speech_balloon'
65   end
66   
67   def self.public_list_where
68     'panels.publish > 0'
69   end
70   
71   def self.list_order
72     'speech_balloons.updated_at desc'
73   end
74   
75   def self.list_json_opt
76     {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
77   end
78   
79   def self.show_opt
80     {:include => {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
81   end
82   
83   def self.show_json_opt
84     {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
85   end
86   
87   def store operators, attr
88     if self.new_record?
89       sb = self.panel.speech_balloons.build
90       sb.attributes = attr
91     else
92       self.panel.speech_balloons.each do |speech_balloon|
93         next unless speech_balloon == self
94         attr.delete 'id'
95         speech_balloon.attributes = attr
96         break
97       end
98     end
99     self.panel.store({}, operators)
100   end
101   
102   def remove au
103     self.panel.remove_element(self, au)
104   end
105   
106   def parts
107     @parts ||= [self.balloon, self.speech]
108   end
109   
110   def engine_path_name 
111     self.speech_balloon_template.engine_name + '/'
112   end
113   
114   def path_name with_engine = false
115     (with_engine ? self.engine_path_name : '') + 
116       self.class.path_name
117   end
118   
119   def form_template with_engine = false
120     self.path_name(true) + '/form'
121   end
122   
123   def speech_form_template with_engine = false
124     self.engine_path_name + 'speeches/form'
125   end
126   
127   def balloon_form_template with_engine = false
128     self.engine_path_name + 'balloons/form'
129   end
130   
131   def scenario_template with_engine = false
132     self.path_name(true) + '/scenario'
133   end
134   
135   def element_face_template with_engine = false
136     self.path_name(false) + '/element_face'
137   end
138   
139   def scenario
140     ERB::Util.html_escape(self.caption.to_s) + self.balloon.scenario.to_s + 
141     self.speech.scenario.to_s
142   end
143   
144   def plain_scenario
145     self.caption.to_s + self.balloon.plain_scenario.to_s + 
146     self.speech.plain_scenario.to_s
147   end
148   
149 end