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.list_json_opt
55 {:include => {:comic => {:include => {:author => {}}} }}
59 {:include => {:comic => {:author => {}} }}
62 def self.show_json_opt
63 {:include => {:comic => {:include => {:author => {}}} }}
66 def self.visible_count
67 Story.count 'visible > 0'
70 def destroy_with_story_sheet
73 self.story_sheets.each do |story_sheet|
74 raise ActiveRecord::Rollback unless story_sheet.destroy
76 raise ActiveRecord::Rollback unless self.destroy
82 def self.new_t comic_id
83 r = Story.max_t(comic_id)
84 r.blank? ? 0 : r.to_i + 1
87 def self.max_t comic_id
88 Story.maximum(:t, :conditions => ['comic_id = ?', comic_id])
91 def self.find_t comic_id, t
92 Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
95 def self.collect_t story
96 r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't')
102 ary.compact.sort.each do |t|
103 break false unless t == i
106 ary.compact.size == i
109 def self.validate_t story
110 Story.serial?(Story.collect_t(story))
114 Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
117 def lesser_shift old_t
118 self.t = 0 if self.t < 0
119 Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
122 def higher_shift old_t
123 nf = Story.find_t(self.comic_id, self.t)
124 max_t = Story.max_t(self.comic_id).to_i
125 self.t = max_t if self.t > max_t
126 Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t])
129 def update_shift old_t
137 def rotate old_t = nil
140 self.t = Story.new_t self.comic_id
147 self.update_shift old_t
153 return nil if self.comic_id == nil
154 self.comic.own?(operators)
157 def store operators, old_t = nil
160 case self.allow? operators
164 raise ActiveRecord::Forbidden
168 raise ActiveRecord::Rollback unless res
169 res = Story.validate_t(self)
171 self.errors.add :t, 'unserialized'
172 raise ActiveRecord::Rollback
178 def destroy_and_shorten
181 Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
182 raise ActiveRecord::Rollback unless self.destroy_with_story_sheet