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 get_speech_balloon
45 if self.get_speech_balloon == nil or self.get_speech_balloon.panel == nil or self.get_speech_balloon.panel.new_record?
48 self.get_speech_balloon.panel.id
53 'panel' + tag_panel_id + tag_element_type + tag_element_id + tag_element_part_type + tag_element_part_id + c.to_s
61 self.get_panel_id.to_s
65 r = if self.get_speech_balloon == nil or self.get_speech_balloon.new_record?
68 self.get_speech_balloon.id
77 def tag_element_part_id
78 self.new_record? ? '0' : self.id.to_s
81 def tag_element_part_type
86 'panels-' + self.tag_panel_id + '-speech_balloons_attributes-' + self.tag_element_id + '-speech_attributes-' + f.to_s
89 def self.default_page_size
93 def self.max_page_size
97 def self.page prm = nil
103 def self.page_size prm = self.default_page_size
105 page_size = self.max_page_size if page_size > self.max_page_size
106 page_size = self.default_page_size if page_size < 1
110 def self.list page = 1, page_size = self.default_page_size
112 opt.merge!(Speech.list_opt)
113 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
114 opt.merge!({:conditions => 'panels.publish > 0', :order => 'speeches.updated_at desc'})
115 Speech.find(:all, opt)
119 {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }}
122 def self.list_json_opt
123 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
126 def self.show cid, au
128 opt.merge!(Speech.show_opt)
129 res = Speech.find(cid, opt)
130 raise ActiveRecord::Forbidden unless res.visible?(au)
135 {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
138 def self.show_json_opt
139 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
145 r.delete 'speech_balloon_id'
146 r.delete 'created_at'
147 r.delete 'updated_at'
151 def self.panelize speech_attributes
152 {'speech_attributes' => speech_attributes}
155 def writing_format_engine_model
156 self.writing_format.engine_model
160 self.writing_format_engine_model.render self.content
168 ERB::Util.html_escape(self.content)