OSDN Git Service

t#31715:speech_balloon boost
[pettanr/pettanr.git] / app / models / speech.rb
index 1cb8403..9bf34cd 100644 (file)
@@ -1,13 +1,32 @@
 class Speech < ActiveRecord::Base
   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 :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, :presence => true
   
+  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 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)
@@ -18,6 +37,67 @@ class Speech < ActiveRecord::Base
     self.speech_balloon.panel.visible? roles
   end
   
+  def new_speech_balloon
+    @new_speech_balloon
+  end
+  
+  def new_speech_balloon= v
+    @new_speech_balloon = v
+  end
+  
+  def get_speech_balloon
+    self.speech_balloon || @new_speech_balloon
+  end
+  
+  def get_panel_id
+    if self.get_speech_balloon == nil or self.get_speech_balloon.panel == nil or self.get_speech_balloon.panel.new_record?
+      0
+    else
+      self.get_speech_balloon.panel.id
+    end
+  end
+  
+  def tag_id c = nil
+    'panel' + tag_panel_id + tag_element_type + tag_element_id + tag_element_part_type + tag_element_part_id + c.to_s
+  end
+  
+  def field_tag_id f
+    self.tag_id + f.to_s
+  end
+  
+  def tag_panel_id
+    self.get_panel_id.to_s
+  end
+  
+  def tag_element_id
+    r = if self.get_speech_balloon == nil or self.get_speech_balloon.new_record?
+      0
+    else
+      self.get_speech_balloon.id
+    end
+    r.to_s
+  end
+  
+  def tag_element_type
+    'speech_balloon'
+  end
+  
+  def tag_element_part_id
+    self.new_record? ? '0' : self.id.to_s
+  end
+  
+  def tag_element_part_type
+    'speech'
+  end
+  
+  def tag_new_index
+    if self.get_speech_balloon == nil
+      nil
+    else
+      self.get_speech_balloon.tag_new_index
+    end
+  end
+  
   def self.default_page_size
     25
   end
@@ -48,11 +128,11 @@ class Speech < ActiveRecord::Base
   end
   
   def self.list_opt
-    {:include => {:speech_balloon => {:panel => {:author => {}}, :balloons => {}, :speech_balloon_template => {}} }}
+    {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }}
   end
   
   def self.list_json_opt
-    {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloons => {}, :speech_balloon_template => {} }}}}
+    {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
   end
   
   def self.show cid, au
@@ -64,11 +144,40 @@ class Speech < ActiveRecord::Base
   end
   
   def self.show_opt
-    {:include => {:speech_balloon => {:panel => {:author => {}}, :balloons => {}, :speech_balloon_template => {} }}}
+    {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
   end
   
   def self.show_json_opt
-    {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloons => {}, :speech_balloon_template => {} }}}}
+    {: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