Skip to content

Commit

Permalink
Fixes #38023 - Stop using deprecated methods for ActiveModel::Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ofedoren committed Nov 20, 2024
1 parent 05f3fc9 commit c496521
Show file tree
Hide file tree
Showing 35 changed files with 102 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ def validate_each(record, attribute, value)
case attribute
when :base_url
unless AlternateContentSourcePathValidator.validate_base_url(value)
record.errors[attribute] << N_("%s is not a valid path") % value
record.errors.add(attribute, N_("%s is not a valid path") % value)
end
when :subpaths
unless AlternateContentSourcePathValidator.validate_subpaths(value)
record.errors[attribute] << N_('All subpaths must have a slash at the end and none at the front')
record.errors.add(attribute, N_('All subpaths must have a slash at the end and none at the front'))
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Validators
class ContainerImageNameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if value && !ContainerImageNameValidator.validate_name(value)
record.errors[attribute] << N_("invalid container image name")
record.errors.add(attribute, N_("invalid container image name"))
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/lib/katello/validators/content_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ContentValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
value.encode("UTF-8", 'binary') unless value.blank?
rescue Encoding::UndefinedConversionError
record.errors[attribute] << (options[:message] || _("cannot be a binary file."))
record.errors.add(attribute, (options[:message] || _("cannot be a binary file.")))
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def validate(record)
env = KTEnvironment.where(:id => environment_id).first
return if view.blank? || env.blank?
if view.default? && !env.library?
record.errors[:base] << _("Lifecycle environment '%{env}' cannot be used with content view '%{view}'") %
{:view => view.name, :env => env.name}
record.errors.add(:base, _("Lifecycle environment '%{env}' cannot be used with content view '%{view}'") %
{:view => view.name, :env => env.name})
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ def validate(record)
view = ContentView.where(:id => record.content_view_id).first
environment = KTEnvironment.where(:id => environment_id).first
if view.blank? || environment.blank?
record.errors[:base] << _("Content view environments must have both a content view and an environment")
record.errors.add(:base, _("Content view environments must have both a content view and an environment"))
end

unless view&.organization == environment&.organization
record.errors[:base] << _("%{view_label} could not be promoted to %{environment_label} because the content view and the environment are not in the same organization!") % {:view_label => view&.label, :environment_label => environment&.label}
record.errors.add(:base, _("%{view_label} could not be promoted to %{environment_label} because the content view and the environment are not in the same organization!") % {:view_label => view&.label, :environment_label => environment&.label})
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def validate(record)
if environment_id
env = KTEnvironment.where(:id => environment_id).first
unless view.blank? || env.blank? || view.in_environment?(env)
record.errors[:base] << _("Content view '%{view}' is not in environment '%{env}'") %
{:view => view.name, :env => env.name}
record.errors.add(:base, _("Content view '%{view}' is not in environment '%{env}'") %
{:view => view.name, :env => env.name})
end
end
if view&.generated_for_repository?
record.errors[:base] << _("Generated content views cannot be assigned to hosts or activation keys")
record.errors.add(:base, _("Generated content views cannot be assigned to hosts or activation keys"))
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def validate(record)
if record.errata_id.blank? && record.start_date.blank? && record.end_date.blank? && record.types.blank?
invalid_parameters = _("Invalid erratum filter rule specified, Must specify at least one of the following:" \
" 'errata_id', 'start_date', 'end_date' or 'types'")
record.errors[:base] << invalid_parameters
record.errors.add(:base, invalid_parameters)
return
end

Expand All @@ -14,7 +14,7 @@ def validate(record)
!record.types.blank?)
invalid_parameters = _("Invalid erratum filter rule specified, 'errata_id' cannot be specified in the" \
" same tuple as 'start_date', 'end_date' or 'types'")
record.errors[:base] << invalid_parameters
record.errors.add(:base, invalid_parameters)
return
end

