Skip to content

Commit

Permalink
DEV: Migrate small_links to new objects setting type
Browse files Browse the repository at this point in the history
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
tgxworld committed May 2, 2024
1 parent 3e06c50 commit 0af77a6
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 30 deletions.
7 changes: 3 additions & 4 deletions javascripts/discourse/components/custom-footer.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@

<div class="third-box">
<div class="footer-links">
{{#each this.smallLinks as |link|}}
{{#each (theme-setting "small_links") as |link|}}
<a
class="small-link"
data-easyfooter-small-link={{link.dataName}}
title={{link.title}}
data-easyfooter-small-link={{dasherize link.text}}
target={{link.target}}
href={{link.href}}
href={{link.url}}
>
{{link.text}}
</a>
Expand Down
18 changes: 0 additions & 18 deletions javascripts/discourse/components/custom-footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@ export default class extends Component {
mainHeading = settings.heading;
blurb = settings.blurb;

smallLinks = settings.small_links
.split("|")
.filter(Boolean)
.map((link) => {
const fragments = link.split(",").map((fragment) => fragment.trim());
const text = fragments[0];
const dataName = dasherize(text);
const href = fragments[1];
const target = fragments[2] === "blank" ? "_blank" : "";

return {
text,
dataName,
href,
target,
};
});

socialLinks = settings.social_links
.split("|")
.filter(Boolean)
Expand Down
18 changes: 13 additions & 5 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@ en:
label: Referrer Policy
description: The referrerpolicy attribute of the link

link_sections:
description: "Add link sections. The ideal number of sections is six. One item per line in this order:<br> Text, title<br><b>Text:</b> what appears on in the footer<br><b>Title:</b> the text that appears when the item is hovered."
links:
description: "Add links to link sections. One item per line in this order:<br>Parent, text, URL, target, title, referrer policy<br>It is a good idea to keep the number of links under each section similar<br><b>Parent:</b> the name of the parent section which this link shows under. Use the `text` value from the list above<br><b>Text:</b> the text that shows for this link<br><b>URL:</b> the path this item links to. You can use relative paths as well.<br><b>Target:</b> Choose whether this item will open in a new tab or in the same tab. Use blank to open the link in a new tab, or use self to open it in the same tab.<br><b>Title:</b> the text that shows when the link is hovered.<br/><b>Referrer Policy:</b> the referrer policy to use in the link."
small_links:
description: "You can add small links at the bottom of the footer like Terms of Service and Privacy. One item per line in this order:<br>Text, URL, target<br><b>Text:</b> The text that shows for the small link<br><b>URL:</b> The path of the link<br><b>Target:</b> Use blank to open the link in a new tab and use self to open it in the same tab"
description: "Small links at the bottom of the footer like Terms of Service and Privacy"
schema:
properties:
text:
label: Text
description: Text to be displayed for the link
url:
label: URL
description: The URL the link points to
target:
label: Target
description: The target attribute of the link

social_links:
description: "Enter the social links you'd like to add to the footer in this format:<br> provider, title, URL, target<br><b>Provider:</b> is the name of the provider like Facebook or Twitter<br><b>Title:</b> The text that shows when the link is hovered<br><b>URL:</b> The path you'd like the link to have<br><b>Target:</b> Use blank to open the link in a new tab and use self to open it in the same tab<br><b>Icon:</b> use a FontAwesome5 icon name (brand icons need a 'fab-' prefix)."
show_footer_on_login_required_page:
Expand Down
37 changes: 37 additions & 0 deletions migrations/settings/0003-migrate-small-links-setting.js
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;
}
39 changes: 36 additions & 3 deletions settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,42 @@ sections:
- unsafe-url

small_links:
type: list
list_type: simple
default: "Privacy, #, self|Terms of service, #, self| About, #, self"
type: objects
default:
- text: Privacy
url: "#"
target: _blank
- text: Terms of service
url: "#"
target: _blank
- text: About
url: "#"
target: _blank
schema:
name: small link
identifier: text
properties:
text:
type: string
required: true
validations:
min: 1
max: 1000
url:
type: string
required: true
validations:
min: 1
max: 2048
url: true
target:
type: enum
default: _blank
choices:
- _blank
- _self
- _parent
- _top

social_links:
type: list
Expand Down
75 changes: 75 additions & 0 deletions spec/migrations/0003_migrate_small_links_setting_spec.rb
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

0 comments on commit 0af77a6

Please sign in to comment.