OSDN Git Service

t#31470:create pager
[pettanr/pettanr.git] / app / models / story.rb
1 #ストーリー
2 class Story < ActiveRecord::Base
3   belongs_to :author
4   belongs_to :panel
5   belongs_to :comic
6   
7   validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
8   validates :panel_id, :presence => true, :numericality => true, :existence => {:both => false}
9   validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
10   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
11   
12   def supply_default
13     self.comic_id = nil
14     self.panel_id = nil
15     self.t = nil
16   end
17   
18   def overwrite au
19     return false unless au
20     self.author_id = au.id
21   end
22   
23   def own? roles
24     roles = [roles] unless roles.respond_to?(:each)
25     au = Story.get_author_from_roles roles
26     return false unless au
27     self.author_id == au.id
28   end
29   
30   def visible? roles
31     if MagicNumber['run_mode'] == 0
32       return false unless guest_role_check(roles)
33     else
34       return false unless reader_role_check(roles)
35     end
36     return true if self.comic.own?(roles)
37     self.comic.visible? roles
38   end
39   
40   def self.default_panel_size
41     30
42   end
43   
44   def self.max_panel_size
45     200
46   end
47   
48   def self.offset cnt, prm = nil
49     offset = prm.to_i
50     offset = cnt - 1 if offset >= cnt
51     offset = cnt - offset.abs if offset < 0
52     offset = 0 if offset < 0
53     offset
54   end
55   
56   def self.panel_count cnt, prm = self.default_panel_size
57     count = prm.to_i
58     count = self.max_panel_size if count > self.max_panel_size
59     count = self.default_panel_size if count < 1
60     count
61   end
62   
63   def self.default_page_size
64     25
65   end
66   
67   def self.max_page_size
68     100
69   end
70   
71   def self.page prm = nil
72     page = prm.to_i
73     page = 1 if page < 1
74     page
75   end
76   
77   def self.page_size prm = self.default_page_size
78     page_size = prm.to_i
79     page_size = self.max_page_size if page_size > self.max_page_size
80     page_size = self.default_page_size if page_size < 1
81     page_size
82   end
83   
84   def self.play_list_where cid
85     ['stories.comic_id = ?', cid]
86   end
87   
88   def self.list_where
89     'comics.visible > 0'
90   end
91   
92   def self.mylist_where au
93     ['stories.author_id = ?', au.id]
94   end
95   
96   def self.himlist_where au
97     ['stories.author_id = ? and comics.visible > 0', au.id]
98   end
99   
100   def self.play_list comic, author, offset = 0, limit = Story.default_panel_size
101     Story.where(self.play_list_where(comic.id)).includes(Story.list_opt).order('stories.t').offset(offset).limit(limit)
102   end
103   
104   def self.list page = 1, page_size = self.default_page_size
105     Story.where(self.list_where()).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
106   end
107   
108   def self.mylist au, page = 1, page_size = Author.default_story_page_size
109     Story.where(self.mylist_where(au)).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
110   end
111   
112   def self.himlist au, page = 1, page_size = Author.default_story_page_size
113     Story.where(self.himlist_where(au)).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
114   end
115   
116   def self.play_list_paginate comic, author, offset = 0, limit = Story.default_panel_size
117   end
118   
119   def self.list_paginate page = 1, page_size = self.default_page_size
120     Kaminari.paginate_array(Array.new(Story.where(self.list_where()).includes(Story.list_opt).count, nil)).page(page).per(page_size)
121   end
122   
123   def self.mylist_paginate au, page = 1, page_size = Author.default_story_page_size
124     Kaminari.paginate_array(Array.new(Story.where(self.mylist_where(au)).includes(Story.list_opt).count, nil)).page(page).per(page_size)
125   end
126   
127   def self.himlist_paginate au, page = 1, page_size = Author.default_story_page_size
128     Kaminari.paginate_array(Array.new(Story.where(self.himlist_where(au)).includes(Story.list_opt).count, nil)).page(page).per(page_size)
129   end
130   
131   def self.list_opt
132     {
133       :author => {}, 
134       :comic => {
135         :author => {}
136       }, 
137       :panel => {
138         :author => {}, 
139         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
140         :speech_balloons =>{:balloons => {}, :speeches => {}}
141       }
142     }
143   end
144   
145   def self.list_json_opt
146     {:include => {
147       :author => {}, 
148       :comic => {
149         :author => {}
150       }, 
151       :panel => {
152         :author => {}, 
153         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
154         :speech_balloons =>{:balloons => {}, :speeches => {}}
155       }
156     }}
157   end
158   
159   def self.show sid, roles
160     opt = {}
161     opt.merge!(Story.show_opt)
162     res = Story.find sid, opt
163     raise ActiveRecord::Forbidden unless res.visible?(roles)
164     res
165   end
166   
167   def self.edit sid, au
168     opt = {}
169     opt.merge!(Story.show_opt)
170     res = Story.find sid, opt
171     raise ActiveRecord::Forbidden unless res.own?(au)
172     res
173   end
174   
175   def self.show_opt
176     {:include => {
177       :author => {}, 
178       :comic => {
179         :author => {}
180       }, 
181       :panel => {
182         :author => {}, 
183         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
184         :speech_balloons =>{:balloons => {}, :speeches => {}}
185       }
186     }}
187   end
188   
189   def elements
190     self.panel.elements
191   end
192   
193   def story_as_json au
194     panel_include = if self.panel and self.panel.visible?(au)
195       {:include => {:author => {}}, :methods => :elements}
196     else
197       {:include => {:author => {}}}
198     end
199     self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
200   end
201   
202   def self.list_as_json_text ary, au
203     '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
204   end
205   
206   def self.licensed_pictures stories
207     r = {}
208     stories.each do |story|
209       r.merge!(story.panel.licensed_pictures) if story.panel
210     end
211     r
212   end
213   
214   def self.new_t comic_id
215     r = Story.max_t(comic_id)
216     r.blank? ? 0 : r.to_i + 1
217   end
218   
219   def self.max_t comic_id
220     Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
221   end
222   
223   def self.find_t comic_id, t
224     Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
225   end
226   
227   def self.collect_t story
228     r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
229     r.map {|s| s.t}
230   end
231   
232   def self.serial? ary
233     i = 0
234     ary.compact.sort.each do |t|
235       break false unless t == i
236       i += 1
237     end
238     ary.compact.size == i
239   end
240   
241   def self.validate_t story
242     Story.serial?(Story.collect_t(story))
243   end
244   
245   def insert_shift
246     Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
247   end
248   
249   def lesser_shift old_t
250     self.t = 0 if self.t < 0
251     Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
252   end
253   
254   def higher_shift old_t
255     nf = Story.find_t(self.comic_id, self.t)
256     max_t = Story.max_t(self.comic_id).to_i
257     self.t = max_t if self.t > max_t
258     Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
259   end
260   
261   def update_shift old_t
262     if self.t > old_t
263       higher_shift old_t
264     else
265       lesser_shift old_t
266     end
267   end
268   
269   def rotate old_t = nil
270     if self.new_record?
271       if self.t.blank?
272         self.t = Story.new_t self.comic_id
273       else
274         self.insert_shift
275       end
276     else
277       if self.t.blank?
278       else
279         self.update_shift old_t
280       end
281     end
282   end
283   
284   def allow?
285     return nil if self.comic_id == nil or self.panel_id == nil
286     self.comic.own?(self.author) and self.panel.usable?(self.author)
287   end
288   
289   def store old_t = nil
290     res = false
291     Story.transaction do
292       case self.allow?
293       when true
294         self.rotate old_t
295       when false
296         raise ActiveRecord::Forbidden
297       else
298       end
299       res = self.save
300       raise ActiveRecord::Rollback unless res
301       res = Story.validate_t(self) 
302       unless res
303         self.errors.add :t, 'unserialized'
304         raise ActiveRecord::Rollback 
305       end
306     end
307     res
308   end
309   
310   def destroy_and_shorten
311     res = false
312     Story.transaction do
313       Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
314       raise ActiveRecord::Rollback unless self.destroy
315       res = true
316     end
317     res
318   end
319   
320   
321 end