From 86ab55b323cfd037568b53163bf376fa6fb85a19 Mon Sep 17 00:00:00 2001 From: Markus Kahl Date: Fri, 4 Nov 2016 15:42:46 +0100 Subject: [PATCH] update type attribute visibility on cf changes cf = custom field --- app/controllers/custom_fields_controller.rb | 6 +++- app/controllers/projects_controller.rb | 8 +++-- app/helpers/types_helper.rb | 23 ++++++++++++ .../custom_fields_controller_spec.rb | 25 +++++++++++++ spec/controllers/projects_controller_spec.rb | 36 ++++++++++++++----- 5 files changed, 87 insertions(+), 11 deletions(-) diff --git a/app/controllers/custom_fields_controller.rb b/app/controllers/custom_fields_controller.rb index ae0d9390a7e..7224b889dea 100644 --- a/app/controllers/custom_fields_controller.rb +++ b/app/controllers/custom_fields_controller.rb @@ -60,7 +60,11 @@ class CustomFieldsController < ApplicationController def update if @custom_field.update_attributes(@custom_field_params) - flash[:notice] = l(:notice_successful_update) + @custom_field.types.each do |type| + TypesHelper.update_type_attribute_visibility! type + end + + flash[:notice] = t(:notice_successful_update) call_hook(:controller_custom_fields_edit_after_save, custom_field: @custom_field) redirect_to custom_fields_path(tab: @custom_field.class.name) else diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 1b3d7b04b52..a2c68a68326 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -207,9 +207,13 @@ class ProjectsController < ApplicationController Project.transaction do @project.work_package_custom_field_ids = permitted_params.project[:work_package_custom_field_ids] if @project.save - flash[:notice] = l(:notice_successful_update) + @project.work_package_custom_fields.flat_map(&:types).uniq.each do |type| + TypesHelper.update_type_attribute_visibility! type + end + + flash[:notice] = t(:notice_successful_update) else - flash[:error] = l(:notice_project_cannot_update_custom_fields, + flash[:error] = t(:notice_project_cannot_update_custom_fields, errors: @project.errors.full_messages.join(', ')) raise ActiveRecord::Rollback end diff --git a/app/helpers/types_helper.rb b/app/helpers/types_helper.rb index 5493c5bc403..4c347af2a44 100644 --- a/app/helpers/types_helper.rb +++ b/app/helpers/types_helper.rb @@ -111,6 +111,29 @@ module ::TypesHelper end end + ## + # Calculates the visibility for all attributes of the given type. + # + # @param type [Type] Type for which to get the attribute visibilities. + # @return [Hash{String => String}] A map from each attribute name to the attribute's visibility. + def type_attribute_visibility(type) + enabled_cfs = type.custom_field_ids.join("|") + visibility = work_package_form_attributes + .keys + .select { |name| name !~ /^custom_field/ || name =~ /^custom_field_(#{enabled_cfs})$/ } + .map { |name| [name, attr_visibility(name, type) || "default"] } + .to_h + end + + ## + # Updates the given type's attribute visibility map. + # + # @param type [Type] The type to be updated + # @return [Type] The updated type + def update_type_attribute_visibility!(type) + type.update! attribute_visibility: type_attribute_visibility(type) + end + ## # Checks visibility of a work package type's attribute. # diff --git a/spec/controllers/custom_fields_controller_spec.rb b/spec/controllers/custom_fields_controller_spec.rb index d399dee3296..3ddf9d5b635 100644 --- a/spec/controllers/custom_fields_controller_spec.rb +++ b/spec/controllers/custom_fields_controller_spec.rb @@ -61,6 +61,31 @@ describe CustomFieldsController, type: :controller do it { expect(custom_field.name(:en)).to eq(en_name) } end + describe "activating it in a type" do + let(:project) { FactoryGirl.create :project } + let(:type) { FactoryGirl.create :type } + let(:custom_field) { FactoryGirl.create :wp_custom_field } + + let(:params) do + { + "custom_field" => { + "type_ids" => [type.id] + } + } + end + + before do + expect(type.attribute_visibility.keys).not_to include "custom_field_#{custom_field.id}" + + put :update, params: params + end + + it "should update the type's attribute visibility map" do + expect(type.reload.attribute_visibility["custom_field_#{custom_field.id}"]) + .to eq "default" + end + end + describe 'WITH one empty name params' do let(:en_name) { 'Issue Field' } let(:de_name) { '' } diff --git a/spec/controllers/projects_controller_spec.rb b/spec/controllers/projects_controller_spec.rb index cc14638a1a0..32896f483d5 100644 --- a/spec/controllers/projects_controller_spec.rb +++ b/spec/controllers/projects_controller_spec.rb @@ -226,14 +226,34 @@ describe ProjectsController, type: :controller do let(:project) { FactoryGirl.create(:project) } let(:custom_field_1) { FactoryGirl.create(:work_package_custom_field) } let(:custom_field_2) { FactoryGirl.create(:work_package_custom_field) } - let(:request) do - put :custom_fields, - params: { - id: project.id, - project: { - work_package_custom_field_ids: [custom_field_1.id, custom_field_2.id] - } - } + + let(:params) do + { + id: project.id, + project: { + work_package_custom_field_ids: [custom_field_1.id, custom_field_2.id] + } + } + end + + let(:request) { put :custom_fields, params: params } + + describe 'attribute visibility' do + let(:type) { FactoryGirl.create :type } + let(:custom_field_1) do + FactoryGirl.create :work_package_custom_field, types: [type] + end + + before do + expect(type.attribute_visibility.keys).not_to include "custom_field_#{custom_field_1.id}" + + request + end + + it 'should be updated when a custom field was activated for the project' do + expect(type.reload.attribute_visibility["custom_field_#{custom_field_1.id}"]) + .to eq("default") + end end context 'with valid project' do