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 self.default_page_size
50     25
51   end
52   
53   def self.max_page_size
54     100
55   end
56   
57   def self.default_panel_size
58     30
59   end
60   
61   def self.max_panel_size
62     200
63   end
64   
65   def self.page prm = nil
66     page = prm.to_i
67     page = 1 if page < 1
68     page
69   end
70   
71   def self.page_size prm = self.default_page_size
72     page_size = prm.to_i
73     page_size = self.max_page_size if page_size > self.max_page_size
74     page_size = self.default_page_size if page_size < 1
75     page_size
76   end
77   
78   def self.list_where
79     'stories.visible > 0'
80   end
81   
82   def self.mylist_where au
83     ['stories.author_id = ?', au.id]
84   end
85   
86   def self.himlist_where au
87     ['stories.author_id = ? and stories.visible > 0', au.id]
88   end
89   
90   def self.list page = 1, page_size = self.default_page_size
91     Story.where(self.list_where()).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
92   end
93   
94   def self.mylist au, page = 1, page_size = Author.default_story_page_size
95     Story.where(self.mylist_where(au)).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
96   end
97   
98   def self.himlist au, page = 1, page_size = Author.default_story_page_size
99     Story.where(self.himlist_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.list_paginate page = 1, page_size = self.default_page_size
103     Kaminari.paginate_array(Array.new(Story.where(self.list_where()).count, nil)).page(page).per(page_size)
104   end
105   
106   def self.mylist_paginate au, page = 1, page_size = Author.default_story_page_size
107     Kaminari.paginate_array(Array.new(Story.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
108   end
109   
110   def self.himlist_paginate au, page = 1, page_size = Author.default_story_page_size
111     Kaminari.paginate_array(Array.new(Story.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
112   end
113   
114   def self.list_opt
115     {:author => {}, :story_sheets => {:sheet => {}, :author => {}} }
116   end
117   
118   def self.list_json_opt
119     {:include => {:author => {}, :story_sheets => {:include => {:sheet => {}, :author => {}}} }}
120   end
121   
122   def self.show sid, roles
123     opt = {}
124     opt.merge!(Story.show_opt)
125     res = Story.find(sid, opt)
126     raise ActiveRecord::Forbidden unless res.visible?(roles)
127     res
128   end
129   
130   def self.edit sid, au
131     opt = {}
132     opt.merge!(Story.show_opt)
133     res = Story.find(sid, opt)
134     raise ActiveRecord::Forbidden unless res.own?(au)
135     res
136   end
137   
138   def self.show_opt
139     {:include => {:author => {}, :story_sheets => {:sheet => {}, :author => {}} }}
140   end
141   
142   def self.show_json_opt
143     {:include => {:author => {}, :story_sheets => {:include => {:sheet => {}, :author => {}}} }}
144   end
145   
146   def self.visible_count
147     Story.count 'visible > 0'
148   end
149   
150   def destroy_with_story_sheet
151     res = false
152     Story.transaction do
153       self.story_sheets.each do |story_sheet|
154         raise ActiveRecord::Rollback unless story_sheet.destroy
155       end
156       raise ActiveRecord::Rollback unless self.destroy
157       res = true
158     end
159     res
160   end
161   
162   def self.new_t comic_id
163     r = Story.max_t(comic_id)
164     r.blank? ? 0 : r.to_i + 1
165   end
166   
167   def self.max_t comic_id
168     Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
169   end
170   
171   def self.find_t comic_id, t
172     Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
173   end
174   
175   def self.collect_t story
176     r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
177     r.map {|s| s.t}
178   end
179   
180   def self.serial? ary
181     i = 0
182     ary.compact.sort.each do |t|
183       break false unless t == i
184       i += 1
185     end
186     ary.compact.size == i
187   end
188   
189   def self.validate_t story
190     Story.serial?(Story.collect_t(story))
191   end
192   
193   def insert_shift
194     Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
195   end
196   
197   def lesser_shift old_t
198     self.t = 0 if self.t < 0
199     Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
200   end
201   
202   def higher_shift old_t
203     nf = Story.find_t(self.comic_id, self.t)
204     max_t = Story.max_t(self.comic_id).to_i
205     self.t = max_t if self.t > max_t
206     Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
207   end
208   
209   def update_shift old_t
210     if self.t > old_t
211       higher_shift old_t
212     else
213       lesser_shift old_t
214     end
215   end
216   
217   def rotate old_t = nil
218     if self.new_record?
219       if self.t.blank?
220         self.t = Story.new_t self.comic_id
221       else
222         self.insert_shift
223       end
224     else
225       if self.t.blank?
226       else
227         self.update_shift old_t
228       end
229     end
230   end
231   
232   def allow?
233     return nil if self.comic_id == nil
234     self.comic.own?(self.author)
235   end
236   
237   def store old_t = nil
238     res = false
239     Story.transaction do
240       case self.allow?
241       when true
242         self.rotate old_t
243       when false
244         raise ActiveRecord::Forbidden
245       else
246       end
247       res = self.save
248       raise ActiveRecord::Rollback unless res
249       res = Story.validate_t(self) 
250       unless res
251         self.errors.add :t, 'unserialized'
252         raise ActiveRecord::Rollback 
253       end
254     end
255     res
256   end
257   
258   def destroy_and_shorten
259     res = false
260     Story.transaction do
261       Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
262       raise ActiveRecord::Rollback unless self.destroy_with_story_sheet
263       res = true
264     end
265     res
266   end
267   
268   
269 end