1 class Speech < ActiveRecord::Base
2 belongs_to :speech_balloon
4 validates :speech_balloon_id, :numericality => {:allow_blank => true}
5 validates :x, :presence => true, :numericality => true
6 validates :y, :presence => true, :numericality => true
7 validates :width, :presence => true, :numericality => true, :natural_number => true
8 validates :height, :presence => true, :numericality => true, :natural_number => true
9 # validates :settings, :presence => true
11 before_validation :valid_encode
14 ['content', 'settings'].each do |a|
15 next if attributes[a] == nil
16 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
28 if MagicNumber['run_mode'] == 0
29 return false unless guest_role_check(roles)
31 return false unless reader_role_check(roles)
33 return true if self.speech_balloon.panel.own?(roles)
34 self.speech_balloon.panel.visible? roles
37 def get_speech_balloon
42 'panel' + tag_panel_id + tag_element_type + tag_element_id + tag_element_part_type + tag_element_part_id + c.to_s
50 self.get_speech_balloon.panel.new_record? ? '0' : self.get_speech_balloon.panel.id.to_s
54 self.get_speech_balloon.new_record? ? '0' : self.get_speech_balloon.id.to_s
61 def tag_element_part_id
62 self.new_record? ? '0' : self.id.to_s
65 def tag_element_part_type
70 'panels-' + self.tag_panel_id + '-speech_balloons_attributes-' + self.tag_element_id + '-speech_attributes-' + f.to_s
73 def self.default_page_size
77 def self.max_page_size
81 def self.page prm = nil
87 def self.page_size prm = self.default_page_size
89 page_size = self.max_page_size if page_size > self.max_page_size
90 page_size = self.default_page_size if page_size < 1
94 def self.list page = 1, page_size = self.default_page_size
96 opt.merge!(Speech.list_opt)
97 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
98 opt.merge!({:conditions => 'panels.publish > 0', :order => 'speeches.updated_at desc'})
99 Speech.find(:all, opt)
103 {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }}
106 def self.list_json_opt
107 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
110 def self.show cid, au
112 opt.merge!(Speech.show_opt)
113 res = Speech.find(cid, opt)
114 raise ActiveRecord::Forbidden unless res.visible?(au)
119 {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
122 def self.show_json_opt
123 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
127 ERB::Util.html_escape(self.content)