2 class Story < Peta::Content
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}
12 def tag_attributes column = nil, opt = {}
19 self.visible = 0 if self.visible.blank?
26 def visible? operators
27 return false unless super
28 self.owner_model.visible? operators
36 I18n.t('stories.show.t', :t => self.disp_t)
40 self.disp_t_by_text + ':' + self.title
43 def story_sheets_count
44 StorySheet.where(['story_sheets.story_id = ?', self.id]).count
52 'stories.updated_at desc'
56 {:comic => {:author => {}} }
59 def self.list_json_opt
60 {:include => {:comic => {:include => {:author => {}}} }}
64 {:include => {:comic => {:author => {}} }}
67 def self.show_json_opt
68 {:include => {:comic => {:include => {:author => {}}} }}
71 def self.visible_count
72 Story.count 'visible > 0'
75 def destroy_with_story_sheet
78 self.story_sheets.each do |story_sheet|
79 raise ActiveRecord::Rollback unless story_sheet.destroy
81 raise ActiveRecord::Rollback unless self.destroy
87 def self.new_t comic_id
88 r = Story.max_t(comic_id)
89 r.blank? ? 0 : r.to_i + 1
92 def self.max_t comic_id
93 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
96 def self.find_t comic_id, t
97 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
100 def self.collect_t story
101 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
107 ary.compact.sort.each do |t|
108 break false unless t == i
111 ary.compact.size == i
114 def self.validate_t story
115 Story.serial?(Story.collect_t(story))
119 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
122 def lesser_shift old_t
123 self.t = 0 if self.t < 0
124 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
127 def higher_shift old_t
128 nf = Story.find_t(self.comic_id, self.t)
129 max_t = Story.max_t(self.comic_id).to_i
130 self.t = max_t if self.t > max_t
131 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
134 def update_shift old_t
142 def rotate old_t = nil
145 self.t = Story.new_t self.comic_id
152 self.update_shift old_t
158 return nil if self.comic_id == nil
159 self.comic.own?(operators)
162 def store operators, old_t = nil
165 case self.allow? operators
169 raise ActiveRecord::Forbidden
173 raise ActiveRecord::Rollback unless res
174 res = Story.validate_t(self)
176 self.errors.add :t, 'unserialized'
177 raise ActiveRecord::Rollback
183 def destroy_and_shorten
186 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
187 raise ActiveRecord::Rollback unless self.destroy_with_story_sheet