Make home link an admin-customisable navigation link (#16268)

* move home link to a customisable navlink

* add trailing slash for path

* update specs

* replace positions, set home link to -1

* add update script for adding home navigation link

* update data update script
This commit is contained in:
Suzanne Aitchison 2022-02-01 09:27:09 +00:00 committed by GitHub
parent 62d3191a91
commit f1864847e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 13 deletions

View file

@ -3,12 +3,6 @@
<nav class="mb-4 <% unless user_signed_in? %>mt-4<% end %>" id="main-navigation" aria-label="<%= community_name %>">
<ul class="default-navigation-links sidebar-navigation-links spec-sidebar-navigation-links">
<li>
<a href="<%= root_path %>" class="c-link c-link--block c-link--icon-left">
<%= crayons_icon_tag("twemoji/house", class: "c-link__icon", native: true, aria_hidden: true) %>
<%= t("views.main.home") %>
</a>
</li>
<% default_nav_links.each do |link| %>
<%= render "layouts/sidebar_nav_link", link: link %>
<% end %>

View file

@ -0,0 +1,20 @@
module DataUpdateScripts
class CreateHomeNavigationLink
def run
NavigationLink.create_or_update_by_identity(
name: "Home",
url: URL.url("/"),
icon: File.read(Rails.root.join("app/assets/images/twemoji/house.svg")),
display_only_when_signed_in: false,
position: 1,
section: :default,
)
# Increment all other default section links to account for new home link, and update to be 1-based
NavigationLink.where(section: :default).find_each do |link|
new_position = link.position + 2
link.update_columns(position: new_position)
end
end
end
end

View file

@ -8,6 +8,7 @@ namespace :navigation_links do
image_path("twemoji", name)
end
home_icon = twemoji_path("house.svg")
reading_icon = twemoji_path("drawer.svg")
contact_icon = twemoji_path("contact.svg")
thumb_up_icon = twemoji_path("thumb-up.svg")
@ -40,6 +41,7 @@ namespace :navigation_links do
# [@jeremyf] I went ahead and atomized these tasks so we _could_ call them individually if
# desired. I did not add descriptions so those tasks will not show up in the task
# list.
Rake::Task["navigation_links:find_or_create:home"].invoke
Rake::Task["navigation_links:find_or_create:readinglist"].invoke
Rake::Task["navigation_links:find_or_create:contact"].invoke
Rake::Task["navigation_links:find_or_create:code_of_conduct"].invoke
@ -52,13 +54,24 @@ namespace :navigation_links do
end
namespace :find_or_create do
task home: :environment do
NavigationLink.create_or_update_by_identity(
name: "Home",
url: URL.url("/"),
icon: home_icon,
display_only_when_signed_in: false,
position: 1,
section: :default,
)
end
task readinglist: :environment do
NavigationLink.create_or_update_by_identity(
url: URL.url("readinglist"),
name: "Reading List",
icon: reading_icon,
display_only_when_signed_in: true,
position: 1,
position: 2,
section: :default,
)
end
@ -69,7 +82,7 @@ namespace :navigation_links do
url: URL.url("contact"),
icon: contact_icon,
display_only_when_signed_in: false,
position: 2,
position: 3,
section: :default,
)
end
@ -114,6 +127,14 @@ namespace :navigation_links do
domain = Rails.application&.initialized? ? Settings::General.app_domain : ApplicationConfig["APP_DOMAIN"]
base_url = "#{protocol}#{domain}".freeze
NavigationLink.create_or_update_by_identity(
name: "Home",
icon: home_icon,
display_only_when_signed_in: false,
position: 1,
section: :default,
)
NavigationLink.create_or_update_by_identity(
url: "#{base_url}/readinglist",
name: "Reading List",

View file

@ -0,0 +1,31 @@
require "rails_helper"
require Rails.root.join(
"lib/data_update_scripts/20220126102900_create_home_navigation_link.rb",
)
describe DataUpdateScripts::CreateHomeNavigationLink do
it "creates a home navigation link when it doesn't already exist" do
expect do
described_class.new.run
end.to change(NavigationLink, :count).by(1)
end
it "skips home navigation link creation if already exists" do
create(:navigation_link, url: "/", name: "Home")
expect do
described_class.new.run
end.not_to change(NavigationLink, :count)
end
it "updates the position of other default navigation links" do
link = create(:navigation_link, url: "/example", name: "Example", section: :default, position: 0)
described_class.new.run
expect(link.reload.position).to eq(2)
end
it "doesn't update the position of other navigation links" do
link = create(:navigation_link, url: "/example", name: "Example", section: :other, position: 0)
described_class.new.run
expect(link.reload.position).to eq(0)
end
end

View file

@ -97,8 +97,8 @@ RSpec.describe "User visits a homepage", type: :system do
visit "/"
within("#main-navigation", match: :first) do
expect(page).to have_selector(".default-navigation-links li:nth-child(2)", text: "Shop")
expect(page).to have_selector(".default-navigation-links li:nth-child(3)", text: "Mock")
expect(page).to have_selector(".default-navigation-links li:nth-child(1)", text: "Shop")
expect(page).to have_selector(".default-navigation-links li:nth-child(2)", text: "Mock")
end
end
end
@ -220,7 +220,7 @@ RSpec.describe "User visits a homepage", type: :system do
it "shows link when display_only_when_signed_in is true" do
within("#main-navigation", match: :first) do
expect(page).to have_selector(".default-navigation-links li:nth-child(3)", text: "Beauty")
expect(page).to have_selector(".default-navigation-links li:nth-child(2)", text: "Beauty")
end
end
end

View file

@ -8,7 +8,7 @@ RSpec.describe "Navigation Links tasks", type: :task do
describe "#create" do
it "creates navigation links for new forem if nonexistent" do
expect { Rake::Task["navigation_links:create"].invoke }.to change(NavigationLink, :count).by(5)
expect { Rake::Task["navigation_links:create"].invoke }.to change(NavigationLink, :count).by(6)
end
context "when navigation links exist" do
@ -22,7 +22,7 @@ RSpec.describe "Navigation Links tasks", type: :task do
end
it "does not create nav links if they already exist" do
expect { Rake::Task["navigation_links:create"].invoke }.to change(NavigationLink, :count).from(1).to(5)
expect { Rake::Task["navigation_links:create"].invoke }.to change(NavigationLink, :count).from(1).to(6)
end
end
end