OSDN Git Service

v07
[pettanr/pettanr.git] / app / models / speech.rb
1 class Speech < Peta::Element
2   load_manifest
3   belongs_to :speech_balloon
4   belongs_to :speech_balloon_template
5   belongs_to :writing_format
6   
7   validates :speech_balloon_id, :numericality => {:allow_blank => true}
8   validates :speech_balloon_template_id, :presence => true, :numericality => true, :existence => {:both => false}
9   validates :speech_balloon_template_module_name, :presence => true, :length => {:maximum => 50}
10   validates :writing_format_id, :presence => true, :numericality => true, :existence => {:both => false}
11   validates :writing_format_module_name, :presence => true, :length => {:maximum => 50}
12   validates :font_size, :presence => true, :numericality => {:only_integer => false}
13   validates :text_align, :presence => true, :numericality => true, :inclusion => { :in => 0..3 }
14   validates :fore_color, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
15   validates :x, :presence => true, :numericality => true
16   validates :y, :presence => true, :numericality => true
17   validates :width, :presence => true, :numericality => true, :natural_number => true
18   validates :height, :presence => true, :numericality => true, :natural_number => true
19   validates :quotes, :length => {:maximum => 15}, :quotes_even => true
20   validates :speech_balloon_template_settings, :boost => {:boost_name => :speech_balloon_template}
21   #validates :writing_format_settings
22   
23   scope :with_panel, -> do
24     includes(speech_balloon: :panel)
25   end
26   
27   scope :with_speech_balloon_template, -> do
28     includes(:speech_balloon_template)
29   end
30   
31   scope :find_index, -> do
32     with_panel.where(Panel.arel_table[:publish].gt 0).references(:panel)
33   end
34   
35   scope :find_private, -> (operators) do 
36     with_panel.where(Panel.arel_table[:author_id].eq operators.author.id).references(:panel)
37   end
38   
39   scope :find_by_panel, -> (panel_id) do 
40     find_index.where(panel_id: panel_id).references(:panel)
41   end
42   
43   scope :find_by_speech_balloon, -> (speech_balloon_id) do 
44     find_index.where(speech_balloon_id: speech_balloon_id).references(:panel)
45   end
46   
47   scope :find_by_speech_balloon_template, -> (speech_balloon_template_id) do 
48     find_index.where(speech_balloon_template_id: speech_balloon_template_id).references(:panel)
49   end
50   
51   scope :find_by_writing_format, -> (writing_format_id) do 
52     find_index.where(writing_format_id: writing_format_id).references(:panel)
53   end
54   
55   scope :find_by_author, -> (author_id) do 
56     find_index.where(Panel.arel_table[:author_id].eq author_id).references(:panel)
57   end
58   
59   @@text_align_texts = ['left', 'left', 'right', 'center']
60   
61   def y
62     self.attributes['y']
63   end
64   
65   def supply_default
66     self.x = 0
67     self.y = 0
68     self.width = 100
69     self.height = 100
70     self.fore_color = 0
71   end
72   
73   def symbol_option
74     self.speech_balloon.speech_balloon_template.symbol_option
75   end
76   
77   def outer_style
78     {
79       'top' => self.y.to_s + '%', 'left' => self.x.to_s + '%', 
80       'width' => self.width.to_s + '%', 'height' => self.height.to_s + '%'
81     }
82   end
83   
84   def inner_style
85     {
86       'font-size' => self.font_size.to_s + 'em',
87       'text-align' => self.text_align_text, 
88       'color' => '#' + format("%06x", self.fore_color)
89     }
90   end
91   
92   def text_align_text
93     @@text_align_texts[self.text_align]
94   end
95   
96   def self.public_list_where list
97     'panels.publish > 0'
98   end
99   
100   def self.by_author_list_includes
101     {
102       :speech_balloon => {
103         :panel => {
104           :author => {}
105         }
106       }
107     }
108   end
109   
110   def self.show_opt
111     {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
112   end
113   
114   def scenario
115     self.boosts 'read'
116     self.render
117   end
118   
119   def plain_scenario
120     self.content.to_s + "\n"
121   end
122   
123   def feed
124     ERB::Util.html_escape(self.content)
125   end
126   
127 end