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.play_list comic, author, offset = 0, limit = Story.default_panel_size
65 opt.merge!(Story.list_opt)
66 opt.merge!({:offset => offset, :limit => limit}) if limit > 0
67 opt.merge!({:conditions => ['stories.comic_id = ?', comic.id], :order => 'stories.t'})
79 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
80 :speech_balloons =>{:balloons => {}, :speeches => {}}
85 def self.list_json_opt
93 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
94 :speech_balloons =>{:balloons => {}, :speeches => {}}
99 def self.default_page_size
103 def self.max_page_size
107 def self.page prm = nil
113 def self.page_size prm = self.default_page_size
115 page_size = self.max_page_size if page_size > self.max_page_size
116 page_size = self.default_page_size if page_size < 1
120 def self.list page = 1, page_size = self.default_page_size
122 opt.merge!(self.list_opt)
123 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
124 opt.merge!({:conditions => ['comics.visible > 0'], :order => 'stories.updated_at desc'})
125 Story.find(:all, opt)
128 def self.mylist au, page = 1, page_size = Author.default_story_page_size
130 opt.merge!(Story.list_opt)
131 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
132 opt.merge!({:conditions => ['stories.author_id = ?', au.id], :order => 'stories.updated_at desc'})
133 Story.find(:all, opt)
136 def self.show sid, roles
138 opt.merge!(Story.show_opt)
139 res = Story.find sid, opt
140 raise ActiveRecord::Forbidden unless res.visible?(roles)
144 def self.edit sid, au
146 opt.merge!(Story.show_opt)
147 res = Story.find sid, opt
148 raise ActiveRecord::Forbidden unless res.own?(au)
160 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
161 :speech_balloons =>{:balloons => {}, :speeches => {}}
171 panel_include = if self.panel and self.panel.visible?(au)
172 {:include => {:author => {}}, :methods => :elements}
174 {:include => {:author => {}}}
176 self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
179 def self.list_as_json_text ary, au
180 '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
183 def self.licensed_pictures stories
185 stories.each do |story|
186 r.merge!(story.panel.licensed_pictures) if story.panel
191 def self.new_t comic_id
192 r = Story.max_t(comic_id)
193 r.blank? ? 0 : r.to_i + 1
196 def self.max_t comic_id
197 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
200 def self.find_t comic_id, t
201 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
204 def self.collect_t story
205 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
211 ary.compact.sort.each do |t|
212 break false unless t == i
215 ary.compact.size == i
218 def self.validate_t story
219 Story.serial?(Story.collect_t(story))
223 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
226 def lesser_shift old_t
227 self.t = 0 if self.t < 0
228 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
231 def higher_shift old_t
232 nf = Story.find_t(self.comic_id, self.t)
233 max_t = Story.max_t(self.comic_id).to_i
234 self.t = max_t if self.t > max_t
235 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
238 def update_shift old_t
246 def rotate old_t = nil
249 self.t = Story.new_t self.comic_id
256 self.update_shift old_t
262 return nil if self.comic_id == nil or self.panel_id == nil
263 self.comic.own?(self.author) and self.panel.usable?(self.author)
266 def store old_t = nil
273 raise ActiveRecord::Forbidden
277 raise ActiveRecord::Rollback unless res
278 res = Story.validate_t(self)
280 self.errors.add :t, 'unserialized'
281 raise ActiveRecord::Rollback
287 def destroy_and_shorten
290 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
291 raise ActiveRecord::Rollback unless self.destroy