]> source.dussan.org Git - redmine.git/commitdiff
Disallow users to delete a version referenced by a custom field (#20159).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 27 Jun 2015 09:02:24 +0000 (09:02 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 27 Jun 2015 09:02:24 +0000 (09:02 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@14376 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/versions_controller.rb
app/models/version.rb
test/object_helpers.rb
test/unit/version_test.rb

index 37de29df8a44b0f870841f6f6670b991a47ed6a9..e6b1fec89132730f861df3dd9331c2f5f99b63db 100644 (file)
@@ -146,7 +146,7 @@ class VersionsController < ApplicationController
   end
 
   def destroy
-    if @version.fixed_issues.empty?
+    if @version.deletable?
       @version.destroy
       respond_to do |format|
         format.html { redirect_back_or_default settings_project_path(@project, :tab => 'versions') }
index b8c7b6f608d5441159f4ad48f4e72335af6e7f93..9910700f2bf07ffdc1dc7175a56e4cce7a659f61 100644 (file)
@@ -227,6 +227,10 @@ class Version < ActiveRecord::Base
     sharing != 'none'
   end
 
+  def deletable?
+    fixed_issues.empty? && !referenced_by_a_custom_field?
+  end
+
   private
 
   def load_issue_counts
@@ -288,4 +292,9 @@ class Version < ActiveRecord::Base
       progress
     end
   end
+
+  def referenced_by_a_custom_field?
+    CustomValue.joins(:custom_field).
+      where(:value => id.to_s, :custom_fields => {:field_format => 'version'}).any?
+  end
 end
index 7061e75c13db64281fc49ce1e8256da58a998890..74f82b68b4bcaebc89f07b9f83e10a6a21cbece3 100644 (file)
@@ -125,6 +125,7 @@ module ObjectHelpers
     @generated_version_name.succ!
     version = Version.new(attributes)
     version.name = @generated_version_name.dup if version.name.blank?
+    version.project ||= Project.find(1)
     yield version if block_given?
     version.save!
     version
index 6c1238eb07816dd6ee1f927618f3324645542260..b61061b85bfb537a7051771027ee7e3cfd194308 100644 (file)
@@ -235,6 +235,27 @@ class VersionTest < ActiveSupport::TestCase
     assert_equal @version, project_2_issue.fixed_version
   end
 
+  def test_deletable_should_return_true_when_not_referenced
+    version = Version.generate!
+
+    assert_equal true, version.deletable?
+  end
+
+  def test_deletable_should_return_false_when_referenced_by_an_issue
+    version = Version.generate!
+    Issue.generate!(:fixed_version => version)
+
+    assert_equal false, version.deletable?
+  end
+
+  def test_deletable_should_return_false_when_referenced_by_a_custom_field
+    version = Version.generate!
+    field = IssueCustomField.generate!(:field_format => 'version')
+    value = CustomValue.create!(:custom_field => field, :customized => Issue.first, :value => version.id)
+
+    assert_equal false, version.deletable?
+  end
+
   private
 
   def add_issue(version, attributes={})