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
38 'panel' + tag_panel_id + tag_element_type + tag_element_id + tag_element_part_type + tag_element_part_id + c.to_s
46 self.speech_balloon.panel.new_record? ? '0' : self.speech_balloon.panel.id.to_s
50 self.speech_balloon.new_record? ? '0' : self.id.to_s
57 def tag_element_part_id
58 self.new_record? ? '0' : self.id.to_s
61 def tag_element_part_type
66 'panels-' + self.tag_panel_id + '-speech_balloons_attributes-' + self.tag_element_id + '-speech_attributes-' + f.to_s
69 def self.default_page_size
73 def self.max_page_size
77 def self.page prm = nil
83 def self.page_size prm = self.default_page_size
85 page_size = self.max_page_size if page_size > self.max_page_size
86 page_size = self.default_page_size if page_size < 1
90 def self.list page = 1, page_size = self.default_page_size
92 opt.merge!(Speech.list_opt)
93 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
94 opt.merge!({:conditions => 'panels.publish > 0', :order => 'speeches.updated_at desc'})
95 Speech.find(:all, opt)
99 {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }}
102 def self.list_json_opt
103 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
106 def self.show cid, au
108 opt.merge!(Speech.show_opt)
109 res = Speech.find(cid, opt)
110 raise ActiveRecord::Forbidden unless res.visible?(au)
115 {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
118 def self.show_json_opt
119 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
123 ERB::Util.html_escape(self.content)