OSDN Git Service

element
[pettanr/pettanr.git] / app / models / speech_balloon.rb
index 161f89e..7f9507a 100644 (file)
-class SpeechBalloon < ActiveRecord::Base
-  has_many :balloons, :dependent => :destroy
-  has_many :speeches, :dependent => :destroy
+class SpeechBalloon < Peta::Element
+  load_manifest
+  include ElementInspire
+  has_one :balloon, :dependent => :destroy
+  has_one :speech, :dependent => :destroy
   belongs_to :speech_balloon_template
   belongs_to :panel
   
-  accepts_nested_attributes_for :balloons
-  accepts_nested_attributes_for :speeches
+  accepts_nested_attributes_for :balloon
+  accepts_nested_attributes_for :speech
   
-  validates :panel_id, :presence => true, :numericality => true, :existence => true
-  validates :speech_balloon_template_id, :presence => true, :numericality => true, :existence => true
+  validates :panel_id, :numericality => {:allow_blank => true}
+  validates :speech_balloon_template_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :classname, :presence => true, :length => {:maximum => 50}
   validates :z, :presence => true, :numericality => {:greater_than => 0}
   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
-#  validates :settings, :presence => true
+  validates :settings, :extend_speech_balloon => true
   
-  before_create :supply_t
+  def self.list_opt_for_panel
+    {
+      :speech_balloons => {:balloon => {}, :speech => {}}
+    }
+  end
+  
+  def self.show_opt_for_panel
+    {
+      :speech_balloons => {:balloon => {}, :speech => {}}
+    }
+  end
+  
+  def self.json_opt_for_panel
+    {
+      :balloon => {}, :speech => {}
+    }
+  end
+  
+  def self.has_picture?
+    false
+  end
+  
+  def extend_column
+    'classname'
+  end
+  
+  def supply_default
+    if self.panel
+      self.t = self.panel.new_t 
+      self.z = self.panel.new_z 
+    end
+  end
+  
+  def overwrite pid
+    self.panel_id = pid
+  end
+  
+  def visible? operators
+    return false unless super
+    self.owner_model.visible? operators
+  end
+  
+  def symbol_option
+    self.speech_balloon_template.symbol_option
+  end
+  
+  def new_balloon
+    @new_balloon
+  end
+  
+  def new_balloon= v
+    @new_balloon = v
+  end
+  
+  def get_balloon
+    self.balloon || @new_balloon
+  end
+  
+  def new_speech
+    @new_speech
+  end
+  
+  def new_speech= v
+    @new_speech = v
+  end
+  
+  def get_speech
+    self.speech || @new_speech
+  end
+  
+  def boost
+    self.extend self.speech_balloon_template.engine_speech_balloon_module
+    if self.balloon
+    else
+      self.new_balloon = self.build_balloon 
+      self.new_speech = self.build_speech 
+    end
+    self.balloon.extend self.speech_balloon_template.engine_balloon_module
+    self.speech.extend self.speech_balloon_template.engine_speech_module
+    self.balloon.new_parent = self
+    self.speech.new_parent = self
+    self.extend_speech_balloon = if self.settings.blank?
+      {}
+    else
+      JSON.parse(self.settings)
+    end
+    self.balloon.extend_balloon = if self.balloon.settings.blank?
+      {}
+    else
+      JSON.parse(self.get_balloon.settings)
+    end
+    self.speech.extend_speech = if self.speech.settings.blank?
+      {}
+    else
+      JSON.parse(self.get_speech.settings)
+    end
+  end
+  
+  def self.fold_extend_settings params
+    speech_balloon_settings = params[:speech_balloon][:settings]
+    if speech_balloon_settings.is_a?(Hash)
+      params[:speech_balloon][:settings] = speech_balloon_settings.to_json
+    end
+    balloon_settings = params[:speech_balloon][:balloon_attributes][:settings]
+    if balloon_settings.is_a?(Hash)
+      params[:speech_balloon][:balloon_attributes][:settings] = balloon_settings.to_json
+    end
+    speech_settings = params[:speech_balloon][:speech_attributes][:settings]
+    if speech_settings.is_a?(Hash)
+      params[:speech_balloon][:speech_attributes][:settings] = speech_settings.to_json
+    end
+  end
+  
+  def tag_element_type
+    'speech_balloon'
+  end
+  
+  def self.list_where
+    'panels.publish > 0'
+  end
+  
+  def self.list_order
+    'speech_balloons.updated_at desc'
+  end
+  
+  def self.list_opt
+    {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }
+  end
+  
+  def self.list_json_opt
+    {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
+  end
+  
+  def self.show_opt
+    {:include => {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
+  end
+  
+  def self.show_json_opt
+    {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
+  end
+  
+  def store operators, attr
+    if self.new_record?
+      sb = self.panel.speech_balloons.build
+      sb.attributes = attr
+    else
+      self.panel.speech_balloons.each do |speech_balloon|
+        next unless speech_balloon == self
+        attr.delete 'id'
+        speech_balloon.attributes = attr
+        break
+      end
+    end
+    self.panel.store({}, operators)
+  end
+  
+  def remove au
+    self.panel.remove_element(self, au)
+  end
+  
+  def parts
+    @parts ||= [self.balloon, self.speech]
+  end
+  
+  def engine_path_name 
+    self.speech_balloon_template.engine_name + '/'
+  end
+  
+  def path_name with_engine = false
+    (with_engine ? self.engine_path_name : '') + 
+      self.class.path_name
+  end
+  
+  def form_template with_engine = false
+    self.path_name(true) + '/form'
+  end
+  
+  def speech_form_template with_engine = false
+    self.engine_path_name + 'speeches/form'
+  end
+  
+  def balloon_form_template with_engine = false
+    self.engine_path_name + 'balloons/form'
+  end
+  
+  def scenario_template with_engine = false
+    self.path_name(true) + '/scenario'
+  end
+  
+  def element_face_template with_engine = false
+    self.path_name(false) + '/element_face'
+  end
+  
+  def scenario
+    ERB::Util.html_escape(self.caption.to_s) + self.balloon.scenario.to_s + 
+    self.speech.scenario.to_s
+  end
   
-  def supply_t
-    self.t = SpeechBalloon.maximum(:t).to_i + 1
+  def plain_scenario
+    self.caption.to_s + self.balloon.plain_scenario.to_s + 
+    self.speech.plain_scenario.to_s
   end
   
 end