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 return false unless au.is_a?(Author)
25 self.author_id == au.id
30 return false if MagicNumber['run_mode'] == 1
31 elsif au.is_a?(Author)
32 return true if self.comic.own?(au)
36 self.comic.visible? au
39 def self.default_panel_size
43 def self.max_panel_size
47 def self.offset cnt, prm = nil
49 offset = cnt - 1 if offset >= cnt
50 offset = cnt - offset.abs if offset < 0
51 offset = 0 if offset < 0
55 def self.panel_count cnt, prm = self.default_panel_size
57 count = self.max_panel_size if count > self.max_panel_size
58 count = self.default_panel_size if count < 1
62 def self.play_list comic, author, offset = 0, limit = Story.default_panel_size
64 opt.merge!(Story.list_opt)
65 opt.merge!({:offset => offset, :limit => limit}) if limit > 0
66 opt.merge!({:conditions => ['stories.comic_id = ?', comic.id], :order => 'stories.t'})
78 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
79 :speech_balloons =>{:balloons => {}, :speeches => {}}
84 def self.list_json_opt
92 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
93 :speech_balloons =>{:balloons => {}, :speeches => {}}
98 def self.default_page_size
102 def self.max_page_size
106 def self.page prm = nil
112 def self.page_size prm = self.default_page_size
114 page_size = self.max_page_size if page_size > self.max_page_size
115 page_size = self.default_page_size if page_size < 1
119 def self.list page = 1, page_size = self.default_page_size
121 opt.merge!(self.list_opt)
122 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
123 opt.merge!({:conditions => ['comics.visible > 0'], :order => 'stories.updated_at desc'})
124 Story.find(:all, opt)
127 def self.mylist au, page = 1, page_size = Author.default_story_page_size
129 opt.merge!(Story.list_opt)
130 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
131 opt.merge!({:conditions => ['stories.author_id = ?', au.id], :order => 'stories.updated_at desc'})
132 Story.find(:all, opt)
135 def self.show sid, au
137 opt.merge!(Story.show_opt)
138 res = Story.find sid, opt
139 raise ActiveRecord::Forbidden unless res.visible?(au)
143 def self.edit sid, au
145 opt.merge!(Story.show_opt)
146 res = Story.find sid, opt
147 raise ActiveRecord::Forbidden unless res.own?(au)
159 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
160 :speech_balloons =>{:balloons => {}, :speeches => {}}
170 panel_include = if self.panel and self.panel.visible?(au)
171 {:include => {:author => {}}, :methods => :elements}
173 {:include => {:author => {}}}
175 self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
178 def self.list_as_json_text ary, au
179 '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
182 def self.licensed_pictures stories
184 stories.each do |story|
185 r.merge!(story.panel.licensed_pictures) if story.panel
190 def self.new_t comic_id
191 r = Story.max_t(comic_id)
192 r.blank? ? 0 : r.to_i + 1
195 def self.max_t comic_id
196 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
199 def self.find_t comic_id, t
200 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
203 def self.collect_t story
204 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
210 ary.compact.sort.each do |t|
211 break false unless t == i
214 ary.compact.size == i
217 def self.validate_t story
218 Story.serial?(Story.collect_t(story))
222 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
225 def lesser_shift old_t
226 self.t = 0 if self.t < 0
227 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
230 def higher_shift old_t
231 nf = Story.find_t(self.comic_id, self.t)
232 max_t = Story.max_t(self.comic_id).to_i
233 self.t = max_t if self.t > max_t
234 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
237 def update_shift old_t
245 def rotate old_t = nil
248 self.t = Story.new_t self.comic_id
255 self.update_shift old_t
263 c and c.own?(self.author) and pl and pl.usable?(self.author)
266 def store old_t = nil
268 raise ActiveRecord::Forbidden unless self.allow?
272 raise ActiveRecord::Rollback unless res
273 res = Story.validate_t(self)
275 self.errors.add :t, 'unserialized'
276 raise ActiveRecord::Rollback
282 def destroy_and_shorten
284 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
285 raise ActiveRecord::Rollback unless self.destroy