Class: Katello::Validators::ProductUniqueAttributeValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/lib/katello/validators/product_unique_attribute_validator.rb

Instance Method Summary collapse

Instance Method Details

#unique_attribute?(record, attribute, value) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
# File 'app/lib/katello/validators/product_unique_attribute_validator.rb', line 12

def unique_attribute?(record, attribute, value)
  unique = true

  if record.provider && !record.provider.redhat_provider? && record.send("#{attribute}_changed?")
    if Product.in_org(record.provider.organization).where(attribute => value).exists?
      unique = false
    end
  end

  unique
end

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
# File 'app/lib/katello/validators/product_unique_attribute_validator.rb', line 4

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.")
  end
end