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.himlist au, page = 1, page_size = Author.default_story_page_size
138 opt.merge!(Story.list_opt)
139 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
140 opt.merge!({:conditions => ['stories.author_id = ? and comics.visible > 0', au.id], :order => 'stories.updated_at desc'})
141 Story.find(:all, opt)
144 def self.show sid, roles
146 opt.merge!(Story.show_opt)
147 res = Story.find sid, opt
148 raise ActiveRecord::Forbidden unless res.visible?(roles)
152 def self.edit sid, au
154 opt.merge!(Story.show_opt)
155 res = Story.find sid, opt
156 raise ActiveRecord::Forbidden unless res.own?(au)
168 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
169 :speech_balloons =>{:balloons => {}, :speeches => {}}
179 panel_include = if self.panel and self.panel.visible?(au)
180 {:include => {:author => {}}, :methods => :elements}
182 {:include => {:author => {}}}
184 self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
187 def self.list_as_json_text ary, au
188 '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
191 def self.licensed_pictures stories
193 stories.each do |story|
194 r.merge!(story.panel.licensed_pictures) if story.panel
199 def self.new_t comic_id
200 r = Story.max_t(comic_id)
201 r.blank? ? 0 : r.to_i + 1
204 def self.max_t comic_id
205 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
208 def self.find_t comic_id, t
209 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
212 def self.collect_t story
213 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
219 ary.compact.sort.each do |t|
220 break false unless t == i
223 ary.compact.size == i
226 def self.validate_t story
227 Story.serial?(Story.collect_t(story))
231 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
234 def lesser_shift old_t
235 self.t = 0 if self.t < 0
236 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
239 def higher_shift old_t
240 nf = Story.find_t(self.comic_id, self.t)
241 max_t = Story.max_t(self.comic_id).to_i
242 self.t = max_t if self.t > max_t
243 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
246 def update_shift old_t
254 def rotate old_t = nil
257 self.t = Story.new_t self.comic_id
264 self.update_shift old_t
270 return nil if self.comic_id == nil or self.panel_id == nil
271 self.comic.own?(self.author) and self.panel.usable?(self.author)
274 def store old_t = nil
281 raise ActiveRecord::Forbidden
285 raise ActiveRecord::Rollback unless res
286 res = Story.validate_t(self)
288 self.errors.add :t, 'unserialized'
289 raise ActiveRecord::Rollback
295 def destroy_and_shorten
298 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
299 raise ActiveRecord::Rollback unless self.destroy