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 :quotes, :length => {:maximum => 15}, :quotes_even => true
10 # validates :settings, :presence => true
12 before_validation :valid_encode
15 ['content', 'quotes', 'settings'].each do |a|
16 next if attributes[a] == nil
17 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
29 if MagicNumber['run_mode'] == 0
30 return false unless guest_role_check(roles)
32 return false unless reader_role_check(roles)
34 return true if self.speech_balloon.panel.own?(roles)
35 self.speech_balloon.panel.visible? roles
38 def get_speech_balloon
43 if self.get_speech_balloon == nil or self.get_speech_balloon.panel == nil or self.get_speech_balloon.panel.new_record?
46 self.get_speech_balloon.panel.id
51 'panel' + tag_panel_id + tag_element_type + tag_element_id + tag_element_part_type + tag_element_part_id + c.to_s
59 self.get_panel_id.to_s
63 r = if self.get_speech_balloon == nil or self.get_speech_balloon.new_record?
66 self.get_speech_balloon.panel.id
75 def tag_element_part_id
76 self.new_record? ? '0' : self.id.to_s
79 def tag_element_part_type
84 'panels-' + self.tag_panel_id + '-speech_balloons_attributes-' + self.tag_element_id + '-speech_attributes-' + f.to_s
87 def self.default_page_size
91 def self.max_page_size
95 def self.page prm = nil
101 def self.page_size prm = self.default_page_size
103 page_size = self.max_page_size if page_size > self.max_page_size
104 page_size = self.default_page_size if page_size < 1
108 def self.list page = 1, page_size = self.default_page_size
110 opt.merge!(Speech.list_opt)
111 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
112 opt.merge!({:conditions => 'panels.publish > 0', :order => 'speeches.updated_at desc'})
113 Speech.find(:all, opt)
117 {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }}
120 def self.list_json_opt
121 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
124 def self.show cid, au
126 opt.merge!(Speech.show_opt)
127 res = Speech.find(cid, opt)
128 raise ActiveRecord::Forbidden unless res.visible?(au)
133 {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
136 def self.show_json_opt
137 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
141 ERB::Util.html_escape(self.content)
149 ERB::Util.html_escape(self.content)