OSDN Git Service

temp
[pettanr/pettanr.git] / app / models / speech.rb
1 class Speech < Pettanr::Item
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 self.singular
21     'Speech'
22   end
23   
24   def self.plural
25     'Speeches'
26   end
27   
28   def self.owner_type
29     :author
30   end
31   
32   def self.valid_encode_columns
33     super.merge ['content', 'quotes', 'settings']
34   end
35   
36   def self.colum_structures
37     @@colum_structures ||= {
38       :fore_color => {
39         :helper => 'panels/color_helper'
40       }
41     }
42   end
43   
44   def supply_default
45     self.x = 0
46     self.y = 0
47     self.width = 100
48     self.height = 100
49   end
50   
51   def visible? roles
52     if MagicNumber['run_mode'] == 0
53       return false unless guest_role_check(roles)
54     else
55       return false unless reader_role_check(roles)
56     end
57     return true if self.speech_balloon.panel.own?(roles)
58     self.speech_balloon.panel.visible? roles
59   end
60   
61   def symbol_option
62     self.get_parent.speech_balloon_template.symbol_option
63   end
64   
65   def get_parent
66     self.speech_balloon || @new_parent
67   end
68   
69   def tag_element_part_type
70     'speech'
71   end
72   
73   def text_align_text
74     @@text_align_texts[self.text_align]
75   end
76   
77   def self.default_page_size
78     25
79   end
80   
81   def self.max_page_size
82     100
83   end
84   
85   def self.page prm = nil
86     page = prm.to_i
87     page = 1 if page < 1
88     page
89   end
90   
91   def self.page_size prm = self.default_page_size
92     page_size = prm.to_i
93     page_size = self.max_page_size if page_size > self.max_page_size
94     page_size = self.default_page_size if page_size < 1
95     page_size
96   end
97   
98   def self.list_where
99     'panels.publish > 0'
100   end
101   
102   def self.list page = 1, page_size = self.default_page_size
103     Speech.where(self.list_where()).includes(Speech.list_opt).order('speeches.updated_at desc').offset((page -1) * page_size).limit(page_size)
104   end
105   
106   def self.list_paginate page = 1, page_size = self.default_page_size
107     Kaminari.paginate_array(Array.new(Speech.where(self.list_where()).includes(Speech.list_opt).count, nil)).page(page).per(page_size)
108   end
109   
110   def self.list_by_speech_balloon_where speech_balloon_id
111     ['speeches.speech_balloon_id = ?', speech_balloon_id]
112   end
113   
114   def self.list_by_speech_balloon speech_balloon_id, roles, page = 1, page_size = self.default_page_size
115     self.where(self.list_by_speech_balloon_where(speech_balloon_id)).includes(self.list_opt).order('speeches.updated_at desc').offset((page -1) * page_size).limit(page_size)
116   end
117   
118   def self.list_by_writing_format_where writing_format_id
119     ['speeches.writing_format_id = ?', writing_format_id]
120   end
121   
122   def self.list_by_writing_format writing_format_id, roles, page = 1, page_size = self.default_page_size
123     self.where(self.list_by_writing_format_where(writing_format_id)).includes(self.list_opt).order('speeches.updated_at desc').offset((page -1) * page_size).limit(page_size)
124   end
125   
126   def self.list_opt
127     {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }
128   end
129   
130   def self.list_json_opt
131     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
132   end
133   
134   def self.show cid, au
135     opt = {}
136     opt.merge!(Speech.show_opt)
137     res = Speech.find(cid, opt)
138     raise ActiveRecord::Forbidden unless res.visible?(au)
139     res
140   end
141   
142   def self.show_opt
143     {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
144   end
145   
146   def self.show_json_opt
147     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
148   end
149   
150   def copy_attributes
151     r = self.attributes
152     r.delete 'id'
153     r.delete 'speech_balloon_id'
154     r.delete 'created_at'
155     r.delete 'updated_at'
156     r
157   end
158   
159   def self.panelize speech_attributes
160     {'speech_attributes' => speech_attributes}
161   end
162   
163   def writing_format_engine_model
164     self.writing_format.engine_model
165   end
166   
167   def scenario
168     self.writing_format_engine_model.render self.content
169   end
170   
171   def plain_scenario
172     self.content + "\n"
173   end
174   
175   def feed
176     ERB::Util.html_escape(self.content)
177   end
178   
179 end