Skip to content

Commit

Permalink
adding a cf via form configuration no longer enables the cf in all pr…
Browse files Browse the repository at this point in the history
…ojects
  • Loading branch information
ulferts committed Oct 25, 2024
1 parent 1657091 commit 1a3a5bd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 72 deletions.
10 changes: 1 addition & 9 deletions app/controllers/types_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def update
.new(@type, current_user)
.call(permitted_type_params) do |call|
call.on_success do
redirect_to_type_tab_path(@type, update_success_message)
redirect_to_type_tab_path(@type, t(:notice_successful_update))
end

call.on_failure do |result|
Expand Down Expand Up @@ -151,14 +151,6 @@ def show_local_breadcrumb
false
end

def update_success_message
if params[:tab].in?(%w[form_configuration projects])
t(:notice_successful_update_custom_fields_added_to_type)
else
t(:notice_successful_update)
end
end

def destroy_error_message
if @type.is_standard?
t(:error_can_not_delete_standard_type)
Expand Down
29 changes: 12 additions & 17 deletions app/services/base_type_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ def set_params_and_validate(params)
set_milestone_param(params) unless params[:is_milestone].nil?
set_active_custom_fields

if params[:project_ids].present?
set_active_custom_fields_for_project_ids(params[:project_ids])
end
set_active_custom_fields_for_project_ids(params[:project_ids]) if params[:project_ids].present?

set_scalar_params(params)

Expand Down Expand Up @@ -167,24 +165,21 @@ def transform_query_group(group)
# for this type. If a custom field is not in a group, it is removed from the
# custom_field_ids list.
def set_active_custom_fields
new_cf_ids_to_add = active_custom_field_ids - type.custom_field_ids
type.custom_field_ids = active_custom_field_ids
set_active_custom_fields_for_projects(type.projects,
new_cf_ids_to_add)
active_cf_ids = []

type.attribute_groups.each do |group|
group.members.each do |attribute|
if CustomField.custom_field_attribute? attribute
active_cf_ids << attribute.gsub(/^custom_field_/, "").to_i
end
end
end

type.custom_field_ids = active_cf_ids.uniq
end

def active_custom_field_ids
@active_custom_field_ids ||= begin
active_cf_ids = []

type.attribute_groups.each do |group|
group.members.each do |attribute|
if CustomField.custom_field_attribute? attribute
active_cf_ids << attribute.gsub(/^custom_field_/, "").to_i
end
end
end
active_cf_ids.uniq
end
end

Expand Down
3 changes: 0 additions & 3 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2960,9 +2960,6 @@ en:
notice_successful_update_custom_fields_added_to_project: |
Successful update. The custom fields of the activated types are automatically activated
on the work package form. <a href="%{url}" target="_blank">See more</a>.
notice_successful_update_custom_fields_added_to_type: |
Successful update. The active custom fields are automatically activated for
the associated projects of this type.
notice_to_many_principals_to_display: "There are too many results.\nNarrow down the search by typing in the name of the new member (or group)."
notice_user_missing_authentication_method: User has yet to choose a password or another way to sign in.
notice_user_invitation_resent: An invitation has been sent to %{email}.
Expand Down
13 changes: 0 additions & 13 deletions spec/features/types/form_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,19 +305,6 @@ def add_cf_to_group
context "if inactive in project" do
it "can be added to the type, but is not shown" do
add_cf_to_group
# Disable in project, should be invisible
# This step is necessary, since we auto-activate custom fields
# when adding them to the form configuration
project_settings_page.visit_tab!("custom_fields")

expect(page).to have_css(".custom-field-#{custom_field.id} td", text: "MyNumber")
expect(page).to have_css(".custom-field-#{custom_field.id} td", text: type.name)

id_checkbox = find("#project_work_package_custom_field_ids_#{custom_field.id}")
expect(id_checkbox).to be_checked
id_checkbox.set(false)

click_button "Save"

# Visit work package with that type
wp_page.visit!
Expand Down
65 changes: 35 additions & 30 deletions spec/services/shared_type_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,10 @@
type.projects = create_list :project, 2
end

it "enables the custom fields in the projects" do
it "does not enable the custom fields in the projects (used to be different until OP 15.0)" do
expect { service_call }
.to change { Project.where(id: type.project_ids).map(&:work_package_custom_fields) }
.not_to change { Project.where(id: type.project_ids).map(&:work_package_custom_field_ids) }
.from([[], []])
.to([[cf1, cf2], [cf1, cf2]])
end

context "when a custom field is already associated with the type" do
before do
type.custom_field_ids = [cf1.id]
end

it "enables the new custom field only" do
expect { service_call }
.to change { Project.where(id: type.project_ids).map(&:work_package_custom_fields) }
.from([[], []])
.to([[cf2], [cf2]])
end
end

context "when all custom fields are already associated with the type" do
Expand All @@ -189,8 +175,9 @@

context "when a project is being set on the type" do
let(:projects) { create_list(:project, 2) }
let(:active_project) { projects.first }
let(:project_ids) { { project_ids: [*projects.map { |p| p.id.to_s }, ""] } }
let(:active_project) { create(:project) }
let(:new_project) { create(:project) }
let(:project_ids) { { project_ids: [*[active_project, new_project].map { |p| p.id.to_s }, ""] } }
let(:params) do
attribute_groups.merge(project_ids)
end
Expand All @@ -199,23 +186,35 @@
type.projects << active_project
end

it "enables the custom fields for all the projects" do
it "enables the custom field on the newly added project" do
expect { service_call }
.to change { Project.where(id: type.project_ids).map(&:work_package_custom_fields) }
.from([[]])
.to([[cf1, cf2], [cf1, cf2]])
.to change { Project.find(new_project.id).work_package_custom_field_ids }
.from([])
.to([cf1.id, cf2.id])
end

it "does not enable the custom fields on the project the type was already active in" do
expect { service_call }
.not_to change { Project.find(active_project.id).work_package_custom_field_ids }
.from([])
end

context "when a custom field is already associated with the type" do
before do
type.custom_field_ids = [cf1.id]
end

it "enables the new cf for the existing project and enables both cfs for the new project" do
it "enables the custom field on the newly added project" do
expect { service_call }
.to change { Project.where(id: type.project_ids).map(&:work_package_custom_fields) }
.from([[]])
.to([[cf2], [cf1, cf2]])
.to change { Project.find(new_project.id).work_package_custom_field_ids }
.from([])
.to([cf1.id, cf2.id])
end

it "does not enable the custom fields on the project the type was already active in" do
expect { service_call }
.not_to change { Project.find(active_project.id).work_package_custom_field_ids }
.from([])
end
end

Expand All @@ -226,11 +225,17 @@
type.custom_field_ids = [cf1.id, cf2.id]
end

it "enables the custom fields in the new project only" do
it "enables the custom field on the newly added project" do
expect { service_call }
.to change { Project.find(new_project.id).work_package_custom_field_ids }
.from([])
.to([cf1.id, cf2.id])
end

it "does not enable the custom fields on the project the type was already active in" do
expect { service_call }
.to change { Project.where(id: type.project_ids).map(&:work_package_custom_fields) }
.from([[]])
.to([[], [cf1, cf2]])
.not_to change { Project.find(active_project.id).work_package_custom_field_ids }
.from([])
end
end
end
Expand Down

0 comments on commit 1a3a5bd

Please sign in to comment.