-class SpeechBalloon < ActiveRecord::Base
- has_many :balloons, :dependent => :destroy
- has_many :speeches, :dependent => :destroy
+class SpeechBalloon < Peta::Element
+ load_manifest
+ 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, :allow_destroy => true
+ accepts_nested_attributes_for :speech, :allow_destroy => true
- validates :panel_id, :presence => true, :numericality => true, :existence => true
- validates :speech_balloon_template_id, :presence => true, :numericality => true, :existence => true
- validates :classname, :presence => true, :length => {:maximum => 50}
+ validates :panel_id, :numericality => {:allow_blank => true}
+ validates :speech_balloon_template_id, :presence => true, :numericality => true, :existence => {:both => false}
+ 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, :presence => true
+ validates :speech_balloon_template_settings, :boost => {:boost_name => :speech_balloon_template}
- before_create :supply_t
+ scope :with_panel, -> do
+ includes(:panel)
+ end
+
+ scope :with_speech_balloon_template, -> do
+ includes(:speech_balloon_template)
+ end
+
+ 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
+ {
+ :panel => {
+ :author => {}
+ }
+ }
+ end
+
+ def self.has_picture?
+ false
+ 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 symbol_option
+ self.speech_balloon_template.symbol_option
+ end
+
+ def filer_caption
+ self.plain_scenario
+ end
+
+ def self.public_list_where list
+ 'panels.publish > 0'
+ end
+
+ def self.show_opt
+ {:include => {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
+ 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