1 class Speech < ActiveRecord::Base
2 belongs_to :speech_balloon
3 belongs_to :writing_format
5 validates :speech_balloon_id, :numericality => {:allow_blank => true}
6 validates :writing_format_id, :presence => true, :numericality => true, :existence => {:both => false}
7 validates :x, :presence => true, :numericality => true
8 validates :y, :presence => true, :numericality => true
9 validates :width, :presence => true, :numericality => true, :natural_number => true
10 validates :height, :presence => true, :numericality => true, :natural_number => true
11 validates :quotes, :length => {:maximum => 15}, :quotes_even => true
12 # validates :settings, :presence => true
14 before_validation :valid_encode
17 ['content', 'quotes', 'settings'].each do |a|
18 next if attributes[a] == nil
19 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
31 if MagicNumber['run_mode'] == 0
32 return false unless guest_role_check(roles)
34 return false unless reader_role_check(roles)
36 return true if self.speech_balloon.panel.own?(roles)
37 self.speech_balloon.panel.visible? roles
40 def new_speech_balloon
44 def new_speech_balloon= v
45 @new_speech_balloon = v
48 def get_speech_balloon
49 self.speech_balloon || @new_speech_balloon
53 if self.get_speech_balloon == nil or self.get_speech_balloon.panel == nil or self.get_speech_balloon.panel.new_record?
56 self.get_speech_balloon.panel.id
61 'panel' + tag_panel_id + tag_element_type + tag_element_id + tag_element_part_type + tag_element_part_id + c.to_s
69 self.get_panel_id.to_s
73 r = if self.get_speech_balloon == nil or self.get_speech_balloon.new_record?
76 self.get_speech_balloon.id
85 def tag_element_part_id
86 self.new_record? ? '0' : self.id.to_s
89 def tag_element_part_type
94 if self.get_speech_balloon == nil
97 self.get_speech_balloon.tag_new_index
101 def self.default_page_size
105 def self.max_page_size
109 def self.page prm = nil
115 def self.page_size prm = self.default_page_size
117 page_size = self.max_page_size if page_size > self.max_page_size
118 page_size = self.default_page_size if page_size < 1
122 def self.list page = 1, page_size = self.default_page_size
124 opt.merge!(Speech.list_opt)
125 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
126 opt.merge!({:conditions => 'panels.publish > 0', :order => 'speeches.updated_at desc'})
127 Speech.find(:all, opt)
131 {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }}
134 def self.list_json_opt
135 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
138 def self.show cid, au
140 opt.merge!(Speech.show_opt)
141 res = Speech.find(cid, opt)
142 raise ActiveRecord::Forbidden unless res.visible?(au)
147 {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
150 def self.show_json_opt
151 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
157 r.delete 'speech_balloon_id'
158 r.delete 'created_at'
159 r.delete 'updated_at'
163 def self.panelize speech_attributes
164 {'speech_attributes' => speech_attributes}
167 def writing_format_engine_model
168 self.writing_format.engine_model
172 self.writing_format_engine_model.render self.content
180 ERB::Util.html_escape(self.content)