diff --git a/app/models/issue.rb b/app/models/issue.rb index 60352d9..80468f2 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -1512,7 +1512,31 @@ class Issue < ActiveRecord::Base end if @custom_values_before_change # custom fields changes - custom_field_values.each {|c| + + # Re-insert removed custom fields with nil value + # TODO check if custom_field_values has to be fixed to take that in account + cf_values = custom_field_values.dup # Dupped because cached in an instance variable + custom_fields = {} + @custom_values_before_change.each{|i, v| + cfv = cf_values.detect {|c| c.custom_field_id == i} + next if cfv # On a not deleted custom field, or already re-added + + cf = custom_fields[i] + if cf.nil? + cf = CustomField.find_by_id(i) + end + + # Custom field has not been deleted in between + if cf + cfv = CustomFieldValue.new + cfv.custom_field = cf + cfv.value = nil + + cf_values << cfv + end + } + + cf_values.each {|c| before = @custom_values_before_change[c.custom_field_id] after = c.value next if before == after || (before.blank? && after.blank?)