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.play_list_paginate comic, author, offset = 0, limit = Story.default_panel_size
119 def self.list_paginate page = 1, page_size = self.default_page_size
120 Kaminari.paginate_array(Array.new(Story.where(self.list_where()).includes(Story.list_opt).count, nil)).page(page).per(page_size)
123 def self.mylist_paginate au, page = 1, page_size = Author.default_story_page_size
124 Kaminari.paginate_array(Array.new(Story.where(self.mylist_where(au)).includes(Story.list_opt).count, nil)).page(page).per(page_size)
127 def self.himlist_paginate au, page = 1, page_size = Author.default_story_page_size
128 Kaminari.paginate_array(Array.new(Story.where(self.himlist_where(au)).includes(Story.list_opt).count, nil)).page(page).per(page_size)
139 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
140 :speech_balloons =>{:balloon => {}, :speech => {}}
145 def self.list_json_opt
153 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
154 :speech_balloons =>{:balloon => {}, :speech => {}}
159 def self.show sid, roles
161 opt.merge!(Story.show_opt)
162 res = Story.find sid, opt
163 raise ActiveRecord::Forbidden unless res.visible?(roles)
167 def self.edit sid, au
169 opt.merge!(Story.show_opt)
170 res = Story.find sid, opt
171 raise ActiveRecord::Forbidden unless res.own?(au)
183 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
184 :speech_balloons =>{:balloon => {}, :speech => {}}
194 panel_include = if self.panel and self.panel.visible?(au)
195 {:include => {:author => {}}, :methods => :elements}
197 {:include => {:author => {}}}
199 self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
202 def self.list_as_json_text ary, au
203 '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
206 def self.licensed_pictures stories
208 stories.each do |story|
209 r.merge!(story.panel.licensed_pictures) if story.panel
214 def self.new_t comic_id
215 r = Story.max_t(comic_id)
216 r.blank? ? 0 : r.to_i + 1
219 def self.max_t comic_id
220 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
223 def self.find_t comic_id, t
224 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
227 def self.collect_t story
228 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
234 ary.compact.sort.each do |t|
235 break false unless t == i
238 ary.compact.size == i
241 def self.validate_t story
242 Story.serial?(Story.collect_t(story))
246 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
249 def lesser_shift old_t
250 self.t = 0 if self.t < 0
251 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
254 def higher_shift old_t
255 nf = Story.find_t(self.comic_id, self.t)
256 max_t = Story.max_t(self.comic_id).to_i
257 self.t = max_t if self.t > max_t
258 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
261 def update_shift old_t
269 def rotate old_t = nil
272 self.t = Story.new_t self.comic_id
279 self.update_shift old_t
285 return nil if self.comic_id == nil or self.panel_id == nil
286 self.comic.own?(self.author) and self.panel.usable?(self.author)
289 def store old_t = nil
296 raise ActiveRecord::Forbidden
300 raise ActiveRecord::Rollback unless res
301 res = Story.validate_t(self)
303 self.errors.add :t, 'unserialized'
304 raise ActiveRecord::Rollback
310 def destroy_and_shorten
313 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
314 raise ActiveRecord::Rollback unless self.destroy