From 5f8e9d71182040473d4072241ce81fcadada497f 2009-12-06 10:28:20
From: Jean-Philippe Lang
")
end
+ def format_version_name(version)
+ if version.project == @project
+ h(version)
+ else
+ h("#{version.project} - #{version}")
+ end
+ end
+
def due_date_distance_in_words(date)
if date
l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date))
diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb
index 0f28cc0..1f74011 100644
--- a/app/helpers/issues_helper.rb
+++ b/app/helpers/issues_helper.rb
@@ -91,8 +91,8 @@ module IssuesHelper
c = IssueCategory.find_by_id(detail.value) and value = c.name if detail.value
c = IssueCategory.find_by_id(detail.old_value) and old_value = c.name if detail.old_value
when 'fixed_version_id'
- v = Version.find_by_id(detail.value) and value = v.name if detail.value
- v = Version.find_by_id(detail.old_value) and old_value = v.name if detail.old_value
+ v = Version.find_by_id(detail.value) and value = format_version_name(v) if detail.value
+ v = Version.find_by_id(detail.old_value) and old_value = format_version_name(v) if detail.old_value
when 'estimated_hours'
value = "%0.02f" % detail.value.to_f unless detail.value.blank?
old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb
index 07ba23d..b675f6b 100644
--- a/app/helpers/projects_helper.rb
+++ b/app/helpers/projects_helper.rb
@@ -18,7 +18,7 @@
module ProjectsHelper
def link_to_version(version, options = {})
return '' unless version && version.is_a?(Version)
- link_to h(version.name), { :controller => 'versions', :action => 'show', :id => version }, options
+ link_to_if version.visible?, format_version_name(version), { :controller => 'versions', :action => 'show', :id => version }, options
end
def project_settings_tabs
@@ -69,4 +69,27 @@ module ProjectsHelper
end
s
end
+
+ # Returns a set of options for a select field, grouped by project.
+ def version_options_for_select(versions, selected=nil)
+ grouped = Hash.new {|h,k| h[k] = []}
+ versions.each do |version|
+ grouped[version.project.name] << [h(version.name), version.id]
+ end
+ # Add in the selected
+ if selected && !versions.include?(selected)
+ grouped[selected.project.name] << [h(selected.name), selected.id]
+ end
+
+ if grouped.keys.size > 1
+ grouped_options_for_select(grouped, selected && selected.id)
+ else
+ options_for_select(grouped.values.first, selected && selected.id)
+ end
+ end
+
+ def format_version_sharing(sharing)
+ sharing = 'none' unless Version::VERSION_SHARINGS.include?(sharing)
+ l("label_version_sharing_#{sharing}")
+ end
end
diff --git a/app/models/issue.rb b/app/models/issue.rb
index dac64cb..f75391f 100644
--- a/app/models/issue.rb
+++ b/app/models/issue.rb
@@ -100,7 +100,10 @@ class Issue < ActiveRecord::Base
# reassign to the category with same name if any
new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
issue.category = new_category
- issue.fixed_version = nil
+ # Keep the fixed_version if it's still valid in the new_project
+ unless new_project.shared_versions.include?(issue.fixed_version)
+ issue.fixed_version = nil
+ end
issue.project = new_project
end
if new_tracker
@@ -242,7 +245,7 @@ class Issue < ActiveRecord::Base
# Versions that the issue can be assigned to
def assignable_versions
- @assignable_versions ||= (project.versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort
+ @assignable_versions ||= (project.shared_versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort
end
# Returns true if this issue is blocked by another issue that is still open
@@ -336,6 +339,23 @@ class Issue < ActiveRecord::Base
s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id
s
end
+
+ # Update all issues so their versions are not pointing to a
+ # fixed_version that is outside of the issue's project hierarchy.
+ #
+ # OPTIMIZE: does a full table scan of Issues with a fixed_version.
+ def self.update_fixed_versions_from_project_hierarchy_change
+ Issue.all(:conditions => ['fixed_version_id IS NOT NULL'],
+ :include => [:project, :fixed_version]
+ ).each do |issue|
+ next if issue.project.nil? || issue.fixed_version.nil?
+ unless issue.project.shared_versions.include?(issue.fixed_version)
+ issue.init_journal(User.current)
+ issue.fixed_version = nil
+ issue.save
+ end
+ end
+ end
private
diff --git a/app/models/project.rb b/app/models/project.rb
index 5cc8ab9..77de59f 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -219,13 +219,20 @@ class Project < ActiveRecord::Base
self.status == STATUS_ACTIVE
end
- # Archives the project and its descendants recursively
+ # Archives the project and its descendants
def archive
- # Archive subprojects if any
- children.each do |subproject|
- subproject.archive
+ # Check that there is no issue of a non descendant project that is assigned
+ # to one of the project or descendant versions
+ v_ids = self_and_descendants.collect {|p| p.version_ids}.flatten
+ if v_ids.any? && Issue.find(:first, :include => :project,
+ :conditions => ["(#{Project.table_name}.lft < ? OR #{Project.table_name}.rgt > ?)" +
+ " AND #{Issue.table_name}.fixed_version_id IN (?)", lft, rgt, v_ids])
+ return false
end
- update_attribute :status, STATUS_ARCHIVED
+ Project.transaction do
+ archive!
+ end
+ true
end
# Unarchives the project
@@ -297,6 +304,7 @@ class Project < ActiveRecord::Base
# move_to_child_of adds the project in last (ie.right) position
move_to_child_of(p)
end
+ Issue.update_fixed_versions_from_project_hierarchy_change
true
else
# Can not move to the given target
@@ -324,6 +332,19 @@ class Project < ActiveRecord::Base
end
end
+ # Returns a scope of the Versions used by the project
+ def shared_versions
+ @shared_versions ||=
+ Version.scoped(:include => :project,
+ :conditions => "#{Project.table_name}.id = #{id}" +
+ " OR (#{Project.table_name}.status = #{Project::STATUS_ACTIVE} AND (" +
+ " #{Version.table_name}.sharing = 'system'" +
+ " OR (#{Project.table_name}.lft >= #{root.lft} AND #{Project.table_name}.rgt <= #{root.rgt} AND #{Version.table_name}.sharing = 'tree')" +
+ " OR (#{Project.table_name}.lft < #{lft} AND #{Project.table_name}.rgt > #{rgt} AND #{Version.table_name}.sharing = 'hierarchy')" +
+ " OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt} AND #{Version.table_name}.sharing IN ('hierarchy', 'descendants'))" +
+ "))")
+ end
+
# Returns a hash of project users grouped by role
def users_by_role
members.find(:all, :include => [:user, :roles]).inject({}) do |h, m|
@@ -605,4 +626,12 @@ class Project < ActiveRecord::Base
self.time_entry_activities.active
end
end
+
+ # Archives subprojects recursively
+ def archive!
+ children.each do |subproject|
+ subproject.send :archive!
+ end
+ update_attribute :status, STATUS_ARCHIVED
+ end
end
diff --git a/app/models/query.rb b/app/models/query.rb
index f7aeb0e..2e1680a 100644
--- a/app/models/query.rb
+++ b/app/models/query.rb
@@ -200,8 +200,8 @@ class Query < ActiveRecord::Base
unless @project.issue_categories.empty?
@available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => @project.issue_categories.collect{|s| [s.name, s.id.to_s] } }
end
- unless @project.versions.empty?
- @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => @project.versions.sort.collect{|s| [s.name, s.id.to_s] } }
+ unless @project.shared_versions.empty?
+ @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => @project.shared_versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } }
end
unless @project.descendants.active.empty?
@available_filters["subproject_id"] = { :type => :list_subprojects, :order => 13, :values => @project.descendants.visible.collect{|s| [s.name, s.id.to_s] } }
diff --git a/app/models/version.rb b/app/models/version.rb
index e63ed46..add0dc7 100644
--- a/app/models/version.rb
+++ b/app/models/version.rb
@@ -17,6 +17,7 @@
class Version < ActiveRecord::Base
before_destroy :check_integrity
+ after_update :update_issue_versions
belongs_to :project
has_many :fixed_issues, :class_name => 'Issue', :foreign_key => 'fixed_version_id'
acts_as_customizable
@@ -24,15 +25,24 @@ class Version < ActiveRecord::Base
:delete_permission => :manage_files
VERSION_STATUSES = %w(open locked closed)
+ VERSION_SHARINGS = %w(none descendants hierarchy tree system)
validates_presence_of :name
validates_uniqueness_of :name, :scope => [:project_id]
validates_length_of :name, :maximum => 60
validates_format_of :effective_date, :with => /^\d{4}-\d{2}-\d{2}$/, :message => :not_a_date, :allow_nil => true
validates_inclusion_of :status, :in => VERSION_STATUSES
+ validates_inclusion_of :sharing, :in => VERSION_SHARINGS
named_scope :open, :conditions => {:status => 'open'}
-
+ named_scope :visible, lambda {|*args| { :include => :project,
+ :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
+
+ # Returns true if +user+ or current user is allowed to view the version
+ def visible?(user=User.current)
+ user.allowed_to?(:view_issues, self.project)
+ end
+
def start_date
effective_date
end
@@ -54,6 +64,10 @@ class Version < ActiveRecord::Base
def closed?
status == 'closed'
end
+
+ def open?
+ status == 'open'
+ end
# Returns true if the version is completed: due date reached and no open issues
def completed?
@@ -120,10 +134,38 @@ class Version < ActiveRecord::Base
end
end
+ # Returns the sharings that +user+ can set the version to
+ def allowed_sharings(user = User.current)
+ VERSION_SHARINGS.select do |s|
+ if sharing == s
+ true
+ else
+ case s
+ when 'system'
+ # Only admin users can set a systemwide sharing
+ user.admin?
+ when 'hierarchy', 'tree'
+ # Only users allowed to manage versions of the root project can
+ # set sharing to hierarchy or tree
+ project.nil? || user.allowed_to?(:manage_versions, project.root)
+ else
+ true
+ end
+ end
+ end
+ end
+
private
def check_integrity
raise "Can't delete version" if self.fixed_issues.find(:first)
end
+
+ # Update the issue's fixed versions. Used if a version's sharing changes.
+ def update_issue_versions
+ if sharing_changed?
+ Issue.update_fixed_versions_from_project_hierarchy_change
+ end
+ end
# Returns the average estimated time of assigned issues
# or 1 if no issue has an estimated time
diff --git a/app/views/issues/_attributes.rhtml b/app/views/issues/_attributes.rhtml
index b27c562..b9d17ac 100644
--- a/app/views/issues/_attributes.rhtml
+++ b/app/views/issues/_attributes.rhtml
@@ -19,7 +19,7 @@
:tabindex => 199) if authorize_for('projects', 'add_issue_category') %>
<%= f.select :fixed_version_id, (@issue.assignable_versions.collect {|v| [v.name, v.id]}), :include_blank => true %>
+<%= f.select :fixed_version_id, version_options_for_select(@issue.assignable_versions, @issue.fixed_version), :include_blank => true %>
<% end %> diff --git a/app/views/issues/bulk_edit.rhtml b/app/views/issues/bulk_edit.rhtml index de82e18..f428566 100644 --- a/app/views/issues/bulk_edit.rhtml +++ b/app/views/issues/bulk_edit.rhtml @@ -31,7 +31,7 @@ + version_options_for_select(@project.shared_versions.open)) %>diff --git a/app/views/issues/context_menu.rhtml b/app/views/issues/context_menu.rhtml index c67c1bc..e408e3b 100644 --- a/app/views/issues/context_menu.rhtml +++ b/app/views/issues/context_menu.rhtml @@ -38,12 +38,12 @@ <% end -%> - <% unless @project.nil? || @project.versions.open.empty? -%> + <% unless @project.nil? || @project.shared_versions.open.empty? -%>
<%= submit_tag l(:button_apply), :class => 'button-small', :name => nil %>
diff --git a/app/views/projects/changelog.rhtml b/app/views/projects/changelog.rhtml index a1cd99b..0779e06 100644 --- a/app/views/projects/changelog.rhtml +++ b/app/views/projects/changelog.rhtml @@ -6,15 +6,15 @@ <% @versions.each do |version| %> <%= tag 'a', :name => version.name %> -<%= format_date(version.effective_date) %>
<% end %><%=h version.description %>
- <% issues = version.fixed_issues.find(:all, - :include => [:status, :tracker, :priority], - :conditions => ["#{IssueStatus.table_name}.is_closed=? AND #{Issue.table_name}.tracker_id in (#{@selected_tracker_ids.join(',')})", true], - :order => "#{Tracker.table_name}.position") unless @selected_tracker_ids.empty? + <% issues = version.fixed_issues.visible.find(:all, + :include => [:status, :tracker, :priority], + :conditions => ["#{Issue.table_name}.project_id = ? AND #{IssueStatus.table_name}.is_closed=? AND #{Issue.table_name}.tracker_id in (?)", @project.id, true, @selected_tracker_ids], + :order => "#{Tracker.table_name}.position") unless @selected_tracker_ids.empty? issues ||= [] %> <% if !issues.empty? %> @@ -33,11 +33,15 @@<%= submit_tag l(:button_apply), :class => 'button-small' %>
<% end %>