OSDN Git Service

v07
[pettanr/pettanr.git] / app / models / speech_balloon.rb
index 4a2ab3c..367af4a 100644 (file)
@@ -1,36 +1,53 @@
 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 :balloon
-  accepts_nested_attributes_for :speech
+  accepts_nested_attributes_for :balloon, :allow_destroy => true
+  accepts_nested_attributes_for :speech, :allow_destroy => 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 :speech_balloon_template_module_name, :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, :extend_speech_balloon => true
+  validates :speech_balloon_template_settings, :boost => {:boost_name => :speech_balloon_template}
   
-  def self.list_opt_for_panel
-    {
-      :speech_balloons => {:balloon => {}, :speech => {}}
-    }
+  scope :with_panel, -> do
+    includes(:panel)
   end
   
-  def self.show_opt_for_panel
-    {
-      :speech_balloons => {:balloon => {}, :speech => {}}
-    }
+  scope :with_speech_balloon_template, -> do
+    includes(:speech_balloon_template)
   end
   
-  def self.json_opt_for_panel
+  scope :find_index, -> do
+    with_panel.where(Panel.arel_table[:publish].gt 0).references(:panel)
+  end
+  
+  scope :find_private, -> (operators) do 
+    with_panel.where(Panel.arel_table[:author_id].eq operators.author.id).references(:panel)
+  end
+  
+  scope :find_by_panel, -> (panel_id) do 
+    find_index.where(panel_id: panel_id).references(:panel)
+  end
+  
+  scope :find_by_speech_balloon_template, -> (speech_balloon_template_id) do 
+    find_index.where(speech_balloon_template_id: speech_balloon_template_id).references(:panel)
+  end
+  
+  scope :find_by_author, -> (author_id) do 
+    find_index.where(Panel.arel_table[:author_id].eq author_id).references(:panel)
+  end
+  
+  def self.by_author_list_includes
     {
-      :balloon => {}, :speech => {}
+      :panel => {
+        :author => {}
+      }
     }
   end
   
@@ -49,127 +66,22 @@ class SpeechBalloon < Peta::Element
     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 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
-  return false
-    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'
+  def filer_caption
+    self.plain_scenario
   end
   
-  def self.list_where
+  def self.public_list_where list
     'panels.publish > 0'
   end
   
-  def self.list_order
-    'speech_balloons.updated_at desc'
-  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