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 self.author_id == author.id
25 def self.default_panel_size
29 def self.max_panel_size
33 def self.offset cnt, prm = nil
35 offset = cnt - 1 if offset >= cnt
36 offset = cnt - offset.abs if offset < 0
37 offset = 0 if offset < 0
41 def self.panel_count cnt, prm = self.default_panel_size
43 count = self.max_panel_size if count > self.max_panel_size
44 count = self.default_panel_size if count < 1
48 def self.play_list comic, author, offset = 0, limit = Story.default_panel_size
50 opt.merge!(Story.list_opt)
51 opt.merge!({:offset => offset, :limit => limit}) if limit > 0
52 opt.merge!({:conditions => ['stories.comic_id = ?', comic.id], :order => 'stories.t'})
64 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
65 :speech_balloons =>{:balloons => {}, :speeches => {}}
70 def self.list_json_opt
78 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
79 :speech_balloons =>{:balloons => {}, :speeches => {}}
84 def self.mylist au, page = 1, page_size = Author.default_story_page_size
86 opt.merge!(Story.list_opt)
87 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
88 opt.merge!({:conditions => ['stories.author_id = ?', au.id], :order => 'stories.updated_at desc'})
94 opt.merge!(Story.show_opt)
95 res = Story.find sid, opt
96 raise ActiveRecord::Forbidden unless res.own?(au)
108 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
109 :speech_balloons =>{:balloons => {}, :speeches => {}}
119 panel_include = if self.panel and self.panel.visible?(au)
120 {:include => {:author => {}}, :methods => :elements}
122 {:include => {:author => {}}}
124 self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}})
127 def self.list_as_json_text ary, au
128 '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
131 def self.new_t comic_id
132 r = Story.max_t(comic_id)
133 r.blank? ? 0 : r.to_i + 1
136 def self.max_t comic_id
137 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
140 def self.find_t comic_id, t
141 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
144 def self.collect_t story
145 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
151 ary.compact.sort.each do |t|
152 break false unless t == i
155 ary.compact.size == i
158 def self.validate_t story
159 Story.serial?(Story.collect_t(story))
163 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
166 def lesser_shift old_t
167 self.t = 0 if self.t < 0
168 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
171 def higher_shift old_t
172 nf = Story.find_t(self.comic_id, self.t)
173 max_t = Story.max_t(self.comic_id).to_i
174 self.t = max_t if self.t > max_t
175 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
178 def update_shift old_t
186 def rotate old_t = nil
189 self.t = Story.new_t self.comic_id
196 self.update_shift old_t
204 c and c.own?(self.author) and pl and pl.usable?(self.author)
207 def store old_t = nil
209 raise ActiveRecord::Forbidden unless self.allow?
213 raise ActiveRecord::Rollback unless res
214 res = Story.validate_t(self)
216 self.errors.add :t, 'unserialized'
217 raise ActiveRecord::Rollback
223 def destroy_and_shorten
225 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
226 raise ActiveRecord::Rollback unless self.destroy