update type attribute visibility on cf changes

cf = custom field
This commit is contained in:
Markus Kahl
2016-11-04 15:42:46 +01:00
parent c9940b2227
commit 86ab55b323
5 changed files with 87 additions and 11 deletions
+5 -1
View File
@@ -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
+6 -2
View File
@@ -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
+23
View File
@@ -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.
#
@@ -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) { '' }
+28 -8
View File
@@ -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