1 class ComicStory < Peta::Leaf
7 validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
8 validates :story_id, :presence => true, :numericality => true, :existence => {:both => false}
9 validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
10 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
18 def overwrite operators
19 return false unless operators.author
20 self.author_id = operators.author.id
23 def self.public_list_order
24 'comic_stories.updated_at desc'
31 def self.by_author_list_includes
39 def self.play_list_where cid
40 ['comic_stories.comic_id = ?', cid]
43 def self.play_list scroll, author, offset = 0, limit = ScrollPanel.default_panel_size
44 ScrollPanel.where(self.play_list_where(scroll.id)).includes(ScrollPanel.list_opt).order('scroll_panels.t').offset(offset).limit(limit)
55 def self.list_json_opt
70 def self.new_t comic_id
71 r = Story.max_t(comic_id)
72 r.blank? ? 0 : r.to_i + 1
75 def self.max_t comic_id
76 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
79 def self.find_t comic_id, t
80 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
83 def self.collect_t story
84 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
90 ary.compact.sort.each do |t|
91 break false unless t == i
97 def self.validate_t story
98 Story.serial?(Story.collect_t(story))
102 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
105 def lesser_shift old_t
106 self.t = 0 if self.t < 0
107 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
110 def higher_shift old_t
111 nf = Story.find_t(self.comic_id, self.t)
112 max_t = Story.max_t(self.comic_id).to_i
113 self.t = max_t if self.t > max_t
114 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
117 def update_shift old_t
125 def rotate old_t = nil
128 self.t = Story.new_t self.comic_id
135 self.update_shift old_t
141 return nil if self.story_id == nil or self.comic_id == nil
142 self.comic.own?(operators) and self.comic.usable?(operators)
145 def store operators, old_t = nil
147 ScrollPanel.transaction do
148 case self.allow? operators
152 raise ActiveRecord::Forbidden
156 raise ActiveRecord::Rollback unless res
157 res = ScrollPanel.validate_t(self.comic_id)
159 self.errors.add :t, 'unserialized'
160 raise ActiveRecord::Rollback