From 177ae14c0d68a2182d29ecfb745ec5c9c12b1de5 Mon Sep 17 00:00:00 2001 From: loiswells97 Date: Mon, 9 Jan 2023 17:10:43 +0000 Subject: [PATCH 1/3] Create draft PR for #103 From 1908c7f34dafb69b3fe972d52c1d4b1207c8d992 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Mon, 9 Jan 2023 17:11:37 +0000 Subject: [PATCH 2/3] Allowing update without specifying components --- app/controllers/api/projects_controller.rb | 1 + lib/concepts/project/operations/update.rb | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/projects_controller.rb b/app/controllers/api/projects_controller.rb index 9105a2f5c..4ab555d32 100644 --- a/app/controllers/api/projects_controller.rb +++ b/app/controllers/api/projects_controller.rb @@ -56,6 +56,7 @@ def load_projects def project_params params.fetch(:project, {}).permit( + :identifier, :name, :project_type, { diff --git a/lib/concepts/project/operations/update.rb b/lib/concepts/project/operations/update.rb index 0cbd90f6d..2051d0ffe 100644 --- a/lib/concepts/project/operations/update.rb +++ b/lib/concepts/project/operations/update.rb @@ -26,6 +26,8 @@ def setup_response(project) end def setup_deletions(response, update_hash) + return if update_hash[:components].nil? + existing_component_ids = response[:project].components.pluck(:id) updated_component_ids = update_hash[:components].pluck(:id) response[:component_ids_to_delete] = existing_component_ids - updated_component_ids @@ -47,7 +49,7 @@ def update_project_attributes(response, update_hash) end def update_component_attributes(response, update_hash) - return if response.failure? + return if response.failure? || update_hash[:components].nil? update_hash[:components].each do |component_params| if component_params[:id].present? From b020b1b1a566b03b44c4b911f9c72e07967f4877 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Wed, 11 Jan 2023 11:43:25 +0000 Subject: [PATCH 3/3] Testing update changes and fixing rubocop --- lib/concepts/project/operations/update.rb | 8 ++++++-- spec/concepts/project/update_spec.rb | 20 ++++++++++++++++++++ spec/request/projects/update_spec.rb | 19 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/concepts/project/operations/update.rb b/lib/concepts/project/operations/update.rb index 2051d0ffe..5b0276f51 100644 --- a/lib/concepts/project/operations/update.rb +++ b/lib/concepts/project/operations/update.rb @@ -53,14 +53,18 @@ def update_component_attributes(response, update_hash) update_hash[:components].each do |component_params| if component_params[:id].present? - component = response[:project].components.select { |c| c.id == component_params[:id] }.first - component.assign_attributes(component_params) + overwrite_component_attributes(response, component_params) else response[:project].components.build(component_params) end end end + def overwrite_component_attributes(response, component_params) + component = response[:project].components.select { |c| c.id == component_params[:id] }.first + component.assign_attributes(component_params) + end + def persist_changes(response) return if response.failure? diff --git a/spec/concepts/project/update_spec.rb b/spec/concepts/project/update_spec.rb index c3b4ac762..f28347f75 100644 --- a/spec/concepts/project/update_spec.rb +++ b/spec/concepts/project/update_spec.rb @@ -57,6 +57,26 @@ expect(created_component).not_to be_nil end end + + context 'when a component has been removed' do + let(:component_hash) { [default_component_hash] } + + it 'deletes a component' do + expect { update }.to change(Component, :count).by(-1) + end + end + + context 'when no components have been specified' do + let(:component_hash) { nil } + + it 'keeps the same number of components' do + expect { update }.not_to change(Component, :count) + end + + it 'updates project properties' do + expect { update }.to change { project.reload.name }.to('updated project name') + end + end end def component_properties_hash(component) diff --git a/spec/request/projects/update_spec.rb b/spec/request/projects/update_spec.rb index 009ab6d0a..06e8e9359 100644 --- a/spec/request/projects/update_spec.rb +++ b/spec/request/projects/update_spec.rb @@ -49,6 +49,25 @@ expect(Project::Update).to have_received(:call) end + context 'when no components specified' do + let(:params) { { project: { name: 'updated project name' } } } + + it 'returns success response' do + put "/api/projects/#{project.identifier}", params: params + expect(response).to have_http_status(:ok) + end + + it 'returns json with updated project properties' do + put "/api/projects/#{project.identifier}", params: params + expect(response.body).to include('updated project name') + end + + it 'returns json with previous project components' do + put "/api/projects/#{project.identifier}", params: params + expect(response.body).to include(project.components.first.attributes[:content].to_s) + end + end + context 'when update is invalid' do let(:params) { { project: { components: [] } } }