From 3adfe7ace31845510c8cdb7ab9b15fe2d1d86dc6 Mon Sep 17 00:00:00 2001 From: Arit Amana <32520970+msarit@users.noreply.github.com> Date: Wed, 14 Jul 2021 11:01:56 -0400 Subject: [PATCH] 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 --- .../admin/navigation_links_controller.rb | 2 +- app/models/navigation_link.rb | 2 ++ ...2155135_add_section_to_navigation_links.rb | 5 +++++ db/schema.rb | 3 ++- ...ill_section_column_for_navigation_links.rb | 13 +++++++++++ lib/tasks/add_navigation_links.rake | 13 +++++++++++ spec/factories/navigation_links.rb | 6 +++++ ...ection_column_for_navigation_links_spec.rb | 22 +++++++++++++++++++ spec/requests/admin/navigation_link_spec.rb | 9 +++++++- spec/support/seeds/seeds_e2e.rb | 2 ++ 10 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20210712155135_add_section_to_navigation_links.rb create mode 100644 lib/data_update_scripts/20210712162220_backfill_section_column_for_navigation_links.rb create mode 100644 spec/lib/data_update_scripts/backfill_section_column_for_navigation_links_spec.rb diff --git a/app/controllers/admin/navigation_links_controller.rb b/app/controllers/admin/navigation_links_controller.rb index 941b6a665..5d8d8fc7c 100644 --- a/app/controllers/admin/navigation_links_controller.rb +++ b/app/controllers/admin/navigation_links_controller.rb @@ -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" diff --git a/app/models/navigation_link.rb b/app/models/navigation_link.rb index 45f54860a..debdba17b 100644 --- a/app/models/navigation_link.rb +++ b/app/models/navigation_link.rb @@ -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 diff --git a/db/migrate/20210712155135_add_section_to_navigation_links.rb b/db/migrate/20210712155135_add_section_to_navigation_links.rb new file mode 100644 index 000000000..f384a4426 --- /dev/null +++ b/db/migrate/20210712155135_add_section_to_navigation_links.rb @@ -0,0 +1,5 @@ +class AddSectionToNavigationLinks < ActiveRecord::Migration[6.1] + def change + add_column :navigation_links, :section, :integer, default: 0, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index e0c6b6519..b106a10b7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/lib/data_update_scripts/20210712162220_backfill_section_column_for_navigation_links.rb b/lib/data_update_scripts/20210712162220_backfill_section_column_for_navigation_links.rb new file mode 100644 index 000000000..6906cefb1 --- /dev/null +++ b/lib/data_update_scripts/20210712162220_backfill_section_column_for_navigation_links.rb @@ -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 diff --git a/lib/tasks/add_navigation_links.rake b/lib/tasks/add_navigation_links.rake index 91b331d10..be2c46ec3 100644 --- a/lib/tasks/add_navigation_links.rake +++ b/lib/tasks/add_navigation_links.rake @@ -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 diff --git a/spec/factories/navigation_links.rb b/spec/factories/navigation_links.rb index 7170d970f..0f9284f96 100644 --- a/spec/factories/navigation_links.rb +++ b/spec/factories/navigation_links.rb @@ -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 diff --git a/spec/lib/data_update_scripts/backfill_section_column_for_navigation_links_spec.rb b/spec/lib/data_update_scripts/backfill_section_column_for_navigation_links_spec.rb new file mode 100644 index 000000000..895b08fb0 --- /dev/null +++ b/spec/lib/data_update_scripts/backfill_section_column_for_navigation_links_spec.rb @@ -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 diff --git a/spec/requests/admin/navigation_link_spec.rb b/spec/requests/admin/navigation_link_spec.rb index 35d1acf75..625eaf676 100644 --- a/spec/requests/admin/navigation_link_spec.rb +++ b/spec/requests/admin/navigation_link_spec.rb @@ -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 diff --git a/spec/support/seeds/seeds_e2e.rb b/spec/support/seeds/seeds_e2e.rb index 25b333e55..e6abbbc24 100644 --- a/spec/support/seeds/seeds_e2e.rb +++ b/spec/support/seeds/seeds_e2e.rb @@ -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