2 class Story < Pettanr::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}
15 def self.valid_encode_columns
16 super + ['title', 'description']
21 self.visible = 0 if self.visible.blank?
28 def visible? operators
29 return false unless super
30 self.owner_model.visible? operators
38 I18n.t('stories.show.t', :t => self.disp_t)
42 self.disp_t_by_text + ':' + self.title
45 def story_sheets_count
46 StorySheet.where(['story_sheets.story_id = ?', self.id]).count
54 'stories.updated_at desc'
58 {:comic => {:author => {}} }
61 def self.list_json_opt
62 {:include => {:comic => {:include => {:author => {}}} }}
66 {:include => {:comic => {:author => {}} }}
69 def self.show_json_opt
70 {:include => {:comic => {:include => {:author => {}}} }}
73 def self.visible_count
74 Story.count 'visible > 0'
77 def destroy_with_story_sheet
80 self.story_sheets.each do |story_sheet|
81 raise ActiveRecord::Rollback unless story_sheet.destroy
83 raise ActiveRecord::Rollback unless self.destroy
89 def self.new_t comic_id
90 r = Story.max_t(comic_id)
91 r.blank? ? 0 : r.to_i + 1
94 def self.max_t comic_id
95 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
98 def self.find_t comic_id, t
99 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
102 def self.collect_t story
103 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
109 ary.compact.sort.each do |t|
110 break false unless t == i
113 ary.compact.size == i
116 def self.validate_t story
117 Story.serial?(Story.collect_t(story))
121 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
124 def lesser_shift old_t
125 self.t = 0 if self.t < 0
126 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
129 def higher_shift old_t
130 nf = Story.find_t(self.comic_id, self.t)
131 max_t = Story.max_t(self.comic_id).to_i
132 self.t = max_t if self.t > max_t
133 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
136 def update_shift old_t
144 def rotate old_t = nil
147 self.t = Story.new_t self.comic_id
154 self.update_shift old_t
160 return nil if self.comic_id == nil
161 self.comic.own?(operators)
164 def store operators, old_t = nil
167 case self.allow? operators
171 raise ActiveRecord::Forbidden
175 raise ActiveRecord::Rollback unless res
176 res = Story.validate_t(self)
178 self.errors.add :t, 'unserialized'
179 raise ActiveRecord::Rollback
185 def destroy_and_shorten
188 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
189 raise ActiveRecord::Rollback unless self.destroy_with_story_sheet