OSDN Git Service

work
[pettanr/pettanr.git] / app / models / speech.rb
1 class Speech < Pettanr::Content
2   include ElementPart
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 owner_model
21     self.speech_balloon.panel
22   end
23   
24   def self.valid_encode_columns
25     super + ['content', 'quotes', 'settings']
26   end
27   
28   def self.colum_structures
29     @@colum_structures ||= {
30       :fore_color => {
31         :helper => 'panels/color_helper'
32       }
33     }
34   end
35   
36   def supply_default
37     self.x = 0
38     self.y = 0
39     self.width = 100
40     self.height = 100
41   end
42   
43   def visible? operators
44     return false unless super
45     self.owner_model.visible? operators
46   end
47   
48   def symbol_option
49     self.get_parent.speech_balloon_template.symbol_option
50   end
51   
52   def get_parent
53     self.speech_balloon || @new_parent
54   end
55   
56   def tag_element_part_type
57     'speech'
58   end
59   
60   def text_align_text
61     @@text_align_texts[self.text_align]
62   end
63   
64   def self.list_where
65     'panels.publish > 0'
66   end
67   
68   def self.list_order
69     'speeches.updated_at desc'
70   end
71   
72   def self.list_opt
73     {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }
74   end
75   
76   def self.list_json_opt
77     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
78   end
79   
80   def self.show_opt
81     {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
82   end
83   
84   def self.show_json_opt
85     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
86   end
87   
88   def copy_attributes
89     r = self.attributes
90     r.delete 'id'
91     r.delete 'speech_balloon_id'
92     r.delete 'created_at'
93     r.delete 'updated_at'
94     r
95   end
96   
97   def self.panelize speech_attributes
98     {'speech_attributes' => speech_attributes}
99   end
100   
101   def writing_format_engine_model
102     self.writing_format.engine_model
103   end
104   
105   def scenario
106     self.writing_format_engine_model.render self.content
107   end
108   
109   def plain_scenario
110     self.content + "\n"
111   end
112   
113   def feed
114     ERB::Util.html_escape(self.content)
115   end
116   
117 end