OSDN Git Service

temp
[pettanr/pettanr.git] / app / models / speech_balloon.rb
index 4ff7934..1033360 100644 (file)
@@ -1,4 +1,6 @@
-class SpeechBalloon < ActiveRecord::Base
+class SpeechBalloon < Pettanr::Item
+  include Element
+  include ElementInspire
   has_one :balloon, :dependent => :destroy
   has_one :speech, :dependent => :destroy
   belongs_to :speech_balloon_template
@@ -12,25 +14,59 @@ class SpeechBalloon < ActiveRecord::Base
   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_validation :valid_encode
+  def self.singular
+    'SpeechBalloon'
+  end
   
-  def valid_encode
-    ['caption', 'classname', 'settings'].each do |a|
-      next if attributes[a] == nil
-      raise Pettanr::BadRequest unless attributes[a].valid_encoding?
-    end
+  def self.plural
+    'SpeechBalloons'
+  end
+  
+  def self.owner_type
+    :author
+  end
+  
+  def self.valid_encode_columns
+    super.merge ['caption', 'classname', 'settings']
+  end
+  
+  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
-    self.x = self.x || 0
-    self.y = self.y || 0
-    self.t = self.t || self.panel.new_t if self.panel
-    self.z = self.z || self.panel.new_z if self.panel
+    if self.panel
+      self.t = self.panel.new_t 
+      self.z = self.panel.new_z 
+    end
   end
   
-  def overwrite 
+  def overwrite pid
+    self.panel_id = pid
   end
   
   def visible? roles
@@ -43,6 +79,81 @@ class SpeechBalloon < ActiveRecord::Base
     self.panel.visible? roles
   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.default_page_size
     25
   end
@@ -64,16 +175,60 @@ class SpeechBalloon < ActiveRecord::Base
     page_size
   end
   
+  def self.list_where
+    'panels.publish > 0'
+  end
+  
+  def self.mylist_where au
+    ['panels.author_id = ?', au.id]
+  end
+  
+  def self.himlist_where au
+    ['panels.author_id = ? and panels.publish > 0', au.id]
+  end
+  
   def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(SpeechBalloon.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => 'panels.publish > 0', :order => 'speech_balloons.updated_at desc'})
-    SpeechBalloon.find(:all, opt)
+    SpeechBalloon.where(self.list_where()).includes(SpeechBalloon.list_opt).order('speech_balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.mylist au, page = 1, page_size = Author.default_speech_balloon_page_size
+    SpeechBalloon.where(self.mylist_where(au)).includes(SpeechBalloon.list_opt).order('speech_balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.himlist au, page = 1, page_size = Author.default_speech_balloon_page_size
+    SpeechBalloon.where(self.himlist_where(au)).includes(SpeechBalloon.list_opt).order('speech_balloons.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(SpeechBalloon.where(self.list_where()).includes(SpeechBalloon.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.mylist_paginate au, page = 1, page_size = Author.default_speech_balloon_page_size
+    Kaminari.paginate_array(Array.new(SpeechBalloon.where(self.mylist_where(au)).includes(SpeechBalloon.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.himlist_paginate au, page = 1, page_size = Author.default_speech_balloon_page_size
+    Kaminari.paginate_array(Array.new(SpeechBalloon.where(self.himlist_where(au)).includes(SpeechBalloon.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.list_by_panel_where panel_id
+    ['speech_balloons.panel_id = ?', panel_id]
+  end
+  
+  def self.list_by_panel panel_id, roles, page = 1, page_size = self.default_page_size
+    self.where(self.list_by_panel_where(panel_id)).includes(self.list_opt).order('speech_balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.list_by_speech_balloon_template_where speech_balloon_template_id
+    ['speech_balloons.speech_balloon_template_id = ?', speech_balloon_template_id]
+  end
+  
+  def self.list_by_speech_balloon_template speech_balloon_template_id, roles, page = 1, page_size = self.default_page_size
+    self.where(self.list_by_speech_balloon_template_where(speech_balloon_template_id)).includes(self.list_opt).order('speech_balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
   end
   
   def self.list_opt
-    {:include => {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
+    {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }
   end
   
   def self.list_json_opt
@@ -96,6 +251,58 @@ class SpeechBalloon < ActiveRecord::Base
     {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
   end
   
+  def store au, 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({}, au)
+  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