From 511f5378888c3cb8559740d82e35fecf7cc72bef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Filmont?= Date: Wed, 2 Sep 2026 16:18:30 +0200 Subject: [PATCH 1/5] fix 404 and reset data when no to_port --- app/controllers/connections_controller.rb | 29 ++++++++++++++++------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/app/controllers/connections_controller.rb b/app/controllers/connections_controller.rb index 39611517c..cad09c2bb 100644 --- a/app/controllers/connections_controller.rb +++ b/app/controllers/connections_controller.rb @@ -62,20 +62,31 @@ def update authorize! from_port = Port.find(params[:connection][:from_port_id]) - to_port = Port.find(params[:connection][:to_port_id]) - from_port.connect_to_port(to_port, - params[:connection][:cablename], - params[:connection][:color], - params[:connection][:vlans], - params[:connection][:special_case], - params[:connection][:comments]) + if params[:connection][:to_port_id].presence + to_port = Port.find(params[:connection][:to_port_id]) + + from_port.connect_to_port(to_port, + params[:connection][:cablename], + params[:connection][:color], + params[:connection][:vlans], + params[:connection][:special_case], + params[:connection][:comments]) + + @to_server = to_port.server + else + # destroy connections if to_port_id set to null + from_port.connections.each do |connection| + connection.cable&.destroy + connection.destroy + end + + from_port.update(vlans: "", color: "", cablename: "") + end @from_server = from_port.server @from_pdu = from_port.circuit&.record unless @from_server - @to_server = to_port.server - respond_to do |format| format.html do redirect_to connections_edit_path(from_port_id: from_port.id), notice: t(".flashes.updated") From b9e45969cb0daa9d67e2dac8fcdbeef35d3717dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Filmont?= Date: Wed, 2 Sep 2026 17:35:38 +0200 Subject: [PATCH 2/5] add tests --- .../connections_controller_request_spec.rb | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/spec/requests/connections_controller_request_spec.rb b/spec/requests/connections_controller_request_spec.rb index 5e3dc801f..9271c4866 100644 --- a/spec/requests/connections_controller_request_spec.rb +++ b/spec/requests/connections_controller_request_spec.rb @@ -107,17 +107,13 @@ end end - # context "without attributes" do - # let(:params) { { connection: {} } } + context "without to_port_id" do + let(:params) { { connection: { from_port_id: "1" } } } + let(:records) { [connections(:one), cables(:one), connections(:two)] } - # it { expect { response }.to raise_error(ActionController::ParameterMissing) } - # end - - # context "without parameters" do - # let(:params) { {} } - - # it { expect { response }.to raise_error(ActionController::ParameterMissing) } - # end + it { expect(response).to have_http_status(:redirect) } + it { expect { response }.to change { records.count { |r| r.class.exists?(r.id) } }.from(3).to(0) } + end context "with invalid parameters" do let(:params) { { connection: invalid_attributes } } From da15017e9266df6cf30777a663490d39a88c165a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Filmont?= Date: Thu, 3 Sep 2026 17:24:16 +0200 Subject: [PATCH 3/5] apply review changes --- app/controllers/connections_controller.rb | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/app/controllers/connections_controller.rb b/app/controllers/connections_controller.rb index cad09c2bb..2b685d9da 100644 --- a/app/controllers/connections_controller.rb +++ b/app/controllers/connections_controller.rb @@ -62,26 +62,18 @@ def update authorize! from_port = Port.find(params[:connection][:from_port_id]) + to_port = params[:connection][:to_port_id] - if params[:connection][:to_port_id].presence - to_port = Port.find(params[:connection][:to_port_id]) - + if to_port.present? + @to_server = to_port.server from_port.connect_to_port(to_port, params[:connection][:cablename], params[:connection][:color], params[:connection][:vlans], params[:connection][:special_case], params[:connection][:comments]) - - @to_server = to_port.server else - # destroy connections if to_port_id set to null - from_port.connections.each do |connection| - connection.cable&.destroy - connection.destroy - end - - from_port.update(vlans: "", color: "", cablename: "") + from_port.update(vlans: params[:connection][:vlans]) end @from_server = from_port.server From f8da7c5054da30e4eaeee17c94db06b57d44e36d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Filmont?= Date: Thu, 3 Sep 2026 17:32:40 +0200 Subject: [PATCH 4/5] fix --- app/controllers/connections_controller.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/connections_controller.rb b/app/controllers/connections_controller.rb index 2b685d9da..d0a50210e 100644 --- a/app/controllers/connections_controller.rb +++ b/app/controllers/connections_controller.rb @@ -62,10 +62,12 @@ def update authorize! from_port = Port.find(params[:connection][:from_port_id]) - to_port = params[:connection][:to_port_id] + to_port_id = params[:connection][:to_port_id] - if to_port.present? + if to_port_id.present? + to_port = Port.find(to_port_id) @to_server = to_port.server + from_port.connect_to_port(to_port, params[:connection][:cablename], params[:connection][:color], From 87f50f66d2104cc4f9568d9a56f9d3744b9ba7e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Filmont?= Date: Thu, 3 Sep 2026 17:39:18 +0200 Subject: [PATCH 5/5] correct tests --- spec/requests/connections_controller_request_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/requests/connections_controller_request_spec.rb b/spec/requests/connections_controller_request_spec.rb index 9271c4866..c47bafa51 100644 --- a/spec/requests/connections_controller_request_spec.rb +++ b/spec/requests/connections_controller_request_spec.rb @@ -112,7 +112,6 @@ let(:records) { [connections(:one), cables(:one), connections(:two)] } it { expect(response).to have_http_status(:redirect) } - it { expect { response }.to change { records.count { |r| r.class.exists?(r.id) } }.from(3).to(0) } end context "with invalid parameters" do