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?
29 def self.colum_structures
30 @@colum_structures ||= {
32 :helper => 'panels/color_helper'
45 if MagicNumber['run_mode'] == 0
46 return false unless guest_role_check(roles)
48 return false unless reader_role_check(roles)
50 return true if self.speech_balloon.panel.own?(roles)
51 self.speech_balloon.panel.visible? roles
55 self.get_parent.speech_balloon_template.symbol_option
59 self.speech_balloon || @new_parent
62 def tag_element_part_type
67 @@text_align_texts[self.text_align]
70 def self.default_page_size
74 def self.max_page_size
78 def self.page prm = nil
84 def self.page_size prm = self.default_page_size
86 page_size = self.max_page_size if page_size > self.max_page_size
87 page_size = self.default_page_size if page_size < 1
95 def self.list page = 1, page_size = self.default_page_size
96 Speech.where(self.list_where()).includes(Speech.list_opt).order('speeches.updated_at desc').offset((page -1) * page_size).limit(page_size)
99 def self.list_paginate page = 1, page_size = self.default_page_size
100 Kaminari.paginate_array(Array.new(Speech.where(self.list_where()).includes(Speech.list_opt).count, nil)).page(page).per(page_size)
103 def self.list_by_speech_balloon_where speech_balloon_id
104 ['speeches.speech_balloon_id = ?', speech_balloon_id]
107 def self.list_by_speech_balloon speech_balloon_id, roles, page = 1, page_size = self.default_page_size
108 self.where(self.list_by_speech_balloon_where(speech_balloon_id)).includes(self.list_opt).order('speeches.updated_at desc').offset((page -1) * page_size).limit(page_size)
111 def self.list_by_writing_format_where writing_format_id
112 ['speeches.writing_format_id = ?', writing_format_id]
115 def self.list_by_writing_format writing_format_id, roles, page = 1, page_size = self.default_page_size
116 self.where(self.list_by_writing_format_where(writing_format_id)).includes(self.list_opt).order('speeches.updated_at desc').offset((page -1) * page_size).limit(page_size)
120 {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }
123 def self.list_json_opt
124 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
127 def self.show cid, au
129 opt.merge!(Speech.show_opt)
130 res = Speech.find(cid, opt)
131 raise ActiveRecord::Forbidden unless res.visible?(au)
136 {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
139 def self.show_json_opt
140 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
146 r.delete 'speech_balloon_id'
147 r.delete 'created_at'
148 r.delete 'updated_at'
152 def self.panelize speech_attributes
153 {'speech_attributes' => speech_attributes}
156 def writing_format_engine_model
157 self.writing_format.engine_model
161 self.writing_format_engine_model.render self.content
169 ERB::Util.html_escape(self.content)