OSDN Git Service

t#31521:fix story 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.list_paginate page = 1, page_size = self.default_page_size
117     Kaminari.paginate_array(Array.new(Story.where(self.list_where()).includes(Story.list_opt).count, nil)).page(page).per(page_size)
118   end
119   
120   def self.mylist_paginate au, page = 1, page_size = Author.default_story_page_size
121     Kaminari.paginate_array(Array.new(Story.where(self.mylist_where(au)).includes(Story.list_opt).count, nil)).page(page).per(page_size)
122   end
123   
124   def self.himlist_paginate au, page = 1, page_size = Author.default_story_page_size
125     Kaminari.paginate_array(Array.new(Story.where(self.himlist_where(au)).includes(Story.list_opt).count, nil)).page(page).per(page_size)
126   end
127   
128   def self.list_opt
129     {
130       :author => {}, 
131       :comic => {
132         :author => {}
133       }, 
134       :panel => {
135         :author => {}, 
136         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
137         :speech_balloons =>{:balloon => {}, :speech => {}}
138       }
139     }
140   end
141   
142   def self.list_json_opt
143     {:include => {
144       :author => {}, 
145       :comic => {
146         :author => {}
147       }, 
148       :panel => {
149         :author => {}, 
150         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
151         :speech_balloons =>{:balloon => {}, :speech => {}}
152       }
153     }}
154   end
155   
156   def self.show sid, roles
157     opt = {}
158     opt.merge!(Story.show_opt)
159     res = Story.find sid, opt
160     raise ActiveRecord::Forbidden unless res.visible?(roles)
161     res
162   end
163   
164   def self.edit sid, au
165     opt = {}
166     opt.merge!(Story.show_opt)
167     res = Story.find sid, opt
168     raise ActiveRecord::Forbidden unless res.own?(au)
169     res
170   end
171   
172   def self.show_opt
173     {:include => {
174       :author => {}, 
175       :comic => {
176         :author => {}
177       }, 
178       :panel => {
179         :author => {}, 
180         :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
181         :speech_balloons =>{:balloon => {}, :speech => {}}
182       }
183     }}
184   end
185   
186   def elements
187     self.panel.elements
188   end
189   
190   def story_as_json au
191     panel_include = if self.panel and self.panel.visible?(au)
192       {:include => {:author => {}}, :methods => :elements}
193     else
194       {:include => {:author => {}}}
195     end
196     self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
197   end
198   
199   def self.list_as_json_text ary, au
200     '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
201   end
202   
203   def self.licensed_pictures stories
204     r = {}
205     stories.each do |story|
206       r.merge!(story.panel.licensed_pictures) if story.panel
207     end
208     r
209   end
210   
211   def self.new_t comic_id
212     r = Story.max_t(comic_id)
213     r.blank? ? 0 : r.to_i + 1
214   end
215   
216   def self.max_t comic_id
217     Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
218   end
219   
220   def self.find_t comic_id, t
221     Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
222   end
223   
224   def self.collect_t story
225     r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
226     r.map {|s| s.t}
227   end
228   
229   def self.serial? ary
230     i = 0
231     ary.compact.sort.each do |t|
232       break false unless t == i
233       i += 1
234     end
235     ary.compact.size == i
236   end
237   
238   def self.validate_t story
239     Story.serial?(Story.collect_t(story))
240   end
241   
242   def insert_shift
243     Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
244   end
245   
246   def lesser_shift old_t
247     self.t = 0 if self.t < 0
248     Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
249   end
250   
251   def higher_shift old_t
252     nf = Story.find_t(self.comic_id, self.t)
253     max_t = Story.max_t(self.comic_id).to_i
254     self.t = max_t if self.t > max_t
255     Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
256   end
257   
258   def update_shift old_t
259     if self.t > old_t
260       higher_shift old_t
261     else
262       lesser_shift old_t
263     end
264   end
265   
266   def rotate old_t = nil
267     if self.new_record?
268       if self.t.blank?
269         self.t = Story.new_t self.comic_id
270       else
271         self.insert_shift
272       end
273     else
274       if self.t.blank?
275       else
276         self.update_shift old_t
277       end
278     end
279   end
280   
281   def allow?
282     return nil if self.comic_id == nil or self.panel_id == nil
283     self.comic.own?(self.author) and self.panel.usable?(self.author)
284   end
285   
286   def store old_t = nil
287     res = false
288     Story.transaction do
289       case self.allow?
290       when true
291         self.rotate old_t
292       when false
293         raise ActiveRecord::Forbidden
294       else
295       end
296       res = self.save
297       raise ActiveRecord::Rollback unless res
298       res = Story.validate_t(self) 
299       unless res
300         self.errors.add :t, 'unserialized'
301         raise ActiveRecord::Rollback 
302       end
303     end
304     res
305   end
306   
307   def destroy_and_shorten
308     res = false
309     Story.transaction do
310       Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
311       raise ActiveRecord::Rollback unless self.destroy
312       res = true
313     end
314     res
315   end
316   
317   
318 end