2 class Story < ActiveRecord::Base
7 validates :comic_id, :presence => true, :numericality => true, :existence => true
8 validates :panel_id, :presence => true, :numericality => true, :existence => true
9 validates :author_id, :presence => true, :numericality => true, :existence => true
10 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
17 return false unless au
18 self.author_id = au.id
22 return false unless au.is_a?(Author)
23 self.author_id == au.id
28 return false if MagicNumber['run_mode'] == 1
29 elsif au.is_a?(Author)
30 return true if self.comic.own?(au)
34 self.comic.visible? au
37 def self.default_panel_size
41 def self.max_panel_size
45 def self.offset cnt, prm = nil
47 offset = cnt - 1 if offset >= cnt
48 offset = cnt - offset.abs if offset < 0
49 offset = 0 if offset < 0
53 def self.panel_count cnt, prm = self.default_panel_size
55 count = self.max_panel_size if count > self.max_panel_size
56 count = self.default_panel_size if count < 1
60 def self.play_list comic, author, offset = 0, limit = Story.default_panel_size
62 opt.merge!(Story.list_opt)
63 opt.merge!({:offset => offset, :limit => limit}) if limit > 0
64 opt.merge!({:conditions => ['stories.comic_id = ?', comic.id], :order => 'stories.t'})
76 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
77 :speech_balloons =>{:balloons => {}, :speeches => {}}
82 def self.list_json_opt
90 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
91 :speech_balloons =>{:balloons => {}, :speeches => {}}
96 def self.mylist au, page = 1, page_size = Author.default_story_page_size
98 opt.merge!(Story.list_opt)
99 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
100 opt.merge!({:conditions => ['stories.author_id = ?', au.id], :order => 'stories.updated_at desc'})
101 Story.find(:all, opt)
104 def self.show sid, au
106 opt.merge!(Story.show_opt)
107 res = Story.find sid, opt
108 raise ActiveRecord::Forbidden unless res.visible?(au)
112 def self.edit sid, au
114 opt.merge!(Story.show_opt)
115 res = Story.find sid, opt
116 raise ActiveRecord::Forbidden unless res.own?(au)
128 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
129 :speech_balloons =>{:balloons => {}, :speeches => {}}
139 panel_include = if self.panel and self.panel.visible?(au)
140 {:include => {:author => {}}, :methods => :elements}
142 {:include => {:author => {}}}
144 self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
147 def self.list_as_json_text ary, au
148 '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
151 def self.licensed_pictures stories
153 stories.each do |story|
154 r.merge! story.panel.licensed_pictures
159 def self.new_t comic_id
160 r = Story.max_t(comic_id)
161 r.blank? ? 0 : r.to_i + 1
164 def self.max_t comic_id
165 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
168 def self.find_t comic_id, t
169 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
172 def self.collect_t story
173 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
179 ary.compact.sort.each do |t|
180 break false unless t == i
183 ary.compact.size == i
186 def self.validate_t story
187 Story.serial?(Story.collect_t(story))
191 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
194 def lesser_shift old_t
195 self.t = 0 if self.t < 0
196 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
199 def higher_shift old_t
200 nf = Story.find_t(self.comic_id, self.t)
201 max_t = Story.max_t(self.comic_id).to_i
202 self.t = max_t if self.t > max_t
203 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
206 def update_shift old_t
214 def rotate old_t = nil
217 self.t = Story.new_t self.comic_id
224 self.update_shift old_t
232 c and c.own?(self.author) and pl and pl.usable?(self.author)
235 def store old_t = nil
237 raise ActiveRecord::Forbidden unless self.allow?
241 raise ActiveRecord::Rollback unless res
242 res = Story.validate_t(self)
244 self.errors.add :t, 'unserialized'
245 raise ActiveRecord::Rollback
251 def destroy_and_shorten
253 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
254 raise ActiveRecord::Rollback unless self.destroy