OSDN Git Service

Item Dynamic ClassMethods
[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 text_align_text
54     @@text_align_texts[self.text_align]
55   end
56   
57   def self.list_where
58     'panels.publish > 0'
59   end
60   
61   def self.list_order
62     'speeches.updated_at desc'
63   end
64   
65   def self.list_opt
66     {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }
67   end
68   
69   def self.list_json_opt
70     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
71   end
72   
73   def self.show_opt
74     {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
75   end
76   
77   def self.show_json_opt
78     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
79   end
80   
81   def copy_attributes
82     r = self.attributes
83     r.delete 'id'
84     r.delete 'speech_balloon_id'
85     r.delete 'created_at'
86     r.delete 'updated_at'
87     r
88   end
89   
90   def self.panelize speech_attributes
91     {'speech_attributes' => speech_attributes}
92   end
93   
94   def writing_format_engine_model
95     self.writing_format.engine_model
96   end
97   
98   def scenario
99     self.writing_format_engine_model.render self.content
100   end
101   
102   def plain_scenario
103     self.content + "\n"
104   end
105   
106   def feed
107     ERB::Util.html_escape(self.content)
108   end
109   
110 end