1 class Speech < Pettanr::Item
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']
32 def self.valid_encode_columns
33 super.merge ['content', 'quotes', 'settings']
36 def self.colum_structures
37 @@colum_structures ||= {
39 :helper => 'panels/color_helper'
52 if MagicNumber['run_mode'] == 0
53 return false unless guest_role_check(roles)
55 return false unless reader_role_check(roles)
57 return true if self.speech_balloon.panel.own?(roles)
58 self.speech_balloon.panel.visible? roles
62 self.get_parent.speech_balloon_template.symbol_option
66 self.speech_balloon || @new_parent
69 def tag_element_part_type
74 @@text_align_texts[self.text_align]
77 def self.default_page_size
81 def self.max_page_size
85 def self.page prm = nil
91 def self.page_size prm = self.default_page_size
93 page_size = self.max_page_size if page_size > self.max_page_size
94 page_size = self.default_page_size if page_size < 1
102 def self.list page = 1, page_size = self.default_page_size
103 Speech.where(self.list_where()).includes(Speech.list_opt).order('speeches.updated_at desc').offset((page -1) * page_size).limit(page_size)
106 def self.list_paginate page = 1, page_size = self.default_page_size
107 Kaminari.paginate_array(Array.new(Speech.where(self.list_where()).includes(Speech.list_opt).count, nil)).page(page).per(page_size)
110 def self.list_by_speech_balloon_where speech_balloon_id
111 ['speeches.speech_balloon_id = ?', speech_balloon_id]
114 def self.list_by_speech_balloon speech_balloon_id, roles, page = 1, page_size = self.default_page_size
115 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)
118 def self.list_by_writing_format_where writing_format_id
119 ['speeches.writing_format_id = ?', writing_format_id]
122 def self.list_by_writing_format writing_format_id, roles, page = 1, page_size = self.default_page_size
123 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)
127 {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }
130 def self.list_json_opt
131 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
134 def self.show cid, au
136 opt.merge!(Speech.show_opt)
137 res = Speech.find(cid, opt)
138 raise ActiveRecord::Forbidden unless res.visible?(au)
143 {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
146 def self.show_json_opt
147 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
153 r.delete 'speech_balloon_id'
154 r.delete 'created_at'
155 r.delete 'updated_at'
159 def self.panelize speech_attributes
160 {'speech_attributes' => speech_attributes}
163 def writing_format_engine_model
164 self.writing_format.engine_model
168 self.writing_format_engine_model.render self.content
176 ERB::Util.html_escape(self.content)