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
27 raise ActiveRecord::Forbidden unless res.own?(au)
31 def self.default_panel_size
35 def self.max_panel_size
39 def self.offset cnt, prm = nil
41 offset = cnt - 1 if offset >= cnt
42 offset = cnt - offset.abs if offset < 0
43 offset = 0 if offset < 0
47 def self.panel_count cnt, prm = self.default_panel_size
49 count = self.max_panel_size if count > self.max_panel_size
50 count = self.default_panel_size if count < 1
54 def self.list comic, author, offset = 0, limit = Story.default_panel_size
56 opt.merge!(Story.list_opt)
57 opt.merge!({:offset => offset, :limit => limit}) if limit > 0
58 opt.merge!({:conditions => ['stories.comic_id = ? and comics.visible > 0 and panels.publish > 0', comic.id], :order => 'stories.t'})
70 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
71 :speech_balloons =>{:balloons => {}, :speeches => {}}
76 def self.list_json_opt
84 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
85 :speech_balloons =>{:balloons => {}, :speeches => {}}
90 def self.mylist au, page = 1, page_size = Author.default_story_page_size
92 opt.merge!(Story.list_opt)
93 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
94 opt.merge!({:conditions => ['stories.author_id = ?', au.id], :order => 'stories.updated_at desc'})
99 self.to_json( :include => {:author => {}, :panels => {:methods => :panel_element}})
102 def self.new_t comic_id
103 r = Story.max_t(comic_id)
104 r.blank? ? 0 : r.to_i + 1
107 def self.max_t comic_id
108 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
111 def self.find_t comic_id, t
112 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
115 def self.collect_t story
116 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
122 ary.compact.sort.each do |t|
123 break false unless t == i
126 ary.compact.size == i
129 def self.validate_t story
130 Story.serial?(Story.collect_t(story))
134 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
137 def lesser_shift old_t
138 self.t = 0 if self.t < 0
139 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
142 def higher_shift old_t
143 nf = Story.find_t(self.comic_id, self.t)
144 max_t = Story.max_t(self.comic_id).to_i
145 self.t = max_t if self.t > max_t
146 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
149 def update_shift old_t
157 def rotate old_t = nil
160 self.t = Story.new_t self.comic_id
167 self.update_shift old_t
175 c and c.own?(self.author) and pl and pl.usable?(self.author)
178 def store old_t = nil
180 raise ActiveRecord::Forbidden unless self.allow?
184 raise ActiveRecord::Rollback unless res
185 res = Story.validate_t(self)
187 self.errors.add :t, 'unserialized'
188 raise ActiveRecord::Rollback
194 def destroy_and_shorten
196 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
197 raise ActiveRecord::Rollback unless self.destroy