OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[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_opt
127     {:comic => {:author => {}} }
128   end
129   
130   def self.list_json_opt
131     {:include => {:comic => {:include => {:author => {}}} }}
132   end
133   
134   def self.show sid, roles
135     opt = {}
136     opt.merge!(Story.show_opt)
137     res = Story.find(sid, opt)
138     raise ActiveRecord::Forbidden unless res.visible?(roles)
139     res
140   end
141   
142   def self.edit sid, au
143     opt = {}
144     opt.merge!(Story.show_opt)
145     res = Story.find(sid, opt)
146     raise ActiveRecord::Forbidden unless res.own?(au)
147     res
148   end
149   
150   def self.show_opt
151     {:include => {:comic => {:author => {}} }}
152   end
153   
154   def self.show_json_opt
155     {:include => {:comic => {:include => {:author => {}}} }}
156   end
157   
158   def self.visible_count
159     Story.count 'visible > 0'
160   end
161   
162   def destroy_with_story_sheet
163     res = false
164     Story.transaction do
165       self.story_sheets.each do |story_sheet|
166         raise ActiveRecord::Rollback unless story_sheet.destroy
167       end
168       raise ActiveRecord::Rollback unless self.destroy
169       res = true
170     end
171     res
172   end
173   
174   def self.new_t comic_id
175     r = Story.max_t(comic_id)
176     r.blank? ? 0 : r.to_i + 1
177   end
178   
179   def self.max_t comic_id
180     Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
181   end
182   
183   def self.find_t comic_id, t
184     Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
185   end
186   
187   def self.collect_t story
188     r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
189     r.map {|s| s.t}
190   end
191   
192   def self.serial? ary
193     i = 0
194     ary.compact.sort.each do |t|
195       break false unless t == i
196       i += 1
197     end
198     ary.compact.size == i
199   end
200   
201   def self.validate_t story
202     Story.serial?(Story.collect_t(story))
203   end
204   
205   def insert_shift
206     Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
207   end
208   
209   def lesser_shift old_t
210     self.t = 0 if self.t < 0
211     Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
212   end
213   
214   def higher_shift old_t
215     nf = Story.find_t(self.comic_id, self.t)
216     max_t = Story.max_t(self.comic_id).to_i
217     self.t = max_t if self.t > max_t
218     Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
219   end
220   
221   def update_shift old_t
222     if self.t > old_t
223       higher_shift old_t
224     else
225       lesser_shift old_t
226     end
227   end
228   
229   def rotate old_t = nil
230     if self.new_record?
231       if self.t.blank?
232         self.t = Story.new_t self.comic_id
233       else
234         self.insert_shift
235       end
236     else
237       if self.t.blank?
238       else
239         self.update_shift old_t
240       end
241     end
242   end
243   
244   def allow? au
245     return nil if self.comic_id == nil
246     self.comic.own?(au)
247   end
248   
249   def store au, old_t = nil
250     res = false
251     Story.transaction do
252       case self.allow? au
253       when true
254         self.rotate old_t
255       when false
256         raise ActiveRecord::Forbidden
257       else
258       end
259       res = self.save
260       raise ActiveRecord::Rollback unless res
261       res = Story.validate_t(self) 
262       unless res
263         self.errors.add :t, 'unserialized'
264         raise ActiveRecord::Rollback 
265       end
266     end
267     res
268   end
269   
270   def destroy_and_shorten
271     res = false
272     Story.transaction do
273       Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
274       raise ActiveRecord::Rollback unless self.destroy_with_story_sheet
275       res = true
276     end
277     res
278   end
279   
280   
281 end