X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fmodels%2Fscroll_panel.rb;h=3db39656efd27f34dd18aa2cfbb50a2f2a6e29e8;hb=0174862d186425888ff64ffe1fa52c6266708049;hp=b628edf1ffc68db3ef9dd6317c9f43692daccb82;hpb=387de02ada617308cad50c4bd35e8dbb05c28be4;p=pettanr%2Fpettanr.git diff --git a/app/models/scroll_panel.rb b/app/models/scroll_panel.rb index b628edf1..3db39656 100644 --- a/app/models/scroll_panel.rb +++ b/app/models/scroll_panel.rb @@ -1,4 +1,5 @@ -class ScrollPanel < Pettanr::Content +class ScrollPanel < Peta::Leaf + load_manifest belongs_to :author belongs_to :panel belongs_to :scroll @@ -8,27 +9,18 @@ class ScrollPanel < Pettanr::Content validates :author_id, :presence => true, :numericality => true, :existence => {:both => false} validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0} - def owner_model - self.scroll - end - def supply_default self.scroll_id = nil self.panel_id = nil self.t = nil end - def overwrite au - return false unless au - self.author_id = au.id - end - - def visible? operators - return false unless super - self.owner_model.visible? operators + def overwrite operators + return false unless operators.author + self.author_id = operators.author.id end - def self.list_order + def self.public_list_order 'scroll_panels.updated_at desc' end @@ -36,6 +28,14 @@ class ScrollPanel < Pettanr::Content 'scrolls.visible > 0' end + def self.by_author_list_includes + { + :scroll => { + :author => {} + } + } + end + def self.play_list_where cid ['scroll_panels.scroll_id = ?', cid] end @@ -111,85 +111,15 @@ class ScrollPanel < Pettanr::Content 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? + def allow? operators return nil if self.scroll_id == nil or self.panel_id == nil - self.scroll.own?(self.author) and self.panel.usable?(self.author) + self.scroll.own?(operators) and self.panel.usable?(operators) end - def store old_t = nil + def store operators, old_t = nil res = false - ScrollPanel.transaction do - case self.allow? + self.class.transaction do + case self.allow? operators when true self.rotate old_t when false @@ -198,7 +128,7 @@ class ScrollPanel < Pettanr::Content end res = self.save raise ActiveRecord::Rollback unless res - res = ScrollPanel.validate_t(self) + res = self.class.validate_t(self.scroll_id) unless res self.errors.add :t, 'unserialized' raise ActiveRecord::Rollback @@ -207,14 +137,4 @@ class ScrollPanel < Pettanr::Content 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