Expand All @@ -28,13 +28,13 @@ def _check_single_date_range(record)
if !record.filter.erratum_rules.empty? &&
!record.filter.erratum_rules.any? { |rule| rule.id == record.id }
invalid_parameters = _("May not add a type or date range rule to a filter that has existing rules.")
record.errors[:base] << invalid_parameters
record.errors.add(:base, invalid_parameters)
end

else
if record.filter_has_date_or_type_rule?
invalid_parameters = _("May not add an id rule to a filter that has an existing type or date range rule.")
record.errors[:base] << invalid_parameters
record.errors.add(:base, invalid_parameters)
end

end
Expand All @@ -45,28 +45,28 @@ def _check_date_range(record)
end_date_int = record.end_date.to_time.to_i unless record.end_date.blank?

if start_date_int && (!start_date_int.is_a?(Integer) || !record.start_date.is_a?(String))
record.errors[:base] << _("The erratum filter rule start date is in an invalid format or type.")
record.errors.add(:base, _("The erratum filter rule start date is in an invalid format or type."))
end
if end_date_int && (!end_date_int.is_a?(Integer) || !record.end_date.is_a?(String))
record.errors[:base] << _("The erratum filter rule end date is in an invalid format or type.")
record.errors.add(:base, _("The erratum filter rule end date is in an invalid format or type."))
end

if start_date_int && end_date_int && !(start_date_int <= end_date_int)
record.errors[:base] << _("Invalid date range. The erratum filter rule start date must come before the end date")
record.errors.add(:base, _("Invalid date range. The erratum filter rule start date must come before the end date"))
end
end

def _check_types(record)
if record.types.is_a?(Array)
invalid_types = record.types.collect(&:to_s) - ContentViewErratumFilter::ERRATA_TYPES.keys
unless invalid_types.empty?
record.errors[:base] <<
record.errors.add(:base,
_("Invalid erratum types %{invalid_types} provided. Erratum type can be any of %{valid_types}") %
{ :invalid_types => invalid_types.join(","),
:valid_types => ContentViewErratumFilter::ERRATA_TYPES.keys.join(",")}
:valid_types => ContentViewErratumFilter::ERRATA_TYPES.keys.join(",")})
end
else
record.errors[:base] << _("The erratum type must be an array. Invalid value provided")
record.errors.add(:base, _("The erratum type must be an array. Invalid value provided"))
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def validate(record)
if !record.version.blank? && (!record.min_version.blank? || !record.max_version.blank?)
invalid_parameters = _("Invalid filter rule specified, 'version' cannot be specified in the" \
" same tuple as 'min_version' or 'max_version'")
record.errors[:base] << invalid_parameters
record.errors.add(:base, invalid_parameters)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def validate(record)
if cve.content_view_id
view = ContentView.where(:id => cve.content_view_id).first
if view&.generated_for_repository?
record.errors[:base] << _("Content view '%{cv_name}' is a generated content view, which cannot be assigned to hosts or activation keys.") % { :cv_name => view.name }
record.errors.add(:base, _("Content view '%{cv_name}' is a generated content view, which cannot be assigned to hosts or activation keys.") % { :cv_name => view.name })
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/lib/katello/validators/gpg_key_content_type_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ def validate(record)
# ssl_ca_cert, ssl_client_cert, ssl_client_key of GpgKey.content type "cert"

if !record.gpg_key.blank? && record.gpg_key.content_type != "gpg_key"
record.errors[:gpg_key] << _("Wrong content type submitted.")
record.errors.add(:gpg_key, _("Wrong content type submitted."))
end

if record.instance_of?(Katello::Product)
[:ssl_ca_cert, :ssl_client_cert, :ssl_client_key].each do |cert|
if !record.send(cert).blank? && record.send(cert).content_type != "cert"
record.errors[cert] << _("Wrong content type submitted.")
record.errors.add(cert, _("Wrong content type submitted."))
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions app/lib/katello/validators/gpg_key_content_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ def validate_each(record, attribute, value)
gpg_key_line_array.pop

