X-Git-Url: http://git.osdn.net/view?p=pettanr%2Fpettanr.git;a=blobdiff_plain;f=app%2Fmodels%2Fcomic_story.rb;h=9e2fd280269418b2b94bf876b45cb9edf06937e5;hp=bcde23a40ad19f8e6f69061520a5945424c8c766;hb=f2201d3750a46b2ace05fef4c61eee3e6c9156f5;hpb=edbc93fdb22ec38554f2b3f4f69a23fd8a1fbd8c diff --git a/app/models/comic_story.rb b/app/models/comic_story.rb index bcde23a4..9e2fd280 100644 --- a/app/models/comic_story.rb +++ b/app/models/comic_story.rb @@ -20,6 +20,18 @@ class ComicStory < Peta::Leaf self.author_id = operators.author.id end + def disp_t + self.t + 1 + end + + def disp_t_by_text + I18n.t('comic_stories.show.t', :t => self.disp_t) + end + + def title + self.disp_t_by_text + ':' + self.story.title + end + def self.public_list_order 'comic_stories.updated_at desc' end @@ -67,84 +79,14 @@ class ComicStory < Peta::Leaf }} end - def self.new_t comic_id - r = Story.max_t(comic_id) - r.blank? ? 0 : r.to_i + 1 - end - - def self.max_t comic_id - Story.maximum(:t, :conditions => ['comic_id = ?', comic_id]) - end - - def self.find_t comic_id, t - Story.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t]) - end - - def self.collect_t story - r = Story.find(:all, :conditions => ['comic_id = ?', story.comic_id], :order => 't') - r.map {|s| s.t} - end - - def self.serial? ary - i = 0 - ary.compact.sort.each do |t| - break false unless t == i - i += 1 - end - ary.compact.size == i - end - - def self.validate_t story - Story.serial?(Story.collect_t(story)) - end - - def insert_shift - Story.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t]) - end - - def lesser_shift old_t - self.t = 0 if self.t < 0 - Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t]) - end - - def higher_shift old_t - nf = Story.find_t(self.comic_id, self.t) - max_t = Story.max_t(self.comic_id).to_i - self.t = max_t if self.t > max_t - Story.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, old_t, self.t]) - end - - def update_shift old_t - if self.t > old_t - higher_shift old_t - else - lesser_shift old_t - end - end - - def rotate old_t = nil - if self.new_record? - if self.t.blank? - self.t = Story.new_t self.comic_id - else - self.insert_shift - end - else - if self.t.blank? - else - self.update_shift old_t - end - end - end - def allow? operators return nil if self.story_id == nil or self.comic_id == nil - self.comic.own?(operators) and self.comic.usable?(operators) + self.comic.own?(operators) and self.story.own?(operators) end def store operators, old_t = nil res = false - ScrollPanel.transaction do + self.class.transaction do case self.allow? operators when true self.rotate old_t @@ -154,7 +96,7 @@ class ComicStory < Peta::Leaf end res = self.save raise ActiveRecord::Rollback unless res - res = ScrollPanel.validate_t(self.comic_id) + res = self.class.validate_t(self.comic_id) unless res self.errors.add :t, 'unserialized' raise ActiveRecord::Rollback