Static Sections for Site Navigation - Pt1 (#14213)

* schema file undelete description

* update with main

* update with origin

* Add "section" enum to navlink model

* create migration for section column

* create migration and data-update script

* update related rake tasks

* add scopes for default_links and other_links

* update E2E seeds

* ran migration; correct enum reference

* fix requests and model specs

* write DUS spec; fix factory and DUS

* yarn install

* address PR review comments

* fix migration; add null: false

* Remove yarn.lock from commit

* remove yarn.lock changes from commit

* sync yarn.lock with main

* newline
This commit is contained in:
Arit Amana 2021-07-14 11:01:56 -04:00 committed by GitHub
parent 92ee9ce8cf
commit 3adfe7ace3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 74 additions and 3 deletions

View file

@ -2,7 +2,7 @@ module Admin
class NavigationLinksController < Admin::ApplicationController
after_action :bust_content_change_caches, only: %i[create update destroy]
ALLOWED_PARAMS = %i[
name url icon display_only_when_signed_in position
name url icon display_only_when_signed_in position section
].freeze
layout "admin"

View file

@ -4,6 +4,8 @@ class NavigationLink < ApplicationRecord
before_validation :allow_relative_url, if: :url?
before_save :strip_local_hostname, if: :url?
enum section: { default: 0, other: 1 }, _suffix: true
validates :name, :url, :icon, presence: true
validates :url, url: { schemes: %w[https http] }, uniqueness: { scope: :name }
validates :icon, format: SVG_REGEXP

View file

@ -0,0 +1,5 @@
class AddSectionToNavigationLinks < ActiveRecord::Migration[6.1]
def change
add_column :navigation_links, :section, :integer, default: 0, null: false
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_06_29_174206) do
ActiveRecord::Schema.define(version: 2021_07_12_155135) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
@ -700,6 +700,7 @@ ActiveRecord::Schema.define(version: 2021_06_29_174206) do
t.string "icon", null: false
t.string "name", null: false
t.integer "position"
t.integer "section", default: 0, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "url", null: false
t.index ["url", "name"], name: "index_navigation_links_on_url_and_name", unique: true

View file

@ -0,0 +1,13 @@
module DataUpdateScripts
class BackfillSectionColumnForNavigationLinks
OTHER_LINKS = [
"/code-of-conduct",
"/privacy",
"/terms",
].freeze
def run
NavigationLink.where(url: OTHER_LINKS).update(section: "other")
end
end
end

View file

@ -25,78 +25,91 @@ namespace :navigation_links do
icon: reading_icon,
display_only_when_signed_in: true,
position: 0,
section: :default,
)
NavigationLink.where(url: "#{base_url}/listings").first_or_create(
name: "Listings",
icon: listing_icon,
display_only_when_signed_in: false,
position: 1,
section: :default,
)
NavigationLink.where(url: "#{base_url}/pod").first_or_create(
name: "Podcasts",
icon: mic_icon,
display_only_when_signed_in: false,
position: 2,
section: :default,
)
NavigationLink.where(url: "#{base_url}/videos").first_or_create(
name: "Videos",
icon: camera_icon,
display_only_when_signed_in: false,
position: 3,
section: :default,
)
NavigationLink.where(url: "#{base_url}/tags").first_or_create(
name: "Tags",
icon: tag_icon,
display_only_when_signed_in: false,
position: 4,
section: :default,
)
NavigationLink.where(url: "#{base_url}/code-of-conduct").first_or_create(
name: "Code of Conduct",
icon: thumb_up_icon,
display_only_when_signed_in: false,
position: 5,
section: :other,
)
NavigationLink.where(url: "#{base_url}/faq").first_or_create(
name: "FAQ",
icon: bulb_icon,
display_only_when_signed_in: false,
position: 6,
section: :default,
)
NavigationLink.where(url: "https://shop.dev.to/").first_or_create(
name: "DEV Shop",
icon: shopping_icon,
display_only_when_signed_in: false,
position: 7,
section: :default,
)
NavigationLink.where(url: "#{base_url}/sponsors").first_or_create(
name: "Sponsors",
icon: heart_icon,
display_only_when_signed_in: false,
position: 8,
section: :default,
)
NavigationLink.where(url: "#{base_url}/about").first_or_create(
name: "About",
icon: rainbowdev,
display_only_when_signed_in: false,
position: 9,
section: :default,
)
NavigationLink.where(url: "#{base_url}/privacy").first_or_create(
name: "Privacy Policy",
icon: smart_icon,
display_only_when_signed_in: false,
position: 10,
section: :other,
)
NavigationLink.where(url: "#{base_url}/terms").first_or_create(
name: "Terms of use",
icon: look_icon,
display_only_when_signed_in: false,
position: 11,
section: :other,
)
NavigationLink.where(url: "#{base_url}/contact").first_or_create(
name: "Contact",
icon: contact_icon,
display_only_when_signed_in: false,
position: 12,
section: :default,
)
end
end

View file

@ -13,5 +13,11 @@ FactoryBot.define do
navigation_link.class.set_callback :save, :before, :strip_local_hostname
end
end
trait :other_section_link do
after(:build) do |navigation_link|
navigation_link.section = "other"
end
end
end
end

View file

@ -0,0 +1,22 @@
require "rails_helper"
require Rails.root.join(
"lib/data_update_scripts/20210712162220_backfill_section_column_for_navigation_links.rb",
)
describe DataUpdateScripts::BackfillSectionColumnForNavigationLinks do
it "backfills relevant navigation links with 'other' section" do
other_navigation_link = create(:navigation_link, url: "/privacy")
expect do
described_class.new.run
end.to change { other_navigation_link.reload.section }.from("default").to("other")
end
it "leaves irrelevant navigation links unchanged" do
default_navigation_link = create(:navigation_link, url: "/readinglist")
expect do
described_class.new.run
end.not_to change { default_navigation_link.reload.section }
end
end

View file

@ -60,12 +60,19 @@ RSpec.describe "NavigationLinks", type: :request do
expect(response).to redirect_to admin_navigation_links_path
end
it "updates the profile field values" do
it "updates the navigation-link field values" do
put admin_navigation_link_path(navigation_link.id),
params: { navigation_link: { name: "Example" } }
changed_navigation_link_record = NavigationLink.find(navigation_link.id)
expect(changed_navigation_link_record.name).to eq("Example")
expect(changed_navigation_link_record.other_section?).to be(false)
put admin_navigation_link_path(navigation_link.id),
params: { navigation_link: { section: "other" } }
changed_navigation_link_record2 = NavigationLink.find(navigation_link.id)
expect(changed_navigation_link_record2.other_section?).to be(true)
end
end

View file

@ -251,6 +251,7 @@ seeder.create_if_none(NavigationLink) do
icon: reading_icon,
display_only_when_signed_in: true,
position: 0,
section: :default,
)
end
@ -267,6 +268,7 @@ seeder.create_if_doesnt_exist(NavigationLink, "url", "/contact") do
position: i + 1,
url: "/contact",
icon: icon,
section: :default,
)
end
end