Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

calculating values on change of dependants

Ivan Kuchin20b9520d3382
diff --git a/app/models/acts_as_customizable/calculated_value.rb b/app/models/acts_as_customizable/calculated_value.rb
new file mode 100644
index 00000000000..d6b5aeb493a
--- /dev/null
+++ b/app/models/acts_as_customizable/calculated_value.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module ActsAsCustomizable::CalculatedValue
+ extend ActiveSupport::Concern
+
+ included do
+ def calculate_custom_fields(custom_fields)
+ return if custom_fields.empty?
+
+ unless custom_fields.all?(&:field_format_calculated_value?)
+ fail ArgumentError,
+ "Expected array of calculated value custom fields"
+ end
+
+ given = calculated_value_fields_referenced_values(custom_fields)
+ to_compute = custom_fields.to_h { [it.column_name, it.formula_str_without_patterns] }
+
+ calculator = CustomField::CalculatedValue.calculator_instance
+ calculator.store(given)
+ result = calculator.solve(to_compute).reject { |_, value| value == :undefined }
+
+ self.custom_field_values = custom_fields.to_h { [it.id, result[it.column_name]] }
+ end
+
+ private
+
+ def calculated_value_fields_referenced_values(custom_fields)
+ given_cf_ids = custom_fields.flat_map(&:formula_referenced_custom_field_ids).uniq - custom_fields.map(&:id)
+
+ custom_field_values
+ .select { it.custom_field_id.in?(given_cf_ids) }
+ .to_h { [it.custom_field.column_name, it.typed_value] }
+ end
+ end
+end
diff --git a/app/models/projects/custom_fields.rb b/app/models/projects/custom_fields.rb
index 6fe734a804e..126b020d02c 100644
--- a/app/models/projects/custom_fields.rb
+++ b/app/models/projects/custom_fields.rb
@@ -31,6 +31,8 @@
module Projects::CustomFields
extend ActiveSupport::Concern
+ include ActsAsCustomizable::CalculatedValue
+
attr_accessor :_limit_custom_fields_validation_to_section_id
included do
diff --git a/app/services/projects/concerns/set_calculated_custom_field_values.rb b/app/services/projects/concerns/set_calculated_custom_field_values.rb
new file mode 100644
index 00000000000..906dd8ce332
--- /dev/null
+++ b/app/services/projects/concerns/set_calculated_custom_field_values.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Projects::Concerns
+ module SetCalculatedCustomFieldValues
+ private
+
+ def set_attributes(params)
+ super(params_except_calculated_fields(params)).tap do
+ update_calculated_value_custom_fields
+ end
+ end
+
+ def params_except_calculated_fields(params)
+ custom_field_value_params = params[:custom_field_values]
+ return params unless custom_field_value_params
+
+ calculated_field_ids = model.available_custom_fields.select(&:field_format_calculated_value?).map(&:id)
+ custom_field_value_params = custom_field_value_params.reject { |id, _| id.to_s.to_i.in?(calculated_field_ids) }
+
+ params.merge(custom_field_values: custom_field_value_params)
+ end
+
+ def update_calculated_value_custom_fields
+ changed_cf_ids = model.custom_values.select(&:changed?).map(&:custom_field_id)
+ affected_cfs = model.available_custom_fields.affected_calculated_fields(changed_cf_ids)
+
+ model.calculate_custom_fields(affected_cfs)
+ end
+ end
+end
diff --git a/app/services/projects/set_attributes_service.rb b/app/services/projects/set_attributes_service.rb
index 77adacfc20d..dabedc49215 100644
--- a/app/services/projects/set_attributes_service.rb
+++ b/app/services/projects/set_attributes_service.rb
@@ -30,6 +30,8 @@
module Projects
class SetAttributesService < ::BaseServices::SetAttributes
+ prepend Projects::Concerns::SetCalculatedCustomFieldValues
+
private
def set_attributes(params)