Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow update to project attributes without specifying components #104

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/api/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def load_projects

def project_params
params.fetch(:project, {}).permit(
:identifier,
:name,
:project_type,
{
Expand Down
12 changes: 9 additions & 3 deletions lib/concepts/project/operations/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,18 +49,22 @@ 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?
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?

Expand Down
20 changes: 20 additions & 0 deletions spec/concepts/project/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions spec/request/projects/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] } } }

Expand Down