Skip to content

A platform for community discussion. Free, open, simple.

FIX: set external flag before validation (#20599)

Krzysztof Kotlarek22bccef8f401
diff --git a/app/models/sidebar_url.rb b/app/models/sidebar_url.rb
index c173fcc8580..670ac656227 100644
--- a/app/models/sidebar_url.rb
+++ b/app/models/sidebar_url.rb
@@ -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?
diff --git a/spec/models/sidebar_url_spec.rb b/spec/models/sidebar_url_spec.rb
index 5fb0ebc3d23..37d5218b0cd 100644
--- a/spec/models/sidebar_url_spec.rb
+++ b/spec/models/sidebar_url_spec.rb
@@ -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