OSDN Git Service

temp
[pettanr/pettanr.git] / app / models / story.rb
1 #ストーリー
2 class Story < ActiveRecord::Base
3   has_many :story_sheets
4   belongs_to :comic
5   
6   validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
7   validates :title, :presence => true, :length => {:maximum => 100}
8   validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
9   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
10   before_validation :valid_encode
11   
12   def valid_encode
13     ['title', 'description'].each do |a|
14       next if attributes[a] == nil
15       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
16     end
17   end
18   
19   def supply_default
20     self.comic_id = nil
21     self.visible = 0 if self.visible.blank?
22     self.t = nil
23   end
24   
25   def overwrite
26   end
27   
28   def own? roles
29     roles = [roles] unless roles.respond_to?(:each)
30     au = Story.get_author_from_roles roles
31     return false unless au
32     self.comic.author_id == au.id
33   end
34   
35   def visible? roles
36     if MagicNumber['run_mode'] == 0
37       return false unless guest_role_check(roles)
38     else
39       return false unless reader_role_check(roles)
40     end
41     return true if self.own?(roles)
42     self.visible > 0
43   end
44   
45   def disp_t
46     self.t + 1
47   end
48   
49   def disp_t_by_text
50     I18n.t('stories.show.t', :t => self.disp_t)
51   end
52   
53   def title_with_t
54     self.disp_t_by_text  + ':' + self.title
55   end
56   
57   def story_sheets_count
58     StorySheet.where(['story_sheets.story_id = ?', self.id]).count
59   end
60   
61   def self.default_page_size
62     25
63   end
64   
65   def self.max_page_size
66     100
67   end
68   
69   def self.default_panel_size
70     30
71   end
72   
73   def self.max_panel_size
74     200
75   end
76   
77   def self.page prm = nil
78     page = prm.to_i
79     page = 1 if page < 1
80     page
81   end
82   
83   def self.page_size prm = self.default_page_size
84     page_size = prm.to_i
85     page_size = self.max_page_size if page_size > self.max_page_size
86     page_size = self.default_page_size if page_size < 1
87     page_size
88   end
89   
90   def self.list_where
91     'stories.visible > 0'
92   end
93   
94   def self.mylist_where au
95     ['comics.author_id = ?', au.id]
96   end
97   
98   def self.himlist_where au
99     ['comics.author_id = ? and stories.visible > 0', au.id]
100   end
101   
102   def self.list page = 1, page_size = self.default_page_size
103     Story.where(self.list_where()).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
104   end
105   
106   def self.mylist au, page = 1, page_size = Author.default_story_page_size
107     Story.where(self.mylist_where(au)).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
108   end
109   
110   def self.himlist au, page = 1, page_size = Author.default_story_page_size
111     Story.where(self.himlist_where(au)).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
112   end
113   
114   def self.list_paginate page = 1, page_size = self.default_page_size
115     Kaminari.paginate_array(Array.new(Story.where(self.list_where()).includes(Story.list_opt).count, nil)).page(page).per(page_size)
116   end
117   
118   def self.mylist_paginate au, page = 1, page_size = Author.default_story_page_size
119     Kaminari.paginate_array(Array.new(Story.where(self.mylist_where(au)).includes(Story.list_opt).count, nil)).page(page).per(page_size)
120   end
121   
122   def self.himlist_paginate au, page = 1, page_size = Author.default_story_page_size
123     Kaminari.paginate_array(Array.new(Story.where(self.himlist_where(au)).includes(Story.list_opt).count, nil)).page(page).per(page_size)
124   end
125   
126   def self.list_by_comic_where comic_id
127     ['stories.comic_id = ?', comic_id]
128   end
129   
130   def self.list_by_comic comic_id, roles, page = 1, page_size = self.default_page_size
131     self.where(self.list_by_comic_where(comic_id)).includes(self.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
132   end
133   
134   def self.list_by_sheet sheet_id, roles, page = 1, page_size = self.default_page_size
135     self.where(StorySheet.list_by_sheet_where(sheet_id)).includes(
136       {:story_sheets => {}}
137     ).order('story_sheets.updated_at desc').offset((page -1) * page_size).limit(page_size)
138   end
139   
140   def self.list_by_author_where author_id
141     ['stories.author_id = ?', author_id]
142   end
143   
144   def self.list_by_author author_id, roles, page = 1, page_size = self.default_page_size
145     self.where(self.list_by_author_where(author_id)).includes(self.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
146   end
147   
148   def self.list_opt
149     {:comic => {:author => {}} }
150   end
151   
152   def self.list_json_opt
153     {:include => {:comic => {:include => {:author => {}}} }}
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 => {:comic => {:author => {}} }}
174   end
175   
176   def self.show_json_opt
177     {:include => {:comic => {:include => {:author => {}}} }}
178   end
179   
180   def self.visible_count
181     Story.count 'visible > 0'
182   end
183   
184   def destroy_with_story_sheet
185     res = false
186     Story.transaction do
187       self.story_sheets.each do |story_sheet|
188         raise ActiveRecord::Rollback unless story_sheet.destroy
189       end
190       raise ActiveRecord::Rollback unless self.destroy
191       res = true
192     end
193     res
194   end
195   
196   def self.new_t comic_id
197     r = Story.max_t(comic_id)
198     r.blank? ? 0 : r.to_i + 1
199   end
200   
201   def self.max_t comic_id
202     Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
203   end
204   
205   def self.find_t comic_id, t
206     Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
207   end
208   
209   def self.collect_t story
210     r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
211     r.map {|s| s.t}
212   end
213   
214   def self.serial? ary
215     i = 0
216     ary.compact.sort.each do |t|
217       break false unless t == i
218       i += 1
219     end
220     ary.compact.size == i
221   end
222   
223   def self.validate_t story
224     Story.serial?(Story.collect_t(story))
225   end
226   
227   def insert_shift
228     Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
229   end
230   
231   def lesser_shift old_t
232     self.t = 0 if self.t < 0
233     Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
234   end
235   
236   def higher_shift old_t
237     nf = Story.find_t(self.comic_id, self.t)
238     max_t = Story.max_t(self.comic_id).to_i
239     self.t = max_t if self.t > max_t
240     Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
241   end
242   
243   def update_shift old_t
244     if self.t > old_t
245       higher_shift old_t
246     else
247       lesser_shift old_t
248     end
249   end
250   
251   def rotate old_t = nil
252     if self.new_record?
253       if self.t.blank?
254         self.t = Story.new_t self.comic_id
255       else
256         self.insert_shift
257       end
258     else
259       if self.t.blank?
260       else
261         self.update_shift old_t
262       end
263     end
264   end
265   
266   def allow? au
267     return nil if self.comic_id == nil
268     self.comic.own?(au)
269   end
270   
271   def store au, old_t = nil
272     res = false
273     Story.transaction do
274       case self.allow? au
275       when true
276         self.rotate old_t
277       when false
278         raise ActiveRecord::Forbidden
279       else
280       end
281       res = self.save
282       raise ActiveRecord::Rollback unless res
283       res = Story.validate_t(self) 
284       unless res
285         self.errors.add :t, 'unserialized'
286         raise ActiveRecord::Rollback 
287       end
288     end
289     res
290   end
291   
292   def destroy_and_shorten
293     res = false
294     Story.transaction do
295       Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
296       raise ActiveRecord::Rollback unless self.destroy_with_story_sheet
297       res = true
298     end
299     res
300   end
301   
302   
303 end