2 class Story < ActiveRecord::Base
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}
19 return false unless au
20 self.author_id = au.id
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
31 if MagicNumber['run_mode'] == 0
32 return false unless guest_role_check(roles)
34 return false unless reader_role_check(roles)
36 return true if self.comic.own?(roles)
37 self.comic.visible? roles
40 def self.default_panel_size
44 def self.max_panel_size
48 def self.offset cnt, prm = nil
50 offset = cnt - 1 if offset >= cnt
51 offset = cnt - offset.abs if offset < 0
52 offset = 0 if offset < 0
56 def self.panel_count cnt, prm = self.default_panel_size
58 count = self.max_panel_size if count > self.max_panel_size
59 count = self.default_panel_size if count < 1
63 def self.default_page_size
67 def self.max_page_size
71 def self.page prm = nil
77 def self.page_size prm = self.default_page_size
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
84 def self.play_list_where cid
85 ['stories.comic_id = ?', cid]
92 def self.mylist_where au
93 ['stories.author_id = ?', au.id]
96 def self.himlist_where au
97 ['stories.author_id = ? and comics.visible > 0', au.id]
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)
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)
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)
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)
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)
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)
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)
136 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
137 :speech_balloons =>{:balloon => {}, :speech => {}}
142 def self.list_json_opt
150 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
151 :speech_balloons =>{:balloon => {}, :speech => {}}
156 def self.show sid, roles
158 opt.merge!(Story.show_opt)
159 res = Story.find sid, opt
160 raise ActiveRecord::Forbidden unless res.visible?(roles)
164 def self.edit sid, au
166 opt.merge!(Story.show_opt)
167 res = Story.find sid, opt
168 raise ActiveRecord::Forbidden unless res.own?(au)
180 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
181 :speech_balloons =>{:balloon => {}, :speech => {}}
191 panel_include = if self.panel and self.panel.visible?(au)
192 {:include => {:author => {}}, :methods => :elements}
194 {:include => {:author => {}}}
196 self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
199 def self.list_as_json_text ary, au
200 '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
203 def self.licensed_pictures stories
205 stories.each do |story|
206 r.merge!(story.panel.licensed_pictures) if story.panel
211 def self.new_t comic_id
212 r = Story.max_t(comic_id)
213 r.blank? ? 0 : r.to_i + 1
216 def self.max_t comic_id
217 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
220 def self.find_t comic_id, t
221 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
224 def self.collect_t story
225 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
231 ary.compact.sort.each do |t|
232 break false unless t == i
235 ary.compact.size == i
238 def self.validate_t story
239 Story.serial?(Story.collect_t(story))
243 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
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])
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])
258 def update_shift old_t
266 def rotate old_t = nil
269 self.t = Story.new_t self.comic_id
276 self.update_shift old_t
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)
286 def store old_t = nil
293 raise ActiveRecord::Forbidden
297 raise ActiveRecord::Rollback unless res
298 res = Story.validate_t(self)
300 self.errors.add :t, 'unserialized'
301 raise ActiveRecord::Rollback
307 def destroy_and_shorten
310 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
311 raise ActiveRecord::Rollback unless self.destroy