Skip to content

Commit

Permalink
Update Cables.index to be a merge of Connections and Cables
Browse files Browse the repository at this point in the history
  • Loading branch information
B-Rass committed Jan 9, 2025
1 parent 6886375 commit c8d1ede
Show file tree
Hide file tree
Showing 24 changed files with 361 additions and 452 deletions.
62 changes: 55 additions & 7 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ table.card_layout {
}

.portRJ, .portFC, .portSCSI, .portALIM {
cursor: pointer;
position: relative;
font-size: 10px;
display: inline-block;
Expand All @@ -172,7 +171,8 @@ table.card_layout {
.portRJ,
.portFC,
.portSCSI,
.portALIM{
.portALIM,
.cable {
// Colors are !important so we can print them
&.R {
color: #FFFFFF !important;
Expand Down Expand Up @@ -443,6 +443,59 @@ dd {
font-weight: 700;
}

/* Cable index */
.cable-index-servers {
.cable,
.badge.empty {
&.R {
border-color: #ee3b3b !important;
}
&.G {
border-color: #AAAAAA !important;
}
&.V {
border-color: green !important;
}
&.T {
border-color: #3B9C9C !important;
}
&.Vi {
border-color: #663399 !important;
}
&.M {
border-color: #8b4c39 !important;
}
&.B {
border-color: #4876ff !important;
}
&.N {
border-color: #000000 !important;
}
&.O {
border-color: #FF9000 !important;
}
&.W {
border-color: #FFFFFF !important;
}
&.J {
border-color: #FFDD00 !important;
}
&.P {
border-color: #ff9ee5 !important;
}
}

.badge {
&.no_client {
border: solid 2px #FF9000 !important;
}

&.unreferenced_client {
border: solid 2px red !important;
}
}
}

/* Page about */
.about {
.github-link {
Expand All @@ -456,11 +509,6 @@ dd {
}
}

.align-middle {
vertical-align: middle !important;
}


/* MyDCIM CSS */
html,
body {
Expand Down
14 changes: 10 additions & 4 deletions app/controllers/cables_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ class CablesController < ApplicationController
before_action :set_cable, only: [:destroy]

def index
@cables = sorted(Cable.includes(:connections, connections: [:port]).order(created_at: :desc))
@pagy, @cables = pagy(@cables)
@cables = Cable.includes(:connections,
connections: [:port, :server, :card],
cards: [:card_type],
card_types: [:port_type])
.order(created_at: :desc)
@filter = ProcessorFilter.new(@cables, params)

@pagy, @cables = pagy(@filter.results)
end

def destroy
port_id = params[:redirect_to_port_id]
params[:redirect_to_port_id]

@cable.ports.each do |port|
if @from_server.nil?
Expand All @@ -23,7 +29,7 @@ def destroy

respond_to do |format|
format.html do
redirect_to connections_edit_path(from_port_id: port_id), notice: t(".flashes.destroyed")
redirect_to cables_path, notice: t(".flashes.destroyed")
end

format.js { render 'connections/update' }
Expand Down
8 changes: 0 additions & 8 deletions app/controllers/connections_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
# frozen_string_literal: true

class ConnectionsController < ApplicationController
def index
@connections = Connection.includes(:port, :card, :server, :card_type, :port_type, cable: :connections)
.order(created_at: :desc)
@filter = ProcessorFilter.new(@connections, params)

@pagy, @connections = pagy(@filter.results)
end

def edit
if params[:from_port_id].present? && params[:from_port_id].to_i > 0
@from_port = Port.find_by_id(params[:from_port_id])
Expand Down
78 changes: 78 additions & 0 deletions app/decorators/cable_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# frozen_string_literal: true

class CableDecorator < ApplicationDecorator
include ActionView::Helpers::AssetTagHelper
include Rails.application.routes.url_helpers
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TextHelper
include ActionView::Context

class << self
def special_case_options_for_select
[true, false].map do |s|
[I18n.t("boolean.#{s}"), s]
end
end

def colors_options_for_select
Cable::COLORS.map do |k, v|
[I18n.t(".activerecord.attributes.cable/color.#{v}"), k]
end
end
end

def connected_servers
@connected_servers ||= begin
from_connection = connections.first
to_connection = connections.second

tag.span class: "cable-index-servers d-flex column-gap-2 align-items-center justify-content-center" do
concat(server_with_link(from_connection))
concat(tag.span(class: "d-inline-flex align-items-center") do
concat(draw_connection(from_connection))
concat(tag.hr(class: "cable border #{color} border-t px-3 opacity-100"))
concat(draw_connection(to_connection))
end)
concat(server_with_link(to_connection))
end
end
end

private

def server_with_link(connection)
if (server = connection&.server)
link_to server.to_s,
server_path(server),
class: "text-body-emphasis",
title: connection.card.decorated.full_name,
data: { controller: "tooltip", 'bs-placement': "bottom", turbo_frame: :_top },
aria: { hidden: "true" }
else
tag.span "n/c", class: "fst-italic fw-lighter"
end
end

def draw_connection(connection)
if (twin_card_id = connection&.card&.twin_card_id)
twin_card = Card.find(twin_card_id)
twin_connections = twin_card.ports.map(&:connection)
twin_card_used_ports = []
twin_connections.each { |c| twin_card_used_ports << c.port.position if c&.port }

port_data = connection.port
end

if name.present?
tag.span name,
class: class_names("badge me-0 #{color} text-body-emphasis",
'fst-italic fw-lighter': name.blank?,
no_client: twin_card_used_ports && port_data && port_data.cable_name && twin_card_used_ports.exclude?(port_data.position)),
data: { controller: "tooltip", 'bs-placement': "bottom" },
title: connection&.port&.vlans,
aria: { hidden: "true" }
else
tag.span "n/c", class: "badge empty me-0 border text-body-emphasis fst-italic fw-lighter"
end
end
end
1 change: 0 additions & 1 deletion app/helpers/servers_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def ports_by_card_with_presentation(card:, selected_port: nil, moved_connections
if (cell_index + 1) % number_of_columns_in_cell(card.orientation, ports_per_cell, card_type.max_aligned_ports) == 0 # Every XX ports do
html += '</div><div class="d-flex">'
end

end

html += "</div></td>"
Expand Down
19 changes: 19 additions & 0 deletions app/models/cable.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
# frozen_string_literal: true

class Cable < ApplicationRecord
COLORS = {
N: :black,
M: :brown,
R: :red,
O: :orange,
J: :yellow,
V: :green,
T: :turquoise,
B: :blue,
Vi: :purple,
P: :pink,
G: :grey,
W: :white
}.freeze

has_changelog

has_many :connections, dependent: :destroy
has_many :ports, through: :connections
has_many :servers, through: :connections
has_many :cards, through: :connections
has_many :card_types, through: :cards
has_many :port_types, through: :card_types

after_update :touch_ports

Expand Down
6 changes: 1 addition & 5 deletions app/models/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ def respond_to_missing?(symbol, include_all)

def _permitted_attributes_names
attribute_names.map do |attribute_name|
if attribute_name.end_with?("_ids")
[attribute_name, { attribute_name => [] }]
else
attribute_name
end
[attribute_name, { attribute_name => [] }]
end
end

Expand Down
46 changes: 46 additions & 0 deletions app/processors/cables_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

class CablesProcessor < ApplicationProcessor
include Sortable
SORTABLE_FIELDS = %w[special_case].freeze

map :cable_name do |cable_name:|
raw.where(name: cable_name)
end

map :special_case do |special_case:|
raw.where(special_case:)
end

map :colors, filter_with: :non_empty_array do |colors:|
raw.where(color: colors)
end

map :comments do |comments:|
raw.where(Cable.arel_table[:comments].matches("%#{comments}%"))
end

map :server_ids, filter_with: :non_empty_array do |server_ids:|
raw.joins(:servers).where(servers: { id: server_ids })
end

map :port_type_ids, filter_with: :non_empty_array do |port_type_ids:|
raw.joins(:port_types).where(port_types: { id: port_type_ids })
end

map :card_query do |card_query:|
name_condition = Card.arel_table[:name].matches("%#{card_query}%")
card_type_condition = CardType.arel_table[:name].matches("%#{card_query}%")
slot_condition = Composant.arel_table[:name].matches("%#{card_query}%")
combined_conditions = name_condition.or(card_type_condition).or(slot_condition)
raw.joins(:cards, :card_types, cards: :composant).where(combined_conditions)
end

sortable fields: SORTABLE_FIELDS do
having "comments" do |sort: "asc"|
valid_sort_value!(sort)

raw.reorder("comments #{sort.upcase} nulls last")
end
end
end
32 changes: 0 additions & 32 deletions app/processors/connections_processor.rb

This file was deleted.

Loading

0 comments on commit c8d1ede

Please sign in to comment.