OSDN Git Service

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