diff --git a/app/models/move.rb b/app/models/move.rb index 0733ba381..6f1649e87 100644 --- a/app/models/move.rb +++ b/app/models/move.rb @@ -10,6 +10,8 @@ class Move < ApplicationRecord has_one :moves_project, through: :step + has_many :moved_connections, through: :step + validates :moveable_id, uniqueness: { scope: %i[step moveable_type] } validates :position, presence: true @@ -23,17 +25,17 @@ def clear_connections_and_save save end - def moved_connections + def moveable_moved_connections return [] unless moveable - MovedConnection.per_servers([moveable]) + moved_connections.per_servers([moveable]) end def clear_connections - return unless remove_existing_connections_on_execution - # Delete current moved connections - moved_connections.delete_all + moveable_moved_connections.delete_all + + return unless remove_connections # Add moved connection for each port moveable.ports.each do |p| @@ -59,7 +61,7 @@ def execute!(apply_connections: true) equipment.position = position if equipment.save! - moved_connections.map(&:execute!) if apply_connections + moveable_moved_connections.map(&:execute!) if apply_connections # Update prev_frame and prev_position for incoming moves Move.not_executed diff --git a/app/models/moved_connection.rb b/app/models/moved_connection.rb index c94df2fea..421e65a08 100644 --- a/app/models/moved_connection.rb +++ b/app/models/moved_connection.rb @@ -3,6 +3,7 @@ class MovedConnection < ApplicationRecord has_changelog + belongs_to :step, class_name: "MovesProjectStep" belongs_to :port_from, class_name: "Port" belongs_to :port_to, class_name: "Port", optional: true @@ -10,7 +11,8 @@ class MovedConnection < ApplicationRecord def self.per_servers(servers) servers_ports_ids = servers.map(&:ports).flatten.map(&:id) - MovedConnection.where("port_from_id IN (?) OR port_to_id IN (?)", servers_ports_ids, servers_ports_ids) + + where("port_from_id IN (?) OR port_to_id IN (?)", servers_ports_ids, servers_ports_ids) end def ports diff --git a/app/models/moves_project_step.rb b/app/models/moves_project_step.rb index 9e7ab56ef..c430bb920 100644 --- a/app/models/moves_project_step.rb +++ b/app/models/moves_project_step.rb @@ -4,7 +4,9 @@ class MovesProjectStep < ApplicationRecord has_changelog belongs_to :moves_project + has_many :moves, dependent: :restrict_with_error + has_many :moved_connections, dependent: :restrict_with_error acts_as_list scope: :moves_project diff --git a/db/migrate/20251210161815_add_step_reference_to_moved_connections.rb b/db/migrate/20251210161815_add_step_reference_to_moved_connections.rb new file mode 100644 index 000000000..8e5e6f274 --- /dev/null +++ b/db/migrate/20251210161815_add_step_reference_to_moved_connections.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddStepReferenceToMovedConnections < ActiveRecord::Migration[8.0] + def change + add_reference :moved_connections, :step, null: true, foreign_key: { to_table: :moves_project_steps } + end +end diff --git a/db/schema.rb b/db/schema.rb index f0827e609..5d57226fa 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -361,8 +361,10 @@ t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false t.datetime "executed_at", precision: nil + t.bigint "step_id" t.index ["port_from_id"], name: "index_moved_connections_on_port_from_id" t.index ["port_to_id"], name: "index_moved_connections_on_port_to_id" + t.index ["step_id"], name: "index_moved_connections_on_step_id" end create_table "moves", id: :serial, force: :cascade do |t| @@ -587,6 +589,7 @@ add_foreign_key "modeles", "architectures" add_foreign_key "modeles", "categories" add_foreign_key "modeles", "manufacturers" + add_foreign_key "moved_connections", "moves_project_steps", column: "step_id" add_foreign_key "moved_connections", "ports", column: "port_from_id" add_foreign_key "moved_connections", "ports", column: "port_to_id" add_foreign_key "moves", "frames" diff --git a/spec/models/move_spec.rb b/spec/models/move_spec.rb index 29a6a3080..d36d97c94 100644 --- a/spec/models/move_spec.rb +++ b/spec/models/move_spec.rb @@ -18,6 +18,8 @@ it { is_expected.to belong_to(:prev_frame).class_name("Frame") } it { is_expected.to have_one(:moves_project).through(:step) } + + it { is_expected.to have_many(:moved_connections).through(:step) } end describe "validations" do diff --git a/spec/models/moved_connection_spec.rb b/spec/models/moved_connection_spec.rb index f7dd1bbc5..d3d3d42c2 100644 --- a/spec/models/moved_connection_spec.rb +++ b/spec/models/moved_connection_spec.rb @@ -5,9 +5,12 @@ RSpec.describe MovedConnection do # it_behaves_like "changelogable", new_attributes: { } - subject(:moved_connection) { described_class.new(color: "bleu", cablename: "cable") } + subject(:moved_connection) { described_class.new(color: "bleu", cablename: "cable", step:) } + + let(:step) { move_project_steps(:one) } describe "associations" do + it { is_expected.to belong_to(:step) } it { is_expected.to belong_to(:port_from) } it { is_expected.to belong_to(:port_to).optional(true) } end diff --git a/test/fixtures/moved_connections.yml b/test/fixtures/moved_connections.yml index ba5c84289..f68c09112 100644 --- a/test/fixtures/moved_connections.yml +++ b/test/fixtures/moved_connections.yml @@ -1,6 +1,7 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: + step_id: 1 port_from_id: 1 port_to_id: 2 vlans: vlan01 @@ -8,6 +9,7 @@ one: color: Blue two: + step_id: 1 port_from_id: 1 port_to_id: 2 vlans: VlanXZZ