if gpg_key_line_array.empty?
record.errors[attribute] << _("must contain valid Public GPG Key")
record.errors(attribute, _("must contain valid Public GPG Key"))
else
unless gpg_key_line_array.drop(1).join.match(/[a-zA-Z0-9+\/=]*/)
record.errors[attribute] << _("must contain valid Public GPG Key")
record.errors(attribute, _("must contain valid Public GPG Key"))
end
end

else
record.errors[attribute] << _("must contain valid Public GPG Key")
record.errors(attribute, _("must contain valid Public GPG Key"))
end

else
record.errors[attribute] << _("must contain GPG Key")
record.errors(attribute, _("must contain GPG Key"))
end
end

def self.validate_line_length(record, attribute, value)
value.each_line do |line|
record.errors[attribute] << _("must contain valid Public GPG Key") if line.length > ContentCredential::MAX_CONTENT_LINE_LENGTH
record.errors(attribute, _("must contain valid Public GPG Key")) if line.length > ContentCredential::MAX_CONTENT_LINE_LENGTH
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions app/lib/katello/validators/katello_label_format_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ module Validators
class KatelloLabelFormatValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if value
record.errors[attribute] << N_("cannot contain characters other than ascii alpha numerals, '_', '-'. ") unless value =~ /\A[a-zA-Z0-9_\-]+\z/
record.errors.add(attribute, N_("cannot contain characters other than ascii alpha numerals, '_', '-'. ")) unless value =~ /\A[a-zA-Z0-9_\-]+\z/
NoTrailingSpaceValidator.validate_trailing_space(record, attribute, value)
KatelloLabelFormatValidator.validate_length(record, attribute, value)
else
record.errors[attribute] << N_("can't be blank")
record.errors.add(attribute, N_("can't be blank"))
end
end

def self.validate_length(record, attribute, value, max_length = 128, min_length = 1)
if value
record.errors[attribute] << N_("cannot contain more than %s characters") % max_length unless value.length <= max_length
record.errors[attribute] << N_("must contain at least %s character") % min_length unless value.length >= min_length
record.errors.add(attribute, N_("cannot contain more than %s characters") % max_length) unless value.length <= max_length
record.errors.add(attribute, N_("must contain at least %s character") % min_length) unless value.length >= min_length
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/lib/katello/validators/katello_name_format_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ def validate_each(record, attribute, value)
NoTrailingSpaceValidator.validate_trailing_space(record, attribute, value)
KatelloNameFormatValidator.validate_length(record, attribute, value)
else
record.errors[attribute] << N_("cannot be blank")
record.errors.add(attribute, N_("cannot be blank"))
end
end

def self.validate_length(record, attribute, value, min_length = 1)
if value && !(value.length >= min_length)
record.errors[attribute] << _("must contain at least %s character") % min_length
record.errors.add(attribute, _("must contain at least %s character") % min_length)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/lib/katello/validators/katello_url_format_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def validate_each(record, attribute, value)
end

attribute_name = options[:field_name] || attribute
record.errors[attribute_name] << N_("is invalid") unless kurl_valid?(value)
record.errors.add(attribute_name, N_("is invalid")) unless kurl_valid?(value)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/lib/katello/validators/library_presence_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Katello
module Validators
class LibraryPresenceValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << N_("must contain '%s'") % "Library" if value.select { |e| e.library }.empty?
record.errors.add(attribute, N_("must contain '%s'") % "Library") if value.select { |e| e.library }.empty?
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/lib/katello/validators/no_trailing_space_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def validate_each(record, attribute, value)

