Skip to content

Commit

Permalink
Extract PDUs from Servers views (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
B-Rass authored Feb 5, 2025
1 parent eb1481c commit 6e7108a
Show file tree
Hide file tree
Showing 29 changed files with 1,312 additions and 75 deletions.
106 changes: 106 additions & 0 deletions app/controllers/power_distribution_units_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# frozen_string_literal: true

class PowerDistributionUnitsController < ApplicationController
before_action :set_pdu, only: %i[show edit update destroy destroy_connections]

def index
@pdus = Server.only_pdus.includes(:frame, :room, :islet, bay: :frames, modele: :category)
.references(:room, :islet, :bay, modele: :category)
.order(:name)
@filter = ProcessorFilter.new(@pdus, params)

@pdus = @filter.results
@search_params = search_params

respond_to do |format|
format.json
format.html { @pagy, @pdus = pagy(@pdus) }
end
end

def show; end

def new
@pdu = Server.new
end

def edit; end

def create
@pdu = Server.new(pdu_params)

respond_to do |format|
if @pdu.save
format.html { redirect_to power_distribution_unit_path(@pdu), notice: t(".flashes.created") }
format.json { render :show, status: :created, location: @pdu }
else
format.html { render :new }
format.json { render json: @pdu.errors, status: :unprocessable_entity }
end
end
end

def update
respond_to do |format|
if @pdu.update(pdu_params)
format.html { redirect_to power_distribution_unit_path(@pdu), notice: t(".flashes.updated") }
format.json { render :show, status: :ok, location: @pdu }
else
format.html { render :edit }
format.json { render json: @pdu.errors, status: :unprocessable_entity }
end
end
end

def destroy
respond_to do |format|
if @pdu.destroy
format.html { redirect_to power_distribution_units_path(search_params), notice: t(".flashes.destroyed") }
format.json { head :no_content }
else
format.html { redirect_to power_distribution_units_path(search_params), alert: t(".flashes.not_destroyed") }
format.json { head :bad_request }
end
end
end

def duplicate
@original_pdu = Server.friendly.find(params[:id].to_s.downcase)
@pdu = @original_pdu.deep_dup
end

def destroy_connections
if @pdu.destroy_connections!
flash[:notice] = t(".flashes.connections_destroyed")
else
flash[:alert] = t(".flashes.connections_not_destroyed")
end

redirect_to power_distribution_unit_path(@pdu)
end

private

def set_pdu
@pdu = Server.friendly_find_by_numero_or_name(params[:id])
end

def pdu_params
params.expect(
power_distribution_unit: [
:photo, :comment, :position, :frame_id, :gestion_id, :fc_futur, :rj45_cm, :name, :modele_id, :numero, :critique,
:domaine_id, :fc_total, :fc_utilise, :rj45_total, :rj45_utilise, :rj45_futur, :ipmi_utilise, :ipmi_futur,
:ipmi_dedie,
:frame, # TODO: Check if it should be removed or if it's used somewhere
{ cards_attributes: [%i[composant_id card_type_id twin_card_id orientation name first_position _destroy id]] },
{ documents_attributes: [%i[document id _destroy]] },
]
)
end

def search_params
params.permit(:sort, :sort_by, :page, :per_page, :q,
network_types: [], bay_ids: [], islet_ids: [], room_ids: [], frame_ids: [], cluster_ids: [],
gestion_ids: [], domaine_ids: [], modele_ids: [], server_state_ids: [], stack_ids: [])
end
end
68 changes: 34 additions & 34 deletions app/controllers/servers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def index
logger.warn("DEPRECATION WARNING: Search with 'name' is now deprecated. Use 'q' instead.")
end

@servers = Server.includes(:frame, :room, :islet, bay: :frames, modele: :category)
@servers = Server.no_pdus.includes(:frame, :room, :islet, bay: :frames, modele: :category)
.references(:room, :islet, :bay, modele: :category)
.order(:name)
@filter = ProcessorFilter.new(@servers, params)
Expand All @@ -27,27 +27,6 @@ def index
end
end

def grid
@servers = ServersGrid.new(params[:servers_grid])
end

def sort
room = Room.find_by_name(params[:room]) unless params[:room].include?('non ')
frame = room.frames.where('islets.name = ? AND frames.name = ?', params[:islet], params[:frame]).first
positions = params[:positions].split(',')

params[:server].each_with_index do |id, index|
if positions[index].present?
server = Server.find_by_id(id)
new_params = { position: positions[index] }
new_params[:frame_id] = frame.id if frame.present?

server.update(new_params)
end
end if params[:server].present?
head :ok # render empty body, status only
end

def show; end

def new
Expand Down Expand Up @@ -82,6 +61,39 @@ def update
end
end

def destroy
respond_to do |format|
if @server.destroy
format.html { redirect_to servers_path(search_params), notice: t(".flashes.destroyed") }
format.json { head :no_content }
else
format.html { redirect_to servers_path(search_params), alert: t(".flashes.not_destroyed") }
format.json { head :bad_request }
end
end
end

def grid
@servers = ServersGrid.new(params[:servers_grid])
end

def sort
room = Room.find_by_name(params[:room]) unless params[:room].include?('non ')
frame = room.frames.where('islets.name = ? AND frames.name = ?', params[:islet], params[:frame]).first
positions = params[:positions].split(',')

params[:server].each_with_index do |id, index|
if positions[index].present?
server = Server.find_by_id(id)
new_params = { position: positions[index] }
new_params[:frame_id] = frame.id if frame.present?

server.update(new_params)
end
end if params[:server].present?
head :ok # render empty body, status only
end

def import_csv; end

def import
Expand All @@ -96,18 +108,6 @@ def import
end
end

def destroy
respond_to do |format|
if @server.destroy
format.html { redirect_to servers_path(search_params), notice: t(".flashes.destroyed") }
format.json { head :no_content }
else
format.html { redirect_to servers_path(search_params), alert: t(".flashes.not_destroyed") }
format.json { head :bad_request }
end
end
end

def duplicate
@original_server = Server.friendly.find(params[:id].to_s.downcase)
@server = @original_server.deep_dup
Expand Down
2 changes: 2 additions & 0 deletions app/models/modele.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Modele < ApplicationRecord
scope :sorted, -> { order(:name) }
scope :with_servers, -> { joins(:servers).uniq }
scope :glpi_synchronizable, -> { where(category: Category.glpi_synchronizable) }
scope :no_pdus, -> { joins(:category).where("categories.name<>'Pdu'") }
scope :only_pdus, -> { joins(:category).where("categories.name='Pdu'").order(:name) }

def self.all_sorted
Modele.includes(:manufacturer).all.sort { |f1, f2| f1.name_with_brand.capitalize <=> f2.name_with_brand.capitalize }
Expand Down
18 changes: 9 additions & 9 deletions app/models/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ class Server < ApplicationRecord
validate :validate_network_types_values

accepts_nested_attributes_for :cards,
:allow_destroy => true,
:reject_if => :all_blank
allow_destroy: true,
reject_if: :all_blank
accepts_nested_attributes_for :disks,
:allow_destroy => true,
:reject_if => :all_blank
allow_destroy: true,
reject_if: :all_blank
accepts_nested_attributes_for :memory_components,
:allow_destroy => true,
:reject_if => :all_blank
allow_destroy: true,
reject_if: :all_blank
accepts_nested_attributes_for :documents,
:allow_destroy => true,
:reject_if => :all_blank
allow_destroy: true,
reject_if: :all_blank

normalizes :network_types, with: ->(values) { values.compact_blank }

before_create :set_default_network_types

scope :sorted, -> { order(:position => :desc) }
scope :sorted, -> { order(position: :desc) }
scope :sorted_by_name, -> { order('LOWER(name) ASC') }

scope :glpi_synchronizable, -> { joins(modele: :category).merge(Modele.glpi_synchronizable) }
Expand Down
2 changes: 1 addition & 1 deletion app/views/cables/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
<%= cable.decorated.server_connected_with_link(from_connection, from: true) %>
<span class="d-inline-flex align-items-center justify-content-center col-4 col-xl-3">
<%= cable.decorated.draw_port(from_connection) %>
<hr class="cable border #{color} border-t px-3 opacity-100">
<hr class="cable border <%= cable.color %> border-t px-3 opacity-100">
<%= cable.decorated.draw_port(to_connection) %>
</span>
<%= cable.decorated.server_connected_with_link(to_connection) %>
Expand Down
8 changes: 7 additions & 1 deletion app/views/layouts/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</li>
</ul>
</li>
<!-- Equipement -->
<!-- Equipments -->
<li class="mydcim-sidebar-links-group py-2">
<strong class="mydcim-sidebar-links-heading d-flex align-items-center fw-semibold">
<i class="bi bi-hdd-rack me-2 text-primary"></i>
Expand All @@ -58,6 +58,12 @@
class: class_names("mydcim-sidebar-links-link d-inline-block rounded",
active: controller.controller_name == "servers") %>
</li>
<li>
<%= link_to t("power_distribution_units.index.title"),
power_distribution_units_path,
class: class_names("mydcim-sidebar-links-link d-inline-block rounded",
active: controller.controller_name == "power_distribution_units") %>
</li>
<li>
<%= link_to AirConditioner.model_name.human.pluralize,
air_conditioners_path,
Expand Down
Loading

0 comments on commit 6e7108a

Please sign in to comment.