OSDN Git Service

t#31715:speech_balloon boost
[pettanr/pettanr.git] / app / models / speech.rb
index dbcbad8..9bf34cd 100644 (file)
@@ -1,17 +1,20 @@
 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', 'settings'].each do |a|
+    ['content', 'quotes', 'settings'].each do |a|
       next if attributes[a] == nil
       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
     end
@@ -34,6 +37,26 @@ 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
@@ -43,11 +66,16 @@ class Speech < ActiveRecord::Base
   end
   
   def tag_panel_id
-    self.speech_balloon.panel.new_record? ? '0' : self.speech_balloon.panel.id.to_s
+    self.get_panel_id.to_s
   end
   
   def tag_element_id
-    self.speech_balloon.new_record? ? '0' : self.id.to_s
+    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
@@ -62,8 +90,12 @@ class Speech < ActiveRecord::Base
     'speech'
   end
   
-  def field_tree f
-    'panels-' + self.tag_panel_id + '-speech_balloons_attributes-' + self.tag_element_id + '-speech_attributes-' + f.to_s
+  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
@@ -119,12 +151,33 @@ class Speech < ActiveRecord::Base
     {: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
-    ERB::Util.html_escape(self.content)
+    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