Skip to content

368436 évolution des PortTypes - #783

Open
moxyom wants to merge 17 commits into
nanego:masterfrom
pantographe:368436-port-type-evolution
Open

368436 évolution des PortTypes#783
moxyom wants to merge 17 commits into
nanego:masterfrom
pantographe:368436-port-type-evolution

Conversation

@moxyom

@moxyom moxyom commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@moxyom
moxyom force-pushed the 368436-port-type-evolution branch from f832eb1 to ae33114 Compare July 29, 2026 13:57
@moxyom
moxyom marked this pull request as ready for review July 29, 2026 14:59
Comment thread app/models/port_type.rb Outdated
Comment thread app/models/port_type.rb Outdated
Comment on lines +13 to +14
scope :attachable_to_server, -> { where("? = ANY(attachable_to)", :server) }
scope :attachable_to_pdu, -> { where("? = ANY(attachable_to)", :pdu) }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
scope :attachable_to_server, -> { where("? = ANY(attachable_to)", :server) }
scope :attachable_to_pdu, -> { where("? = ANY(attachable_to)", :pdu) }
scope :usable_by, -> (usable_by) { where("? = ANY(attachable_to)", usable_by) }

Comment thread app/views/port_types/index.html.erb
Comment thread app/views/port_types/show.html.erb
Comment thread app/models/port_type.rb Outdated
has_many :card_types, dependent: :restrict_with_error
has_many :sockets, class_name: "PowerDistributionUnit::Socket", dependent: :restrict_with_error

validates :attachable_to, array_inclusion: { in: %w[pdu server] }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Enum definition is missing, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have not found a "multiple value enum" built in for rails. It is juste an array of string isn't it ?

def change
rename_column :port_types, :power, :is_power

create_enum :port_attachable_type, %w[pdu server]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
create_enum :port_attachable_type, %w[pdu server]
create_enum :port_types_usable_by, %w[pdu server]

up_only do
PortTypeMigration.reset_column_information
PortTypeMigration.find_each do |port_type|
port_type.update!(is_power: port_type.name == "ALIM", attachable_to: ["server"])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
port_type.update!(is_power: port_type.name == "ALIM", attachable_to: ["server"])
port_type.update!(is_power: port_type.name == "ALIM", usable_by: ["server"])

rename_column :port_types, :power, :is_power

create_enum :port_attachable_type, %w[pdu server]
add_column :port_types, :attachable_to, :enum, enum_type: :port_attachable_type, array: true

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
add_column :port_types, :attachable_to, :enum, enum_type: :port_attachable_type, array: true
add_column :port_types, :usable_by, :enum, enum_type: :port_types_usable_by, array: true

@moxyom
moxyom force-pushed the 368436-port-type-evolution branch from 0c7935a to dd80681 Compare August 4, 2026 09:10
@nicolas-brousse
nicolas-brousse force-pushed the 368436-port-type-evolution branch from 4bc769a to 1582efd Compare August 5, 2026 14:38
Comment thread app/decorators/port_type_decorator.rb Outdated

def self.usable_by_options_for_select
PortType::USABLE_BY_VALUES.map do |value|
[I18n.t(".activerecord.attributes.port_type.usable_by/#{value}"), value]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Does human_attribute_name usable here?

Comment thread app/models/concerns/enum/array.rb Outdated
Comment thread config/locales/activerecord.en.yml Outdated
blank: can't be blank, or "All domains" must be checked

messages:
array_inclusion: "includes unauthorized values : %{wrong_values}"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
array_inclusion: "includes unauthorized values : %{wrong_values}"
array_inclusion: "includes unauthorized values: %{wrong_values}"

Comment thread app/views/port_types/index.html.erb Outdated
<% end %>

<% table.with_column(PortType.human_attribute_name(:usable_by)) do |port_type| %>
<%= port_type.usable_by.to_sentence %>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Values not translated. Maybe use decorator for this.

Comment thread app/views/port_types/show.html.erb Outdated

<dl class="show-page_dl d-grid row-gap-2 mb-0">
<dt class="pb-2"><%= PortType.human_attribute_name(:usable_by) %></dt>
<dd class="mb-0 pb-2 ps-3"><%= @port_type.usable_by.to_sentence %></dd>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Values not translated. Maybe use decorator for this

Comment thread app/views/port_types/index.html.erb
Comment thread config/locales/activerecord.en.yml Outdated
Comment on lines +22 to +23
usable_by/server: Server
usable_by/pdu: Pdu

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Use same format as we usually do for enums

Comment thread config/locales/activerecord.fr.yml Outdated
Comment on lines +257 to +258
usable_by/server: Serveur
usable_by/pdu: Pdu

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Use same format as we usually do for enums

one: Network hub cluster
other: Network hub clusters
color: Color
port_type:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Add missing attributes already present in fr

def port_type_options_for_select
PortTypeDecorator.options_for_select(PortType.where(power: true))
def port_types_options_for_select
PortTypeDecorator.options_for_select(PortType.usable_by(:pdu))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

should only show usable_by(:pdu) & power: true for this form.

Comment thread app/models/concerns/enum/array.rb
Comment on lines +28 to +39
# get sql type to cast properly
# It returns the array's element type
element_type = columns_hash[name.to_s].sql_type

# encode array so it can be understood by PG
encoder = PG::TextEncoder::Array.new
encoded = encoder.encode(db_values)

where(
"#{name} @> ?::#{element_type}[]",
encoded,
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Does where("#{name} @> ARRAY[?]::varchar[]", db_values) works?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It does not if I'm right.
Like the DB fields is / (can be) a PG enum, @> will not accept another type as the exact field type.
A PG enum value isn't comparable to a varchar, the array elements can also be integers ...

The values are sent as text, and PG cast them. I haven't found any encoder for that column API

@moxyom
moxyom force-pushed the 368436-port-type-evolution branch from ea77ce9 to ae4ac74 Compare August 31, 2026 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants