2 class Story < Peta::Binder
7 validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
8 validates :title, :presence => true, :length => {:maximum => 100}
9 validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
10 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
14 self.visible = 0 if self.visible.blank?
21 def visible? operators
22 return false unless super
23 self.owner_model.visible? operators
31 I18n.t('stories.show.t', :t => self.disp_t)
35 self.disp_t_by_text + ':' + self.title
38 def story_sheets_count
39 StorySheet.where(['story_sheets.story_id = ?', self.id]).count
47 'stories.updated_at desc'
51 {:comic => {:author => {}} }
54 def self.by_author_list_includes
62 def self.list_json_opt
63 {:include => {:comic => {:include => {:author => {}}} }}
67 {:include => {:comic => {:author => {}} }}
70 def self.show_json_opt
71 {:include => {:comic => {:include => {:author => {}}} }}
74 def self.visible_count
75 Story.count 'visible > 0'
78 def destroy_with_story_sheet
81 self.story_sheets.each do |story_sheet|
82 raise ActiveRecord::Rollback unless story_sheet.destroy
84 raise ActiveRecord::Rollback unless self.destroy
90 def self.new_t comic_id
91 r = Story.max_t(comic_id)
92 r.blank? ? 0 : r.to_i + 1
95 def self.max_t comic_id
96 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
99 def self.find_t comic_id, t
100 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
103 def self.collect_t story
104 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
110 ary.compact.sort.each do |t|
111 break false unless t == i
114 ary.compact.size == i
117 def self.validate_t story
118 Story.serial?(Story.collect_t(story))
122 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
125 def lesser_shift old_t
126 self.t = 0 if self.t < 0
127 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
130 def higher_shift old_t
131 nf = Story.find_t(self.comic_id, self.t)
132 max_t = Story.max_t(self.comic_id).to_i
133 self.t = max_t if self.t > max_t
134 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
137 def update_shift old_t
145 def rotate old_t = nil
148 self.t = Story.new_t self.comic_id
155 self.update_shift old_t
161 return nil if self.comic_id == nil
162 self.comic.own?(operators)
165 def store operators, old_t = nil
168 case self.allow? operators
172 raise ActiveRecord::Forbidden
176 raise ActiveRecord::Rollback unless res
177 res = Story.validate_t(self)
179 self.errors.add :t, 'unserialized'
180 raise ActiveRecord::Rollback
186 def destroy_and_shorten
189 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
190 raise ActiveRecord::Rollback unless self.destroy_with_story_sheet