OSDN Git Service

v07
[pettanr/pettanr.git] / app / models / speech_balloon.rb
1 class SpeechBalloon < Peta::Element
2   load_manifest
3   has_one :balloon, :dependent => :destroy
4   has_one :speech, :dependent => :destroy
5   belongs_to :speech_balloon_template
6   belongs_to :panel
7   
8   accepts_nested_attributes_for :balloon, :allow_destroy => true
9   accepts_nested_attributes_for :speech, :allow_destroy => true
10   
11   validates :panel_id, :numericality => {:allow_blank => true}
12   validates :speech_balloon_template_id, :presence => true, :numericality => true, :existence => {:both => false}
13   validates :speech_balloon_template_module_name, :presence => true, :length => {:maximum => 50}
14   validates :z, :presence => true, :numericality => {:greater_than => 0}
15   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
16   validates :speech_balloon_template_settings, :boost => {:boost_name => :speech_balloon_template}
17   
18   scope :with_panel, -> do
19     includes(:panel)
20   end
21   
22   scope :with_speech_balloon_template, -> do
23     includes(:speech_balloon_template)
24   end
25   
26   scope :find_index, -> do
27     with_panel.where(Panel.arel_table[:publish].gt 0).references(:panel)
28   end
29   
30   scope :find_private, -> (operators) do 
31     with_panel.where(Panel.arel_table[:author_id].eq operators.author.id).references(:panel)
32   end
33   
34   scope :find_by_panel, -> (panel_id) do 
35     find_index.where(panel_id: panel_id).references(:panel)
36   end
37   
38   scope :find_by_speech_balloon_template, -> (speech_balloon_template_id) do 
39     find_index.where(speech_balloon_template_id: speech_balloon_template_id).references(:panel)
40   end
41   
42   scope :find_by_author, -> (author_id) do 
43     find_index.where(Panel.arel_table[:author_id].eq author_id).references(:panel)
44   end
45   
46   def self.by_author_list_includes
47     {
48       :panel => {
49         :author => {}
50       }
51     }
52   end
53   
54   def self.has_picture?
55     false
56   end
57   
58   def supply_default
59     if self.panel
60       self.t = self.panel.new_t 
61       self.z = self.panel.new_z 
62     end
63   end
64   
65   def overwrite pid
66     self.panel_id = pid
67   end
68   
69   def symbol_option
70     self.speech_balloon_template.symbol_option
71   end
72   
73   def filer_caption
74     self.plain_scenario
75   end
76   
77   def self.public_list_where list
78     'panels.publish > 0'
79   end
80   
81   def self.show_opt
82     {:include => {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
83   end
84   
85   def scenario_template with_engine = false
86     self.path_name(true) + '/scenario'
87   end
88   
89   def element_face_template with_engine = false
90     self.path_name(false) + '/element_face'
91   end
92   
93   def scenario
94     ERB::Util.html_escape(self.caption.to_s) + self.balloon.scenario.to_s + 
95     self.speech.scenario.to_s
96   end
97   
98   def plain_scenario
99     self.caption.to_s + self.balloon.plain_scenario.to_s + 
100     self.speech.plain_scenario.to_s
101   end
102   
103 end