-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV: Migrate small_links to new objects setting type
This commit migrates the `small_links` theme setting to `type: objects`. Since discourse/discourse@a440e15, we have started to support objects typed theme setting so we are switching this theme component to use it instead as it provides a much better UX for configuring the settings required for the theme component.
- Loading branch information
Showing
6 changed files
with
164 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export default function migrate(settings, helpers) { | ||
const oldSmallLinks = settings.get("small_links"); | ||
|
||
if (oldSmallLinks) { | ||
const newSmallLinks = []; | ||
|
||
oldSmallLinks.split("|").forEach((link) => { | ||
const [linkText, linkUrl, linkTarget] = link | ||
.split(",") | ||
.map((fragment) => fragment.trim()); | ||
|
||
if (linkText) { | ||
const newSmallLink = { | ||
text: linkText, | ||
}; | ||
|
||
if (linkUrl && helpers.isValidUrl(linkUrl)) { | ||
newSmallLink.url = linkUrl; | ||
} else { | ||
newSmallLink.url = "#"; | ||
} | ||
|
||
if (linkTarget === "self") { | ||
newSmallLink.target = "_self"; | ||
} else { | ||
newSmallLink.target = "_blank"; | ||
} | ||
|
||
newSmallLinks.push(newSmallLink); | ||
} | ||
}); | ||
|
||
settings.set("small_links", newSmallLinks); | ||
} | ||
|
||
return settings; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe "0003-migrate-small-links-setting migration" do | ||
let!(:theme) { upload_theme_component } | ||
|
||
it "should set target property to `_blank` if previous target component is not valid or empty" do | ||
theme.theme_settings.create!( | ||
name: "small_links", | ||
theme: theme, | ||
data_type: ThemeSetting.types[:string], | ||
value: "some text, #|some text 2, #, invalid target", | ||
) | ||
|
||
run_theme_migration(theme, "0003-migrate-small-links-setting") | ||
|
||
expect(theme.settings[:small_links].value).to eq( | ||
[ | ||
{ "text" => "some text", "url" => "#", "target" => "_blank" }, | ||
{ "text" => "some text 2", "url" => "#", "target" => "_blank" }, | ||
], | ||
) | ||
end | ||
|
||
it "should set URL property to `#` if previous URL component is not valid or empty" do | ||
theme.theme_settings.create!( | ||
name: "small_links", | ||
theme: theme, | ||
data_type: ThemeSetting.types[:string], | ||
value: "some text, not a valid URL|some text 2,,,|some text 3, #", | ||
) | ||
|
||
run_theme_migration(theme, "0003-migrate-small-links-setting") | ||
|
||
expect(theme.settings[:small_links].value).to eq( | ||
[ | ||
{ "text" => "some text", "url" => "#", "target" => "_blank" }, | ||
{ "text" => "some text 2", "url" => "#", "target" => "_blank" }, | ||
{ "text" => "some text 3", "url" => "#", "target" => "_blank" }, | ||
], | ||
) | ||
end | ||
|
||
it "should not migrate small links setting if text component does not exist" do | ||
theme.theme_settings.create!( | ||
name: "small_links", | ||
theme: theme, | ||
data_type: ThemeSetting.types[:string], | ||
value: ",/some/url|,,,|some text,/some/url", | ||
) | ||
|
||
run_theme_migration(theme, "0003-migrate-small-links-setting") | ||
|
||
expect(theme.settings[:small_links].value).to eq( | ||
[{ "text" => "some text", "url" => "/some/url", "target" => "_blank" }], | ||
) | ||
end | ||
|
||
it "should migrate the small links setting to objects type correctly" do | ||
theme.theme_settings.create!( | ||
name: "small_links", | ||
theme: theme, | ||
data_type: ThemeSetting.types[:string], | ||
value: "some text,/some/url,blank|some text 2,/some/url2,self|", | ||
) | ||
|
||
run_theme_migration(theme, "0003-migrate-small-links-setting") | ||
|
||
expect(theme.settings[:small_links].value).to eq( | ||
[ | ||
{ "text" => "some text", "url" => "/some/url", "target" => "_blank" }, | ||
{ "text" => "some text 2", "url" => "/some/url2", "target" => "_self" }, | ||
], | ||
) | ||
end | ||
end |