-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, :numericality => {:allow_blank => true}
- validates :speech_balloon_template_id, :presence => true, :numericality => true, :existence => true
- validates :classname, :presence => true, :length => {:maximum => 50}
+ 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
-
- def visible? au
- if au == nil
- return false if MagicNumber['run_mode'] == 1
- elsif au.is_a?(Author)
- elsif au.is_a?(Admin)
- return true
- else
- return false
- end
- self.panel.publish?
+ validates :speech_balloon_template_settings, :boost => {:boost_name => :speech_balloon_template}
+
+ 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
- def self.default_page_size
- 25
+ scope :find_private, -> (operators) do
+ with_panel.where(Panel.arel_table[:author_id].eq operators.author.id).references(:panel)
end
- def self.max_page_size
- 100
+ scope :find_by_panel, -> (panel_id) do
+ find_index.where(panel_id: panel_id).references(:panel)
end
- def self.page prm = nil
- page = prm.to_i
- page = 1 if page < 1
- page
+ 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
- def self.page_size prm = self.default_page_size
- page_size = prm.to_i
- page_size = self.max_page_size if page_size > self.max_page_size
- page_size = self.default_page_size if page_size < 1
- page_size
+ scope :find_by_author, -> (author_id) do
+ find_index.where(Panel.arel_table[:author_id].eq author_id).references(:panel)
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)
+ def self.by_author_list_includes
+ {
+ :panel => {
+ :author => {}
+ }
+ }
end
- def self.list_opt
- {:include => {:panel => {:author => {}}, :balloons => {}, :speeches => {}, :speech_balloon_template => {} }}
+ def self.has_picture?
+ false
end
- def self.list_json_opt
- {:include => {:panel => {:include => {:author => {} }}, :balloons => {}, :speeches => {}, :speech_balloon_template => {} }}
+ def supply_default
+ if self.panel
+ self.t = self.panel.new_t
+ self.z = self.panel.new_z
+ end
end
- def self.show cid, au
- opt = {}
- opt.merge!(SpeechBalloon.show_opt)
- res = SpeechBalloon.find(cid, opt)
- raise ActiveRecord::Forbidden unless res.visible?(au)
- res
+ 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 => {}}, :balloons => {}, :speeches => {}, :speech_balloon_template => {} }}
+ {: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 self.show_json_opt
- {:include => {:panel => {:include => {:author => {} }}, :balloons => {}, :speeches => {}, :speech_balloon_template => {} }}
+ def plain_scenario
+ self.caption.to_s + self.balloon.plain_scenario.to_s +
+ self.speech.plain_scenario.to_s
end
end