def self.validate_trailing_space(record, attribute, value)
if value && !(value.strip == value)
record.errors[attribute] << _("must not contain leading or trailing white spaces.")
record.errors.add(attribute, _("must not contain leading or trailing white spaces."))
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Validators
class NonLibraryEnvironmentValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return unless value
record.errors[attribute] << N_("Cannot register a system to the '%s' environment") % "Library" if !record.environment.nil? && record.environment.library?
record.errors.add(attribute, N_("Cannot register a system to the '%s' environment") % "Library") if !record.environment.nil? && record.environment.library?
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/lib/katello/validators/not_in_library_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Katello
module Validators
class NotInLibraryValidator < ActiveModel::Validator
def validate(record)
record.errors[:environment] << _("The '%s' environment cannot contain a changeset!") % "Library" if record.environment.library?
record.errors.add(:environment, _("The '%s' environment cannot contain a changeset!") % "Library") if record.environment.library?
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/lib/katello/validators/path_descendents_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def validate(record)
#environment is not duplicated in its path
# We do not want circular dependencies
return if record.prior.nil?
record.errors[:prior] << _(" environment cannot be set to an environment already on its path") if duplicate? record.prior
record.errors.add(:prior, _(" environment cannot be set to an environment already on its path")) if duplicate? record.prior
end

def duplicate?(record)
Expand Down
2 changes: 1 addition & 1 deletion app/lib/katello/validators/prior_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def validate(record)
# prior to have only one child (unless its the Library)
ancestor = record.prior
if ancestor && !ancestor.library? && (ancestor.successors.count == 1 && !ancestor.successors.include?(record))
record.errors[:prior] << _("prior environment can only have one child")
record.errors.add(:prior, _("prior environment can only have one child"))
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def validate_each(record, attribute, value)
unique = self.unique_attribute?(record, attribute, value)

unless unique
record.errors[attribute] << _("has already been taken for a product in this organization.")
record.errors.add(attribute, _("has already been taken for a product in this organization."))
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/lib/katello/validators/repo_disablement_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module Validators
class RepoDisablementValidator < ActiveModel::Validator
def validate(record)
if record.redhat? && record.enabled_changed? && !record.enabled? && record.promoted?
record.errors[:base] << N_("Repository cannot be disabled since it has already been promoted.")
record.errors.add(:base, N_("Repository cannot be disabled since it has already been promoted."))
elsif !record.redhat? && !record.enabled?
record.errors[:base] << N_("Custom repositories cannot be disabled.")
record.errors.add(:base, N_("Custom repositories cannot be disabled."))
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def validate_each(record, attribute, value)
exists = RootRepository.where(:product_id => record.product_id, attribute => value).where("id != ?", record.id || -1).exists?

if record.send("#{attribute}_changed?") && record.custom? && exists
record.errors[attribute] << _("has already been taken for this product.")
record.errors.add(attribute, _("has already been taken for this product."))
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Katello
module Validators
class SelfReferenceEnvironmentValidator < ActiveModel::Validator
def validate(record)
record.errors[:base] << _("Environment cannot be in its own promotion path") if record.priors.select(:id).include? record.id
record.errors.add(:base, _("Environment cannot be in its own promotion path")) if record.priors.select(:id).include? record.id
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/lib/katello/validators/unique_field_in_org.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def validate_each(record, attribute, value)
if value
others = record.class.where(attribute => value).joins(:environment).where("#{Katello::KTEnvironment.table_name}.organization_id" => record.environment.organization_id)
others = others.where("#{record.class.table_name}.id != ?", record.id) if record.persisted?
record.errors[attribute] << N_("already taken") if others.any?
record.errors.add(attribute, N_("already taken")) if others.any?
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions app/models/katello/activation_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ class ActivationKey < Katello::Model
validates_each :max_hosts do |record, attr, value|
if record.unlimited_hosts
unless value.nil?
record.errors[attr] << _("cannot be set because unlimited hosts is set")
record.errors.add(attr, _("cannot be set because unlimited hosts is set"))
end
else
if value.nil?
record.errors[attr] << _("cannot be nil")
record.errors.add(attr, _("cannot be nil"))
elsif value <= 0
record.errors[attr] << _("cannot be less than one")
record.errors.add(attr, _("cannot be less than one"))
elsif value < record.subscription_facets.length
# we don't let users to set usage limit lower than current in-use
record.errors[attr] << _("cannot be lower than current usage count (%s)" % record.subscription_facets.length)
record.errors.add(attr, _("cannot be lower than current usage count (%s)" % record.subscription_facets.length))
end
end
end
Expand Down
Loading

0 comments on commit c496521

Please sign in to comment.