FIX: set external flag before validation (#20599)

Previously, `before_save` callback was used but `before_validation` has to be used to set external flag.
This commit is contained in:
Krzysztof Kotlarek 2023-03-09 10:44:54 +11:00 committed by GitHub
parent 008f71b961
commit 22bccef8f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View File

@ -7,7 +7,7 @@ class SidebarUrl < ActiveRecord::Base
validate :path_validator
before_save :remove_internal_hostname, :set_external
before_validation :remove_internal_hostname, :set_external
def path_validator
if external?

View File

@ -8,5 +8,39 @@ RSpec.describe SidebarUrl do
expect(SidebarUrl.new(icon: "link", name: "categories", value: "/invalid_path").valid?).to eq(
false,
)
expect(
SidebarUrl.new(
icon: "link",
name: "external",
value: "https://www.test.com/discourse-test",
).valid?,
).to eq(true)
end
it "sets external flag" do
expect(
SidebarUrl.create!(icon: "link", name: "categories", value: "/categories").external,
).to be false
expect(
SidebarUrl.create!(
icon: "link",
name: "categories",
value: "http://#{Discourse.current_hostname}/categories",
).external,
).to be false
expect(
SidebarUrl.create!(
icon: "link",
name: "categories",
value: "https://#{Discourse.current_hostname}/categories",
).external,
).to be false
expect(
SidebarUrl.create!(
icon: "link",
name: "categories",
value: "https://www.test.com/discourse-test",
).external,
).to be true
end
end