follow robustness principle and allow custom option values

pull/5707/head
Markus Kahl 7 years ago
parent fc894d9eb3
commit e3e7878b89
  1. 35
      app/controllers/api/v2/planning_elements_controller.rb
  2. 47
      spec/controllers/api/v2/planning_elements_controller_spec.rb

@ -57,8 +57,9 @@ module Api
def create def create
@planning_element = @project.work_packages.build @planning_element = @project.work_packages.build
@planning_element.update_attributes(permitted_params.planning_element(project: @project).except :note) attributes = permitted_params.planning_element(project: @project).except :note
@planning_element.update_attributes(lookup_custom_options(attributes))
@planning_element.attach_files(params[:attachments]) @planning_element.attach_files(params[:attachments])
# The planning_element inherits from workpackage, which requires an author. # The planning_element inherits from workpackage, which requires an author.
@ -94,8 +95,8 @@ module Api
def update def update
@planning_element = WorkPackage.find(params[:id]) @planning_element = WorkPackage.find(params[:id])
@planning_element.attributes = permitted_params.planning_element(project: @project).except :note attributes = permitted_params.planning_element(project: @project).except :note
@planning_element.attributes = lookup_custom_options attributes
@planning_element.add_journal(User.current, permitted_params.planning_element(project: @project)[:note]) @planning_element.add_journal(User.current, permitted_params.planning_element(project: @project)[:note])
successfully_updated = @planning_element.save successfully_updated = @planning_element.save
@ -122,6 +123,34 @@ module Api
protected protected
def lookup_custom_options(attributes)
return attributes unless attributes.include?("custom_fields")
custom_fields = attributes["custom_fields"].map do |custom_field|
if CustomField.where(id: custom_field["id"], field_format: "list").exists?
value = custom_field["value"]
custom_option_id = begin
Integer(value)
rescue
lookup_custom_option(custom_field)
end
custom_field.merge value: custom_option_id || value
else
custom_field
end
end
attributes.merge custom_fields: custom_fields
end
def lookup_custom_option(custom_field_attributes)
custom_field_id = custom_field_attributes["id"]
value = custom_field_attributes["value"]
CustomOption.where(custom_field_id: custom_field_id, value: value).pluck(:id)
end
def load_multiple_projects(ids, identifiers) def load_multiple_projects(ids, identifiers)
@projects = [] @projects = []
@projects |= Project.where(id: ids) unless ids.empty? @projects |= Project.where(id: ids) unless ids.empty?

@ -905,6 +905,53 @@ describe Api::V2::PlanningElementsController, type: :controller do
end end
end end
describe 'with list custom fields' do
let(:type) { ::Type.find_by(name: 'None') || FactoryGirl.create(:type_standard) }
let(:custom_field) do
FactoryGirl.create :list_wp_custom_field,
projects: [project],
types: [type],
possible_values: ['foo', 'bar', 'baz']
end
let(:planning_element) do
FactoryGirl.create :work_package,
type: type,
project: project,
custom_values: [
CustomValue.new(
custom_field: custom_field,
value: custom_field.possible_values.first.id
)
]
end
it 'updates the custom field value' do
put 'update',
params: {
project_id: project.identifier,
id: planning_element.id,
planning_element: {
custom_fields: [
{ id: custom_field.id, value: 'bar' }
]
}
},
format: :xml
expect(response.response_code).to eq(204)
wp = WorkPackage.find planning_element.id
custom_value = wp.custom_values.find do |value|
value.custom_field.name == custom_field.name
end
expect(custom_value).not_to be_nil
expect(custom_value.value).not_to eq(custom_field.possible_values.first.id.to_s)
expect(custom_value.value).to eq(custom_field.possible_values.second.id.to_s)
end
end
## ##
# It should be possible to update a planning element's status by transmitting the # It should be possible to update a planning element's status by transmitting the
# field 'status_id'. The test tries to change a planning element's status from # field 'status_id'. The test tries to change a planning element's status from

Loading…
Cancel
Save