2 class Story < Peta::Content
6 validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
7 validates :title, :presence => true, :length => {:maximum => 100}
8 validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
9 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
11 def tag_attributes column = nil, opt = {}
20 def self.valid_encode_columns
21 super + ['title', 'description']
26 self.visible = 0 if self.visible.blank?
33 def visible? operators
34 return false unless super
35 self.owner_model.visible? operators
43 I18n.t('stories.show.t', :t => self.disp_t)
47 self.disp_t_by_text + ':' + self.title
50 def story_sheets_count
51 StorySheet.where(['story_sheets.story_id = ?', self.id]).count
59 'stories.updated_at desc'
63 {:comic => {:author => {}} }
66 def self.list_json_opt
67 {:include => {:comic => {:include => {:author => {}}} }}
71 {:include => {:comic => {:author => {}} }}
74 def self.show_json_opt
75 {:include => {:comic => {:include => {:author => {}}} }}
78 def self.visible_count
79 Story.count 'visible > 0'
82 def destroy_with_story_sheet
85 self.story_sheets.each do |story_sheet|
86 raise ActiveRecord::Rollback unless story_sheet.destroy
88 raise ActiveRecord::Rollback unless self.destroy
94 def self.new_t comic_id
95 r = Story.max_t(comic_id)
96 r.blank? ? 0 : r.to_i + 1
99 def self.max_t comic_id
100 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
103 def self.find_t comic_id, t
104 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
107 def self.collect_t story
108 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
114 ary.compact.sort.each do |t|
115 break false unless t == i
118 ary.compact.size == i
121 def self.validate_t story
122 Story.serial?(Story.collect_t(story))
126 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
129 def lesser_shift old_t
130 self.t = 0 if self.t < 0
131 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
134 def higher_shift old_t
135 nf = Story.find_t(self.comic_id, self.t)
136 max_t = Story.max_t(self.comic_id).to_i
137 self.t = max_t if self.t > max_t
138 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
141 def update_shift old_t
149 def rotate old_t = nil
152 self.t = Story.new_t self.comic_id
159 self.update_shift old_t
165 return nil if self.comic_id == nil
166 self.comic.own?(operators)
169 def store operators, old_t = nil
172 case self.allow? operators
176 raise ActiveRecord::Forbidden
180 raise ActiveRecord::Rollback unless res
181 res = Story.validate_t(self)
183 self.errors.add :t, 'unserialized'
184 raise ActiveRecord::Rollback
190 def destroy_and_shorten
193 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
194 raise ActiveRecord::Rollback unless self.destroy_with_story_sheet