move acts_as_list to backlogs_list which wraps acts_as_list (scoped to :project) and also has a custom move_after method. Impediment and Story include backlogs_list

* move_after method silently updates position on elements without a position
pull/6827/head
Jens Ulferts 14 years ago
parent 36cd838a41
commit cea73e8064
  1. 37
      app/models/impediment.rb
  2. 34
      app/models/story.rb
  3. 68
      lib/backlogs_list.rb

@ -1,7 +1,10 @@
require_dependency 'backlogs_list'
class Impediment < Task
unloadable
acts_as_list :scope => :project
include Backlogs::List
acts_as_backlogs_list(Setting.plugin_redmine_backlogs[:task_tracker])
after_save :update_blocks_list
@ -40,38 +43,6 @@ class Impediment < Task
@blocks_ids_list ||= relations_from.select{ |rel| rel.relation_type == IssueRelation::TYPE_BLOCKS }.collect(&:issue_to_id)
end
def move_after(prev_id)
#our super's, move after is using awesome_nested_set which is inapproriate for impediments.
#Impediments always have a parent_id of nil, it is their defining criteria.
#Therefore we use the acts_as_list method
#TODO: create a module to be included by story and impediment, for now this code is just copy&paste
# remove so the potential 'prev' has a correct position
remove_from_list
begin
prev = self.class.find(prev_id)
rescue ActiveRecord::RecordNotFound
prev = nil
end
# if it's the first story, move it to the 1st position
if prev.blank?
insert_at
move_to_top
# if its predecessor has no position (shouldn't happen), make it
# the last story
elsif !prev.in_list?
insert_at
move_to_bottom
# there's a valid predecessor
else
insert_at(prev.position + 1)
end
end
private
def update_blocks_list

@ -1,10 +1,13 @@
require_dependency 'backlogs_list'
class Story < Issue
unloadable
acts_as_list :scope => :project
include Backlogs::List
acts_as_backlogs_list(Setting.plugin_redmine_backlogs[:story_trackers])
def self.condition(project_id, sprint_id, extras=[])
if sprint_id.nil?
if sprint_id.nil?
c = ["
project_id = ?
and tracker_id in (?)
@ -95,33 +98,6 @@ class Story < Issue
end
end
def move_after(prev_id)
# remove so the potential 'prev' has a correct position
remove_from_list
begin
prev = self.class.find(prev_id)
rescue ActiveRecord::RecordNotFound
prev = nil
end
# if it's the first story, move it to the 1st position
if prev.blank?
insert_at
move_to_top
# if its predecessor has no position (shouldn't happen), make it
# the last story
elsif !prev.in_list?
insert_at
move_to_bottom
# there's a valid predecessor
else
insert_at(prev.position + 1)
end
end
def set_points(p)
self.init_journal(User.current)

@ -0,0 +1,68 @@
module Backlogs
module List
unloadable
def self.included(base)
base.send(:include, InstanceMethods)
base.send(:extend, ClassMethods)
end
module ClassMethods
def acts_as_backlogs_list(trackers)
self.class_eval do
acts_as_list :scope => :project #TODO: consider changing the scope to project, version and tracker
@trackers = trackers
end
end
end
module InstanceMethods
def move_after(prev_id)
# remove so the potential 'prev' has a correct position
remove_from_list
begin
prev = self.class.find(prev_id)
rescue ActiveRecord::RecordNotFound
prev = nil
end
# if it's the first story, move it to the 1st position
if prev.blank?
insert_at
move_to_top
# if its predecessor has no position, create an order on position silently.
# This can happen when sorting inside a version for the first time after backlogs was activated
# and there have already been items inside the version at the time of backlogs activation
elsif !prev.in_list?
prev_pos = set_default_prev_positions_silently(prev)
insert_at(prev_pos += 1)
# there's a valid predecessor
else
insert_at(prev.position + 1)
end
end
private
def set_default_prev_positions_silently(prev)
stories = self.class.find(:all, :conditions => {:fixed_version_id => self.fixed_version_id, :tracker_id => @trackers})
self.class.record_timestamps = false #temporarily turn off column updates
highest_pos = stories.sort_by{|s| s.position ? s.position : 0}.collect(&:position).last
highest_pos = 0 if highest_pos.nil?
stories.sort_by { |s| s.id }.each do |story|
next if story.in_list? || story.id > prev.id || self.id == story.id
story.insert_at(highest_pos += 1)
end
self.class.record_timestamps = true #turning updates back on
highest_pos
end
end
end
end
Loading…
Cancel
Save