Exclude Page from ReservedWords checks (#19853)
This commit is contained in:
parent
058fd17b58
commit
22569b37c8
3 changed files with 9 additions and 6 deletions
|
|
@ -25,8 +25,6 @@ class CrossModelSlug
|
|||
|
||||
value = value.downcase
|
||||
|
||||
# Reserved check may be redundant, but allows this to be used outside of Validator
|
||||
return true if ReservedWords.all.include?(value)
|
||||
return true if value.include?("sitemap-") # https://github.com/forem/forem/pull/6704
|
||||
|
||||
MODELS.detect do |class_name, attribute|
|
||||
|
|
|
|||
|
|
@ -14,21 +14,21 @@ class CrossModelSlugValidator < ActiveModel::EachValidator
|
|||
private
|
||||
|
||||
def not_on_reserved_list?(record, attribute, value)
|
||||
return unless ReservedWords.all.include?(value)
|
||||
return false if record.instance_of?(::Page) || ReservedWords.all.exclude?(value)
|
||||
|
||||
record.errors.add(attribute, I18n.t("validators.cross_model_slug_validator.is_reserved"))
|
||||
end
|
||||
|
||||
def correct_format?(record, attribute, value)
|
||||
return if value.match?(FORMAT_REGEX)
|
||||
return false if value.match?(FORMAT_REGEX)
|
||||
|
||||
record.errors.add(attribute, I18n.t("validators.cross_model_slug_validator.is_invalid"))
|
||||
end
|
||||
|
||||
def unique_across_models?(record, attribute, value)
|
||||
# attribute_changed? is likely redundant, but is much cheaper than the cross-model exists check
|
||||
return unless record.public_send("#{attribute}_changed?")
|
||||
return unless already_exists?(value)
|
||||
return false unless record.public_send("#{attribute}_changed?")
|
||||
return false unless already_exists?(value)
|
||||
|
||||
record.errors.add(attribute, I18n.t("validators.cross_model_slug_validator.is_taken"))
|
||||
end
|
||||
|
|
|
|||
|
|
@ -66,6 +66,11 @@ RSpec.describe Page do
|
|||
expect(page).not_to be_valid
|
||||
expect(page.errors[:slug].to_s.include?("taken")).to be true
|
||||
end
|
||||
|
||||
it "circumnavigates ReservedWords check" do
|
||||
page = build(:page, slug: "code-of-conduct")
|
||||
expect(page).to be_valid
|
||||
end
|
||||
end
|
||||
|
||||
context "when callbacks are triggered before save" do
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue