-
Notifications
You must be signed in to change notification settings - Fork 40
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
129 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,33 @@ | ||
export default function migrate(settings) { | ||
const oldSmallLinks = settings.get("small_links"); | ||
|
||
if (oldSmallLinks) { | ||
const newSmallLinks = oldSmallLinks.split("|").map((link) => { | ||
const [linkText, linkUrl, linkTarget] = link | ||
.split(",") | ||
.map((fragment) => fragment.trim()); | ||
|
||
const newSmallLink = { | ||
text: linkText, | ||
url: linkUrl, | ||
}; | ||
|
||
switch (linkTarget) { | ||
case "blank": | ||
newSmallLink.target = "_blank"; | ||
break; | ||
case "self": | ||
newSmallLink.target = "_self"; | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return 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
44 changes: 44 additions & 0 deletions
44
test/unit/migrations/settings/0003-migrate-small-links-setting-test.js
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,44 @@ | ||
import { module, test } from "qunit"; | ||
import migrate from "../../../../migrations/settings/0003-migrate-small-links-setting"; | ||
|
||
module( | ||
"Unit | Migrations | Settings | 0003-migrate-small-links-setting", | ||
function () { | ||
test("migrate", async function (assert) { | ||
const settings = new Map( | ||
Object.entries({ | ||
small_links: | ||
"Link 1, #, blank|Link 2, #, self|Link 3, #, invalid_target", | ||
}) | ||
); | ||
|
||
const result = migrate(settings); | ||
|
||
const expectedResult = new Map( | ||
Object.entries({ | ||
small_links: [ | ||
{ | ||
text: "Link 1", | ||
url: "#", | ||
target: "_blank", | ||
}, | ||
{ | ||
text: "Link 2", | ||
url: "#", | ||
target: "_self", | ||
}, | ||
{ | ||
text: "Link 3", | ||
url: "#", | ||
}, | ||
], | ||
}) | ||
); | ||
|
||
assert.deepEqual( | ||
Object.fromEntries(result.entries()), | ||
Object.fromEntries(expectedResult.entries()) | ||
); | ||
}); | ||
} | ||
); |