OSDN Git Service

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