1 class Speech < ActiveRecord::Base
3 belongs_to :speech_balloon
4 belongs_to :writing_format
6 validates :speech_balloon_id, :numericality => {:allow_blank => true}
7 validates :writing_format_id, :presence => true, :numericality => true, :existence => {:both => false}
8 validates :font_size, :presence => true, :numericality => {:only_integer => false}
9 validates :text_align, :presence => true, :numericality => true, :inclusion => { :in => 0..3 }
10 validates :fore_color, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
11 validates :x, :presence => true, :numericality => true
12 validates :y, :presence => true, :numericality => true
13 validates :width, :presence => true, :numericality => true, :natural_number => true
14 validates :height, :presence => true, :numericality => true, :natural_number => true
15 validates :quotes, :length => {:maximum => 15}, :quotes_even => true
16 validates :settings, :extend_speech => true
18 @@text_align_texts = ['left', 'left', 'right', 'center']
20 before_validation :valid_encode
23 ['content', 'quotes', 'settings'].each do |a|
24 next if attributes[a] == nil
25 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
37 if MagicNumber['run_mode'] == 0
38 return false unless guest_role_check(roles)
40 return false unless reader_role_check(roles)
42 return true if self.speech_balloon.panel.own?(roles)
43 self.speech_balloon.panel.visible? roles
47 self.speech_balloon || @new_parent
50 def tag_element_part_type
55 @@text_align_texts[self.text_align]
58 def self.default_page_size
62 def self.max_page_size
66 def self.page prm = nil
72 def self.page_size prm = self.default_page_size
74 page_size = self.max_page_size if page_size > self.max_page_size
75 page_size = self.default_page_size if page_size < 1
79 def self.list page = 1, page_size = self.default_page_size
81 opt.merge!(Speech.list_opt)
82 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
83 opt.merge!({:conditions => 'panels.publish > 0', :order => 'speeches.updated_at desc'})
84 Speech.find(:all, opt)
88 {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }}
91 def self.list_json_opt
92 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
97 opt.merge!(Speech.show_opt)
98 res = Speech.find(cid, opt)
99 raise ActiveRecord::Forbidden unless res.visible?(au)
104 {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
107 def self.show_json_opt
108 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
114 r.delete 'speech_balloon_id'
115 r.delete 'created_at'
116 r.delete 'updated_at'
120 def self.panelize speech_attributes
121 {'speech_attributes' => speech_attributes}
124 def writing_format_engine_model
125 self.writing_format.engine_model
129 self.writing_format_engine_model.render self.content
137 ERB::Util.html_escape(self.content)