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
26 def self.default_panel_size
30 def self.max_panel_size
34 def self.offset cnt, prm = nil
36 offset = cnt - 1 if offset >= cnt
37 offset = cnt - offset.abs if offset < 0
38 offset = 0 if offset < 0
42 def self.panel_count cnt, prm = self.default_panel_size
44 count = self.max_panel_size if count > self.max_panel_size
45 count = self.default_panel_size if count < 1
49 def self.play_list comic, author, offset = 0, limit = Story.default_panel_size
51 opt.merge!(Story.list_opt)
52 opt.merge!({:offset => offset, :limit => limit}) if limit > 0
53 opt.merge!({:conditions => ['stories.comic_id = ?', comic.id], :order => 'stories.t'})
65 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
66 :speech_balloons =>{:balloons => {}, :speeches => {}}
71 def self.list_json_opt
79 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
80 :speech_balloons =>{:balloons => {}, :speeches => {}}
85 def self.mylist au, page = 1, page_size = Author.default_story_page_size
87 opt.merge!(Story.list_opt)
88 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
89 opt.merge!({:conditions => ['stories.author_id = ?', au.id], :order => 'stories.updated_at desc'})
95 opt.merge!(Story.show_opt)
96 res = Story.find sid, opt
97 raise ActiveRecord::Forbidden unless res.own?(au)
109 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
110 :speech_balloons =>{:balloons => {}, :speeches => {}}
120 panel_include = if self.panel and self.panel.visible?(au)
121 {:include => {:author => {}}, :methods => :elements}
123 {:include => {:author => {}}}
125 self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
128 def self.list_as_json_text ary, au
129 '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
132 def self.new_t comic_id
133 r = Story.max_t(comic_id)
134 r.blank? ? 0 : r.to_i + 1
137 def self.max_t comic_id
138 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
141 def self.find_t comic_id, t
142 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
145 def self.collect_t story
146 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
152 ary.compact.sort.each do |t|
153 break false unless t == i
156 ary.compact.size == i
159 def self.validate_t story
160 Story.serial?(Story.collect_t(story))
164 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
167 def lesser_shift old_t
168 self.t = 0 if self.t < 0
169 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
172 def higher_shift old_t
173 nf = Story.find_t(self.comic_id, self.t)
174 max_t = Story.max_t(self.comic_id).to_i
175 self.t = max_t if self.t > max_t
176 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
179 def update_shift old_t
187 def rotate old_t = nil
190 self.t = Story.new_t self.comic_id
197 self.update_shift old_t
205 c and c.own?(self.author) and pl and pl.usable?(self.author)
208 def store old_t = nil
210 raise ActiveRecord::Forbidden unless self.allow?
214 raise ActiveRecord::Rollback unless res
215 res = Story.validate_t(self)
217 self.errors.add :t, 'unserialized'
218 raise ActiveRecord::Rollback
224 def destroy_and_shorten
226 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
227 raise ActiveRecord::Rollback unless self.destroy