-
Notifications
You must be signed in to change notification settings - Fork 2
368436 évolution des PortTypes #783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
0f008d6
a94eb53
a3e26eb
3f8d62f
22f29c8
f852179
e047364
b7ab956
2b7bbbb
d0534fc
88d2091
81a0006
1f98ad4
ef7a8ca
01dab3c
2343b6e
ae4ac74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Enum | ||
| module Array | ||
| extend ActiveSupport::Concern | ||
|
|
||
| MISSING_VALUE_MESSAGE = "%<value>s is not a valid value for %<attr>s" # rubocop:disable Style/FormatStringToken | ||
| private_constant :MISSING_VALUE_MESSAGE | ||
|
|
||
| class_methods do | ||
| def array_enum(name = nil, mapping = nil, validate: false) | ||
| name = name.to_s | ||
| mapping_hash = ActiveSupport::HashWithIndifferentAccess.new(mapping) | ||
|
|
||
| defined_enums[name] = mapping_hash | ||
|
|
||
| define_singleton_method(name.pluralize) do | ||
| mapping_hash | ||
| end | ||
|
|
||
| define_singleton_method("with_#{name}") do |*values| | ||
| db_values = values.map do |value| | ||
| raise_missing_value(name, value) unless mapping_hash.key?(value) | ||
|
|
||
| mapping_hash[value] | ||
| end | ||
|
|
||
| # 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, | ||
| ) | ||
|
Comment on lines
+28
to
+39
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does not if I'm right. The values are sent as text, and PG cast them. I haven't found any encoder for that column API
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discuss on chat, let's comment |
||
| end | ||
|
|
||
| define_method(name) do | ||
| Array(self[name]).map { |value| mapping_hash.key(value) || value } | ||
| end | ||
|
|
||
| define_method(:"#{name}=") do |values| | ||
| self[name] = Array(values).compact_blank.map do |value| | ||
| raise_missing_value(name, value) unless validate || mapping_hash.key?(value) | ||
|
|
||
| mapping_hash[value] || value | ||
| end.uniq | ||
| end | ||
|
|
||
| validates name, array_inclusion: { in: mapping_hash.keys } if validate | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def raise_missing_value(name, value) | ||
| raise ArgumentError, format(MISSING_VALUE_MESSAGE, name:, value:) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,18 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class PortType < ApplicationRecord | ||
| include Enum::Array | ||
|
|
||
| has_changelog | ||
|
|
||
| has_many :card_types, dependent: :restrict_with_error | ||
| has_many :sockets, class_name: "PowerDistributionUnit::Socket", dependent: :restrict_with_error | ||
|
|
||
| USABLE_BY_VALUES = %w[pdu server].freeze | ||
| array_enum :usable_by, USABLE_BY_VALUES.index_with(&:to_s), validate: true | ||
|
|
||
| scope :sorted, -> { order(name: :asc) } | ||
| scope :power_ones, -> { where(is_power: true) } | ||
|
|
||
| delegate :to_s, to: :name | ||
|
|
||
| def is_power_input? | ||
| name == "ALIM" | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class ArrayInclusionValidator < ActiveModel::EachValidator | ||
| def validate_each(record, attribute, values) | ||
| not_allowed_values = values - options[:in] | ||
|
|
||
| return if not_allowed_values.empty? | ||
|
|
||
| wrong_values = not_allowed_values.join(", ") | ||
| record.errors.add(attribute, :contains_unpermitted_values, wrong_values:) | ||
| end | ||
| end |
|
nicolas-brousse marked this conversation as resolved.
|
|
nicolas-brousse marked this conversation as resolved.
|
Uh oh!
There was an error while loading. Please reload this page.