class Speech < ActiveRecord::Base
- belongs_to :balloon
+ include ElementPart
+ belongs_to :speech_balloon
+ belongs_to :writing_format
+
+ validates :speech_balloon_id, :numericality => {:allow_blank => true}
+ validates :writing_format_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :font_size, :presence => true, :numericality => {:only_integer => false}
+ validates :text_align, :presence => true, :numericality => true, :inclusion => { :in => 0..3 }
+ validates :fore_color, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
+ validates :x, :presence => true, :numericality => true
+ validates :y, :presence => true, :numericality => true
+ validates :width, :presence => true, :numericality => true, :natural_number => true
+ validates :height, :presence => true, :numericality => true, :natural_number => true
+ validates :quotes, :length => {:maximum => 15}, :quotes_even => true
+ validates :settings, :extend_speech => true
+
+ @@text_align_texts = ['left', 'left', 'right', 'center']
+
+ before_validation :valid_encode
+
+ def valid_encode
+ ['content', 'quotes', 'settings'].each do |a|
+ next if attributes[a] == nil
+ raise Pettanr::BadRequest unless attributes[a].valid_encoding?
+ end
+ end
+
+ def self.colum_structures
+ @@colum_structures ||= {
+ :fore_color => {
+ :helper => 'panels/color_helper'
+ }
+ }
+ end
+
+ def supply_default
+ self.x = 0
+ self.y = 0
+ self.width = 100
+ self.height = 100
+ end
+
+ def visible? roles
+ if MagicNumber['run_mode'] == 0
+ return false unless guest_role_check(roles)
+ else
+ return false unless reader_role_check(roles)
+ end
+ return true if self.speech_balloon.panel.own?(roles)
+ self.speech_balloon.panel.visible? roles
+ end
+
+ def symbol_option
+ self.get_parent.speech_balloon_template.symbol_option
+ end
+
+ def get_parent
+ self.speech_balloon || @new_parent
+ end
+
+ def tag_element_part_type
+ 'speech'
+ end
+
+ def text_align_text
+ @@text_align_texts[self.text_align]
+ end
+
+ def self.default_page_size
+ 25
+ end
+
+ def self.max_page_size
+ 100
+ end
+
+ def self.page prm = nil
+ page = prm.to_i
+ page = 1 if page < 1
+ page
+ end
+
+ def self.page_size prm = self.default_page_size
+ page_size = prm.to_i
+ page_size = self.max_page_size if page_size > self.max_page_size
+ page_size = self.default_page_size if page_size < 1
+ page_size
+ end
+
+ def self.list_where
+ 'panels.publish > 0'
+ end
+
+ def self.list page = 1, page_size = self.default_page_size
+ Speech.where(self.list_where()).includes(Speech.list_opt).order('speeches.updated_at desc').offset((page -1) * page_size).limit(page_size)
+ end
+
+ def self.list_paginate page = 1, page_size = self.default_page_size
+ Kaminari.paginate_array(Array.new(Speech.where(self.list_where()).includes(Speech.list_opt).count, nil)).page(page).per(page_size)
+ end
+
+ def self.list_opt
+ {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }
+ end
+
+ def self.list_json_opt
+ {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
+ end
+
+ def self.show cid, au
+ opt = {}
+ opt.merge!(Speech.show_opt)
+ res = Speech.find(cid, opt)
+ raise ActiveRecord::Forbidden unless res.visible?(au)
+ res
+ end
+
+ def self.show_opt
+ {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
+ end
+
+ def self.show_json_opt
+ {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
+ end
+
+ def copy_attributes
+ r = self.attributes
+ r.delete 'id'
+ r.delete 'speech_balloon_id'
+ r.delete 'created_at'
+ r.delete 'updated_at'
+ r
+ end
+
+ def self.panelize speech_attributes
+ {'speech_attributes' => speech_attributes}
+ end
+
+ def writing_format_engine_model
+ self.writing_format.engine_model
+ end
+
+ def scenario
+ self.writing_format_engine_model.render self.content
+ end
+
+ def plain_scenario
+ self.content + "\n"
+ end
+
+ def feed
+ ERB::Util.html_escape(self.content)
+ end
+
end