diff --git app/models/journal.rb app/models/journal.rb index ec9c9d5..74200d1 100644 --- app/models/journal.rb +++ app/models/journal.rb @@ -228,7 +228,35 @@ class Journal < ActiveRecord::Base end if @custom_values_before_change # custom fields changes - journalized.custom_field_values.each {|c| + + ################## + # Smile fix #21623 : 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 = journalized.custom_field_values.dup # Dupped because cached in an instance variable + custom_fields = {} # cached locally + @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 + + if cf # Custom field has not been deleted in between + cfv = CustomFieldValue.new + cfv.custom_field = cf + cfv.value = nil + + # Re-add it in the list + cf_values << cfv + end + } + # END -- Smile fix #21623 + ######################### + + # Smile fix #21623 : journalized.custom_field_values -> cf_values + cf_values.each {|c| before = @custom_values_before_change[c.custom_field_id] after = c.value next if before == after || (before.blank? && after.blank?)