OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / app / models / speech.rb
1 class Speech < ActiveRecord::Base
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   before_validation :valid_encode
21   
22   def valid_encode
23     ['content', 'quotes', 'settings'].each do |a|
24       next if attributes[a] == nil
25       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
26     end
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? roles
37     if MagicNumber['run_mode'] == 0
38       return false unless guest_role_check(roles)
39     else
40       return false unless reader_role_check(roles)
41     end
42     return true if self.speech_balloon.panel.own?(roles)
43     self.speech_balloon.panel.visible? roles
44   end
45   
46   def get_parent
47     self.speech_balloon || @new_parent
48   end
49   
50   def tag_element_part_type
51     'speech'
52   end
53   
54   def text_align_text
55     @@text_align_texts[self.text_align]
56   end
57   
58   def self.default_page_size
59     25
60   end
61   
62   def self.max_page_size
63     100
64   end
65   
66   def self.page prm = nil
67     page = prm.to_i
68     page = 1 if page < 1
69     page
70   end
71   
72   def self.page_size prm = self.default_page_size
73     page_size = prm.to_i
74     page_size = self.max_page_size if page_size > self.max_page_size
75     page_size = self.default_page_size if page_size < 1
76     page_size
77   end
78   
79   def self.list page = 1, page_size = self.default_page_size
80     opt = {}
81     opt.merge!(Speech.list_opt)
82     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
83     opt.merge!({:conditions => 'panels.publish > 0', :order => 'speeches.updated_at desc'})
84     Speech.find(:all, opt)
85   end
86   
87   def self.list_opt
88     {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }}
89   end
90   
91   def self.list_json_opt
92     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
93   end
94   
95   def self.show cid, au
96     opt = {}
97     opt.merge!(Speech.show_opt)
98     res = Speech.find(cid, opt)
99     raise ActiveRecord::Forbidden unless res.visible?(au)
100     res
101   end
102   
103   def self.show_opt
104     {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
105   end
106   
107   def self.show_json_opt
108     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
109   end
110   
111   def copy_attributes
112     r = self.attributes
113     r.delete 'id'
114     r.delete 'speech_balloon_id'
115     r.delete 'created_at'
116     r.delete 'updated_at'
117     r
118   end
119   
120   def self.panelize speech_attributes
121     {'speech_attributes' => speech_attributes}
122   end
123   
124   def writing_format_engine_model
125     self.writing_format.engine_model
126   end
127   
128   def scenario
129     self.writing_format_engine_model.render self.content
130   end
131   
132   def plain_scenario
133     self.content + "\n"
134   end
135   
136   def feed
137     ERB::Util.html_escape(self.content)
138   end
139   
140 end