From 07c57b2b6d0d2e8c1e0c0d55dd8266cf62c7c05c Mon Sep 17 00:00:00 2001 From: yasushiito Date: Tue, 20 May 2014 08:40:18 +0900 Subject: [PATCH] fix leaf sort --- app/models/panel_picture.rb | 3 +- app/models/scroll_panel.rb | 82 +-------------------------------------------- lib/peta/leaf.rb | 60 +++++++++++++++++++-------------- 3 files changed, 38 insertions(+), 107 deletions(-) diff --git a/app/models/panel_picture.rb b/app/models/panel_picture.rb index b75b4179..daea39d9 100644 --- a/app/models/panel_picture.rb +++ b/app/models/panel_picture.rb @@ -66,7 +66,8 @@ class PanelPicture < Peta::Element def flip res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h') - res += '/' unless res.empty? + res = res # format of /1.png?subdir=v + # res += '/' unless res.empty? # format of /v/1.png res end diff --git a/app/models/scroll_panel.rb b/app/models/scroll_panel.rb index 72071790..e70bc5d8 100644 --- a/app/models/scroll_panel.rb +++ b/app/models/scroll_panel.rb @@ -111,76 +111,6 @@ class ScrollPanel < Peta::Leaf r end - def self.new_t scroll_id - r = ScrollPanel.max_t(scroll_id) - r.blank? ? 0 : r.to_i + 1 - end - - def self.max_t scroll_id - ScrollPanel.maximum(:t, :conditions => ['scroll_id = ?', scroll_id]) - end - - def self.find_t scroll_id, t - ScrollPanel.find(:first, :conditions => ['scroll_id = ? and t = ?', scroll_id, t]) - end - - def self.collect_t scroll_panel - r = ScrollPanel.find(:all, :conditions => ['scroll_id = ?', scroll_panel.scroll_id], :order => 't') - r.map {|sp| sp.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 scroll_panel - ScrollPanel.serial?(ScrollPanel.collect_t(scroll_panel)) - end - - def insert_shift - ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and t >= ?', self.scroll_id, self.t]) - end - - def lesser_shift old_t - self.t = 0 if self.t < 0 - ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and (t >= ? and t < ?)', self.scroll_id, self.t, old_t]) - end - - def higher_shift old_t - nf = ScrollPanel.find_t(self.scroll_id, self.t) - max_t = ScrollPanel.max_t(self.scroll_id).to_i - self.t = max_t if self.t > max_t - ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ? and t <= ?)', self.scroll_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 = ScrollPanel.new_t self.scroll_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.scroll_id == nil or self.panel_id == nil self.scroll.own?(operators) and self.panel.usable?(operators) @@ -198,7 +128,7 @@ class ScrollPanel < Peta::Leaf end res = self.save raise ActiveRecord::Rollback unless res - res = ScrollPanel.validate_t(self) + res = ScrollPanel.validate_t(self.scroll_id) unless res self.errors.add :t, 'unserialized' raise ActiveRecord::Rollback @@ -207,14 +137,4 @@ class ScrollPanel < Peta::Leaf res end - def destroy_and_shorten - res = false - ScrollPanel.transaction do - ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ?)', self.scroll_id, self.t]) - raise ActiveRecord::Rollback unless self.destroy - res = true - end - res - end - end diff --git a/lib/peta/leaf.rb b/lib/peta/leaf.rb index 7f4a5b36..16afd726 100644 --- a/lib/peta/leaf.rb +++ b/lib/peta/leaf.rb @@ -12,6 +12,10 @@ module Peta define_singleton_method("parent_model") do pm end + pfk = self.my_peta.parent_model_name + '_id' + define_singleton_method("binder_key") do + pfk + end # Instance Methods end @@ -67,20 +71,20 @@ module Peta end def self.new_t binder_id - r = ScrollPanel.max_t(binder_id) + r = self.max_t(binder_id) r.blank? ? 0 : r.to_i + 1 end def self.max_t binder_id - self.class..maximum(:t, :conditions => ['scroll_id = ?', binder_id]) + self.maximum(:t, :conditions => [self.binder_key + ' = ?', binder_id]) end def self.find_t binder_id, t - self.class.find(:first, :conditions => ['scroll_id = ? and t = ?', binder_id, t]) + self.find(:first, :conditions => [self.binder_key + ' = ? and t = ?', binder_id, t]) end - def self.collect_t scroll_panel - r = self.class.find(:all, :conditions => ['scroll_id = ?', scroll_panel.scroll_id], :order => 't') + def self.collect_t binder_id + r = self.find(:all, :conditions => [self.binder_key + ' = ?', binder_id], :order => 't') r.map {|sp| sp.t} end @@ -88,11 +92,6 @@ module Peta StorySheet.play_list(story, author).first end - def self.collect_t story_sheet - r = StorySheet.find(:all, :conditions => ['story_id = ?', story_sheet.story_id], :order => 't') - r.map {|sp| sp.t} - end - def self.serial? ary i = 0 ary.compact.sort.each do |t| @@ -102,24 +101,38 @@ module Peta ary.compact.size == i end - def self.validate_t scroll_panel - ScrollPanel.serial?(ScrollPanel.collect_t(scroll_panel)) + def self.validate_t binder_id + self.serial?(self.collect_t(binder_id)) + end + + def binder_key + self.class.binder_key + end + + def binder_id + self.attributes[self.binder_key] end def insert_shift - ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and t >= ?', self.scroll_id, self.t]) + self.class.update_all('t = t + 1', + [self.binder_key + ' = ? and t >= ?', self.binder_id, self.t] + ) end def lesser_shift old_t self.t = 0 if self.t < 0 - ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and (t >= ? and t < ?)', self.scroll_id, self.t, old_t]) + self.class.update_all('t = t + 1', + [self.binder_key + ' = ? and (t >= ? and t < ?)', self.binder_id, self.t, old_t] + ) end def higher_shift old_t - nf = ScrollPanel.find_t(self.scroll_id, self.t) - max_t = ScrollPanel.max_t(self.scroll_id).to_i + nf = self.class.find_t(self.binder_id, self.t) + max_t = self.class.max_t(self.binder_id).to_i self.t = max_t if self.t > max_t - ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ? and t <= ?)', self.scroll_id, old_t, self.t]) + self.class.update_all('t = t - 1', + [self.binder_key + ' = ? and (t > ? and t <= ?)', self.binder_id, old_t, self.t] + ) end def update_shift old_t @@ -133,7 +146,7 @@ module Peta def rotate old_t = nil if self.new_record? if self.t.blank? - self.t = ScrollPanel.new_t self.scroll_id + self.t = self.class.new_t self.binder_id else self.insert_shift end @@ -145,15 +158,12 @@ module Peta end end - def allow? operators - return nil if self.scroll_id == nil or self.panel_id == nil - self.scroll.own?(operators) and self.panel.usable?(operators) - end - def destroy_and_shorten res = false - ScrollPanel.transaction do - ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ?)', self.scroll_id, self.t]) + self.class.transaction do + self.class.update_all('t = t - 1', + [self.binder_key + ' = ? and (t > ?)', self.binder_id, self.t] + ) raise ActiveRecord::Rollback unless self.destroy res = true end -- 2.11.0