OSDN Git Service

fix: fetch fail
[pettanr/pettanr.git] / app / models / speech_balloon_template.rb
1 #フキダシテンプレート
2 class SpeechBalloonTemplate < Peta::Template
3   load_manifest
4   has_many :speech_balloons
5   belongs_to :system_picture
6   
7   validates :name, :presence => true, :uniqueness => true, :length => {:maximum => 50}
8   validates :module_name, :presence => true, :length => {:maximum => 50}
9   validates :caption, :presence => true, :length => {:maximum => 30}
10   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
11   validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
12   validates :settings, :presence => true
13   
14   scope :find_index, -> do
15     self
16   end
17   
18   scope :find_by_original_picture, -> (original_picture_id) do 
19     find_index.where(original_picture_id: original_picture_id)
20   end
21   
22   scope :with_panels, -> do
23     includes(speech_balloons: panel)
24   end
25   
26   scope :find_by_panel, -> (panel_id) do 
27     with_panels.where(Panel.arel_table[:id].eq panel_id).references(:panel)
28   end
29   
30   scope :find_by_system_picture, -> (system_picture_id) do 
31     find_index.where(system_picture_id: system_picture_id)
32   end
33   
34   def supply_default
35   end
36   
37   def overwrite
38     self.t = SpeechBalloonTemplate.count.to_i if self.new_record?
39   end
40   
41   def symbol_option
42     self.system_picture.tmb_opt_img_tag
43   end
44   
45   def self.show_opt
46     {}
47   end
48   
49   def parsed_settings
50     @parsed_settings ||= JSON.parse(self.settings)
51   end
52   
53   def self.store name, attr
54     #settingsにはHashデータが丸っと文字列化されて入る
55     r = SpeechBalloonTemplate.replace_system_picture(attr || {})
56     attr["settings"] = r["settings"] ? r["settings"].to_json : nil
57     r = SpeechBalloonTemplate.modify_object name, attr
58     r.overwrite
59     r.save
60     r
61   end
62   
63   def self.import filename
64     SpeechBalloonTemplate.import_file(filename) {|name, attr| SpeechBalloonTemplate.store(name, attr)}
65   end
66   
67   def self.disp_import_error r
68     if r == false
69       puts "json file error. file does not exist or broken"
70     else
71       unless r.empty?
72         r.each do |sbt|
73           put sbt.errors.full_messages
74         end
75       end
76     end
77   end
78 end