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] refactor hidden calculated values w/o feature flag

Tobias Dillmannf49ea9998262
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 d2e1ee86a50..1230e1e449f 100644
--- a/app/components/settings/project_custom_field_sections/show_component.rb
+++ b/app/components/settings/project_custom_field_sections/show_component.rb
@@ -38,6 +38,11 @@ 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 cf801f9ad2c..9699a1fed63 100644
--- a/app/controllers/admin/settings/project_custom_fields_controller.rb
+++ b/app/controllers/admin/settings/project_custom_fields_controller.rb
@@ -210,6 +210,8 @@ 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)
@@ -222,5 +224,9 @@ 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 8e3ab0d0f26..ef66d647cf1 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -52,14 +52,6 @@ class CustomField < ApplicationRecord
scope :hierarchy_root_and_children, -> { includes(hierarchy_root: { children: :children }) }
scope :required, -> { where(is_required: true) }
- default_scope -> {
- if OpenProject::FeatureDecisions.calculated_value_project_attribute_active?
- all
- else
- where.not(field_format: "calculated_value")
- end
- }
-
acts_as_list scope: [:type]
validates :field_format, presence: true
diff --git a/app/models/project_custom_field.rb b/app/models/project_custom_field.rb
index df4c67428ff..5a21220fb53 100644
--- a/app/models/project_custom_field.rb
+++ b/app/models/project_custom_field.rb
@@ -40,12 +40,18 @@ class ProjectCustomField < CustomField
class << self
def visible(user = User.current, project: nil)
- if user.admin?
- all
- elsif user.allowed_in_any_project?(:select_project_custom_fields) || user.allowed_globally?(:add_project)
- where(admin_only: false)
+ 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
else
- where(admin_only: false).where(mappings_with_view_project_attributes_permission(user, project).exists)
+ cfs.where.not(field_format: "calculated_value")
end
end
diff --git a/spec/contracts/project_custom_field_project_mappings/base_contract_spec.rb b/spec/contracts/project_custom_field_project_mappings/base_contract_spec.rb
index d50157db756..951288f98ea 100644
--- a/spec/contracts/project_custom_field_project_mappings/base_contract_spec.rb
+++ b/spec/contracts/project_custom_field_project_mappings/base_contract_spec.rb
@@ -49,10 +49,12 @@ RSpec.describe ProjectCustomFieldProjectMappings::BaseContract do
end
context "with non-visible custom field and admin user" do
- let(:project_custom_field) { build_stubbed(:project_custom_field, admin_only: true) }
+ let(:project_custom_field) { create(:project_custom_field, admin_only: true) }
before do
- allow(ProjectCustomField).to receive(:all).and_return([project_custom_field])
+ # Stub `all` to return an ActiveRecord::Relation, not an array, so that further query chaining
+ # (e.g., .where) works as expected.
+ allow(ProjectCustomField).to receive(:all).and_return(ProjectCustomField.where(id: project_custom_field.id))
end
it_behaves_like "contract is valid"