Skip to content
Closed
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
14 changes: 8 additions & 6 deletions app/models/move.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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|
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion app/models/moved_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
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

scope :not_executed, -> { where(executed_at: nil) }

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
Expand Down
2 changes: 2 additions & 0 deletions app/models/moves_project_step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirm what we do about existing data (don't forget about executed)

Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions spec/models/move_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion spec/models/moved_connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/moved_connections.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# 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
cablename: NouveauNomDuCableUn
color: Blue

two:
step_id: 1
port_from_id: 1
port_to_id: 2
vlans: VlanXZZ
Expand Down