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

[#64347] Allow viewing/editing calculated values without FF

Tobias Dillmannf0c119ca8587
diff --git a/app/components/admin/custom_fields/calculated_values/details_component.html.erb b/app/components/admin/custom_fields/calculated_values/details_component.html.erb
index 3f0b557de44..6bdceda3369 100644
--- a/app/components/admin/custom_fields/calculated_values/details_component.html.erb
+++ b/app/components/admin/custom_fields/calculated_values/details_component.html.erb
@@ -30,6 +30,10 @@ See COPYRIGHT and LICENSE files for more details.
<%=
component_wrapper do
flex_layout do |content|
+ content.with_row do
+ render BaseErrorsComponent.new(model, keys: %w[field_format], mb: 3)
+ end
+
content.with_row do
settings_primer_form_with(
model:,
diff --git a/app/components/settings/project_custom_field_sections/show_component.rb b/app/components/settings/project_custom_field_sections/show_component.rb
index 1230e1e449f..7f47572b426 100644
--- a/app/components/settings/project_custom_field_sections/show_component.rb
+++ b/app/components/settings/project_custom_field_sections/show_component.rb
@@ -39,10 +39,6 @@ module Settings
@project_custom_field_section = project_custom_field_section
@project_custom_fields = project_custom_field_section.custom_fields
- unless OpenProject::FeatureDecisions.calculated_value_project_attribute_active?
- @project_custom_fields = @project_custom_fields.where.not(field_format: "calculated_value")
- end
-
@first_and_last = first_and_last
end
diff --git a/app/controllers/admin/settings/project_custom_fields_controller.rb b/app/controllers/admin/settings/project_custom_fields_controller.rb
index 9699a1fed63..cf801f9ad2c 100644
--- a/app/controllers/admin/settings/project_custom_fields_controller.rb
+++ b/app/controllers/admin/settings/project_custom_fields_controller.rb
@@ -210,8 +210,6 @@ module Admin::Settings
def find_custom_field
@custom_field = ProjectCustomField.find(params[:id])
-
- render_404 if @custom_field.field_format_calculated_value? && !calculated_value_feature_flag?
end
def drop_success_streams(call)
@@ -224,9 +222,5 @@ module Admin::Settings
def include_sub_projects?
ActiveRecord::Type::Boolean.new.cast(params.to_unsafe_h[:project_custom_field_project_mapping][:include_sub_projects])
end
-
- def calculated_value_feature_flag?
- OpenProject::FeatureDecisions.calculated_value_project_attribute_active?
- end
end
end
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb
index ef66d647cf1..ce71762809a 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -70,8 +70,7 @@ class CustomField < ApplicationRecord
errors.add(:name, :taken) if name.in?(taken_names)
end
- validates :field_format, inclusion: { in: -> { OpenProject::CustomFieldFormat.available_formats } }
-
+ validate :validate_field_format_inclusion
validate :validate_default_value
validate :validate_regex
validate :validate_formula
@@ -108,6 +107,17 @@ class CustomField < ApplicationRecord
end
end
+ def validate_field_format_inclusion
+ available = OpenProject::CustomFieldFormat.available_formats
+ # When creating a new custom field, only the available formats are allowed.
+ # But you can edit and update existing custom fields, even if they have a field format that is disabled.
+ allowed = new_record? ? available : (available + OpenProject::CustomFieldFormat.disabled_formats).uniq
+
+ unless allowed.include?(field_format)
+ errors.add(:field_format, :inclusion)
+ end
+ end
+
def validate_default_value
# It is not possible to determine the validity of a value, when there is no valid format.
# another validation will take care of adding an error, but here we need to abort.
diff --git a/app/models/project_custom_field.rb b/app/models/project_custom_field.rb
index 5a21220fb53..df4c67428ff 100644
--- a/app/models/project_custom_field.rb
+++ b/app/models/project_custom_field.rb
@@ -40,18 +40,12 @@ class ProjectCustomField < CustomField
class << self
def visible(user = User.current, project: nil)
- cfs = if user.admin?
- all
- elsif user.allowed_in_any_project?(:select_project_custom_fields) || user.allowed_globally?(:add_project)
- where(admin_only: false)
- else
- where(admin_only: false).where(mappings_with_view_project_attributes_permission(user, project).exists)
- end
-
- if OpenProject::FeatureDecisions.calculated_value_project_attribute_active?
- cfs
+ if user.admin?
+ all
+ elsif user.allowed_in_any_project?(:select_project_custom_fields) || user.allowed_globally?(:add_project)
+ where(admin_only: false)
else
- cfs.where.not(field_format: "calculated_value")
+ where(admin_only: false).where(mappings_with_view_project_attributes_permission(user, project).exists)
end
end
diff --git a/config/initializers/custom_field_format.rb b/config/initializers/custom_field_format.rb
index 71971d18c13..329b8c06b43 100644
--- a/config/initializers/custom_field_format.rb
+++ b/config/initializers/custom_field_format.rb
@@ -94,5 +94,8 @@ OpenProject::CustomFieldFormat.map do |fields|
fields.register OpenProject::CustomFieldFormat.new("calculated_value",
label: :label_calculated_value,
only: %w(Project),
- order: 13)
+ order: 13,
+ enabled: lambda do
+ OpenProject::FeatureDecisions.calculated_value_project_attribute_active?
+ end)
end
diff --git a/lib/open_project/custom_field_format.rb b/lib/open_project/custom_field_format.rb
index 0ce379e26ff..a416dc33103 100644
--- a/lib/open_project/custom_field_format.rb
+++ b/lib/open_project/custom_field_format.rb
@@ -43,6 +43,7 @@ module OpenProject
only: nil,
multi_value_possible: false,
enterprise_feature: nil,
+ enabled: lambda { true },
formatter: "CustomValue::StringStrategy")
@name = name
@label = label
@@ -51,6 +52,7 @@ module OpenProject
@class_names = only
@multi_value_possible = multi_value_possible
@enterprise_feature = enterprise_feature
+ @enabled = enabled
@formatter = formatter
end
@@ -63,6 +65,14 @@ module OpenProject
Kernel.const_get(@formatter)
end
+ def enabled?
+ @enabled.call
+ end
+
+ def disabled?
+ !enabled?
+ end
+
class << self
def map(&)
yield self
@@ -74,14 +84,8 @@ module OpenProject
end
def available
- formats = registered.select do |_, format|
- !format.enterprise_feature || EnterpriseToken.allows_to?(format.enterprise_feature)
- end
-
- if OpenProject::FeatureDecisions.calculated_value_project_attribute_active?
- formats
- else
- formats.except("calculated_value")
+ registered.select do |_, format|
+ format.enabled? && (!format.enterprise_feature || EnterpriseToken.allows_to?(format.enterprise_feature))
end
end
@@ -100,6 +104,10 @@ module OpenProject
.sort_by(&:order)
.reject { |format| format.label.nil? }
end
+
+ def disabled_formats
+ registered.select { |_, format| format.disabled? }.keys
+ end
end
end
end
diff --git a/spec/features/admin/custom_fields/projects/calculated_value_spec.rb b/spec/features/admin/custom_fields/projects/calculated_value_spec.rb
index bb56be33f19..bbf28fd1a91 100644
--- a/spec/features/admin/custom_fields/projects/calculated_value_spec.rb
+++ b/spec/features/admin/custom_fields/projects/calculated_value_spec.rb
@@ -124,14 +124,20 @@ RSpec.describe "Edit project custom field calculated value", :js, with_flag: { c
end
end
- context "without the feature flag" do
- it "renders a 404" do
- allow(OpenProject::FeatureDecisions).to receive(:calculated_value_project_attribute_active?).and_return(false)
-
- login_as(admin)
- visit edit_admin_settings_project_custom_field_path(calculated_value)
-
- expect(page).to have_http_status(:not_found)
+ context "without the feature flag", with_flag: { calculated_value_project_attribute: false } do
+ let!(:calculated_value) { nil }
+
+ it "prevents saving a calculated value" do
+ expect do
+ login_as(admin)
+ visit new_admin_settings_project_custom_field_path(field_format: "calculated_value",
+ custom_field_section_id: section_for_input_fields.id)
+ fill_in("custom_field_name", with: "New calculated value")
+ fill_in("custom_field_formula", with: "1 + 1")
+ click_on "Save"
+
+ expect(page).to have_text("Format is not set to one of the allowed values.")
+ end.not_to change(CustomField, :count)
end
end
end
diff --git a/spec/features/admin/custom_fields/projects/index_spec.rb b/spec/features/admin/custom_fields/projects/index_spec.rb
index 6891df6bf9d..4d71c7c66e2 100644
--- a/spec/features/admin/custom_fields/projects/index_spec.rb
+++ b/spec/features/admin/custom_fields/projects/index_spec.rb
@@ -155,19 +155,19 @@ RSpec.describe "List project custom fields", :js do
end
end
- it "does not list calculated values if the feature flag is deactivated later" do
- # This spec tests that calculated values are no longer shown after the feature flag is deactivated.
+ it "lists calculated values even if the feature flag is deactivated later" do
+ # This spec tests that calculated values are still shown after the feature flag is deactivated.
# First, a custom field of type calculated value is created. This must be done while the feature flag is active,
# or else the model validation will fail.
# Next, we simulate that the feature flag is off:
allow(OpenProject::FeatureDecisions).to receive(:calculated_value_project_attribute_active?).and_return(false)
- # Revisit the page and check that the field is no longer listed:
+ # Revisit the page and check that the field is still listed:
cf_index_page.visit!
within_project_custom_field_section_container(section_for_input_fields) do
containers = page.all(".op-project-custom-field-container")
- expect(containers.last.text).not_to include(calculated_value_project_custom_field.name)
+ expect(containers.last.text).to include(calculated_value_project_custom_field.name)
end
end
end
diff --git a/spec/lib/open_project/custom_field_format_spec.rb b/spec/lib/open_project/custom_field_format_spec.rb
index de9eb9ed06f..7bc6da0066e 100644
--- a/spec/lib/open_project/custom_field_format_spec.rb
+++ b/spec/lib/open_project/custom_field_format_spec.rb
@@ -119,4 +119,18 @@ RSpec.describe OpenProject::CustomFieldFormat do
end
end
end
+
+ describe ".disabled_formats" do
+ it "returns disabled formats" do
+ formats = described_class.disabled_formats
+ expect(formats).to match_array(%w[calculated_value])
+ end
+
+ context "with calculated values feature flag enabled", with_flag: { calculated_value_project_attribute: true } do
+ it "returns no disabled formats" do
+ formats = described_class.disabled_formats
+ expect(formats).to be_empty
+ end
+ end
+ end
end