OSDN Git Service

fix model
[pettanr/pettanr.git] / app / models / speech.rb
1 class Speech < Peta::Content
2   load_manifest
3   belongs_to :speech_balloon
4   belongs_to :writing_format
5   
6   validates :speech_balloon_id, :numericality => {:allow_blank => true}
7   validates :writing_format_id, :presence => true, :numericality => true, :existence => {:both => false}
8   validates :font_size, :presence => true, :numericality => {:only_integer => false}
9   validates :text_align, :presence => true, :numericality => true, :inclusion => { :in => 0..3 }
10   validates :fore_color, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
11   validates :x, :presence => true, :numericality => true
12   validates :y, :presence => true, :numericality => true
13   validates :width, :presence => true, :numericality => true, :natural_number => true
14   validates :height, :presence => true, :numericality => true, :natural_number => true
15   validates :quotes, :length => {:maximum => 15}, :quotes_even => true
16   validates :settings, :extend_speech => true
17   
18   @@text_align_texts = ['left', 'left', 'right', 'center']
19   
20   def supply_default
21     self.x = 0
22     self.y = 0
23     self.width = 100
24     self.height = 100
25   end
26   
27   def visible? operators
28     return false unless super
29     self.owner_model.visible? operators
30   end
31   
32   def symbol_option
33     self.get_parent.speech_balloon_template.symbol_option
34   end
35   
36   def outer_style
37     {
38       'top' => self.y, 'left' => self.x, 
39       'width' => self.width, 'height' => self.height
40     }
41   end
42   
43   def inner_style
44     {
45       'font-size' => self.font_size.to_s + 'em',
46       'text-align' => self.text_align_text, 
47       'color' => '#' + format("%06x", self.fore_color)
48     }
49   end
50   
51   def text_align_text
52     @@text_align_texts[self.text_align]
53   end
54   
55   def self.list_where
56     'panels.publish > 0'
57   end
58   
59   def self.list_order
60     'speeches.updated_at desc'
61   end
62   
63   def self.list_opt
64     {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }
65   end
66   
67   def self.list_json_opt
68     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
69   end
70   
71   def self.show_opt
72     {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
73   end
74   
75   def self.show_json_opt
76     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
77   end
78   
79   def copy_attributes
80     r = self.attributes
81     r.delete 'id'
82     r.delete 'speech_balloon_id'
83     r.delete 'created_at'
84     r.delete 'updated_at'
85     r
86   end
87   
88   def self.panelize speech_attributes
89     {'speech_attributes' => speech_attributes}
90   end
91   
92   def writing_format_engine_model
93     self.writing_format.engine_model
94   end
95   
96   def scenario
97     self.writing_format_engine_model.render self.content
98   end
99   
100   def plain_scenario
101     self.content + "\n"
102   end
103   
104   def feed
105     ERB::Util.html_escape(self.content)
106   end
107   
108 end