-class SpeechBalloonTemplate < ActiveRecord::Base
- has_many :balloon_templates, :dependent => :destroy
- has_many :speech_templates, :dependent => :destroy
- accepts_nested_attributes_for :balloon_templates
- accepts_nested_attributes_for :speech_templates
- before_create :supply_t
+#フキダシテンプレート
+class SpeechBalloonTemplate < Peta::Template
+ load_manifest
+ has_many :speech_balloons
+ belongs_to :system_picture
- def supply_t
- self.t = SpeechBalloon.maximum(:t).to_i + 1
+ validates :name, :presence => true, :uniqueness => true, :length => {:maximum => 50}
+ validates :module_name, :presence => true, :length => {:maximum => 50}
+ validates :caption, :presence => true, :length => {:maximum => 30}
+ validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
+ validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :settings, :presence => true
+
+ scope :find_index, -> do
+ self
+ end
+
+ scope :find_by_original_picture, -> (original_picture_id) do
+ find_index.where(original_picture_id: original_picture_id)
+ end
+
+ scope :with_panels, -> do
+ includes(speech_balloons: panel)
+ end
+
+ scope :find_by_panel, -> (panel_id) do
+ with_panels.where(Panel.arel_table[:id].eq panel_id).references(:panel)
+ end
+
+ scope :find_by_system_picture, -> (system_picture_id) do
+ find_index.where(system_picture_id: system_picture_id)
+ end
+
+ def supply_default
+ end
+
+ def overwrite
+ self.t = SpeechBalloonTemplate.count.to_i if self.new_record?
+ end
+
+ def symbol_option
+ self.system_picture.tmb_opt_img_tag
+ end
+
+ def self.show_opt
+ {}
+ end
+
+ def parsed_settings
+ @parsed_settings ||= JSON.parse(self.settings)
+ end
+
+ def self.store name, attr
+ #settingsにはHashデータが丸っと文字列化されて入る
+ r = SpeechBalloonTemplate.replace_system_picture(attr || {})
+ attr["settings"] = r["settings"] ? r["settings"].to_json : nil
+ r = SpeechBalloonTemplate.modify_object name, attr
+ r.overwrite
+ r.save
+ r
end
+ def self.import filename
+ SpeechBalloonTemplate.import_file(filename) {|name, attr| SpeechBalloonTemplate.store(name, attr)}
+ end
+
+ def self.disp_import_error r
+ if r == false
+ puts "json file error. file does not exist or broken"
+ else
+ unless r.empty?
+ r.each do |sbt|
+ put sbt.errors.full_messages
+ end
+ end
+ end
+ end
end