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 author
23 self.author_id == author.id
28 raise ActiveRecord::Forbidden unless res.own?(au)
32 def self.new_t comic_id
33 r = Story.max_t(comic_id)
34 r.blank? ? 0 : r.to_i + 1
37 def self.max_t comic_id
38 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
41 def self.find_t comic_id, t
42 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
45 def self.collect_t story
46 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
52 ary.compact.sort.each do |t|
53 break false unless t == i
59 def self.validate_t story
60 Story.serial?(Story.collect_t(story))
64 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
67 def lesser_shift old_t
68 self.t = 0 if self.t < 0
69 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
72 def higher_shift old_t
73 nf = Story.find_t(self.comic_id, self.t)
74 max_t = Story.max_t(self.comic_id).to_i
75 self.t = max_t if self.t > max_t
76 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
79 def update_shift old_t
87 def rotate old_t = nil
90 self.t = Story.new_t self.comic_id
97 self.update_shift old_t
105 c and c.own?(self.author) and pl and pl.usable?(self.author)
108 def store old_t = nil
110 raise ActiveRecord::Forbidden unless self.allow?
114 raise ActiveRecord::Rollback unless res
115 res = Story.validate_t(self)
117 self.errors.add :t, 'unserialized'
118 raise ActiveRecord::Rollback
124 def destroy_and_shorten
126 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
127 raise ActiveRecord::Rollback unless self.destroy
131 def self.default_page_size
135 def self.max_page_size
139 def self.page prm = nil
145 def self.page_size prm = self.default_page_size
147 page_size = self.max_page_size if page_size > self.max_page_size
148 page_size = self.default_page_size if page_size < 1
152 def self.default_panel_size
156 def self.max_panel_size
160 def self.offset cnt, prm = nil
162 offset = cnt - 1 if offset >= cnt
163 offset = cnt - offset.abs if offset < 0
164 offset = 0 if offset < 0
168 def self.panel_count cnt, prm = self.default_panel_size
170 count = self.max_panel_size if count > self.max_panel_size
171 count = self.default_panel_size if count < 1
175 def self.list comic, author, offset = 0, limit = Story.default_panel_size
177 opt.merge!({:conditions => ['stories.comic_id = ? and panels.publish > 0', comic.id], :order => 'stories.t', :offset => offset, :limit => limit})
178 Story.find(:all, opt)
189 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
190 :speech_balloons =>{:balloons => {}, :speeches => {}}
195 def self.list_json_opt
203 :panel_pictures => {:picture => {:artist => {}, :license => {}}},
204 :speech_balloons =>{:balloons => {}, :speeches => {}}
209 def self.mylist au, opt = {}, page = 1, story_page_size = Author.default_story_page_size
210 opt.merge!(self.list_opt) unless opt[:include]
211 opt.merge!({:conditions => ['stories.author_id = ?', au.id], :order => 'stories.updated_at desc', :limit => page_size, :offset => (page -1) * story_page_size})
212 Story.find(:all, opt)
216 self.to_json( :include => {:author => {}, :panels => {:methods => :panel_element}})