Create Default Nav Links In New Forem (RFC #237) (#14345)

* schema file undelete description

* update with main

* update with origin

* update

* create rake task for new-forem nav links

* remove debugger

* create rake task for new-forem nav links

* remove debugger

* incorporate review feedback

* DRY image paths up

Co-authored-by: Jamie Gaskins <jgaskins@hey.com>
This commit is contained in:
Arit Amana 2021-08-02 12:52:36 -04:00 committed by GitHub
parent 9e1affeb05
commit 812bfa2bbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 104 additions and 24 deletions

View file

@ -1,25 +1,77 @@
# rubocop:disable Metrics/BlockLength
namespace :navigation_links do
def image_path(*paths)
File.read(Rails.root.join("app/assets/images/#{paths.join('/')}")).freeze
end
def twemoji_path(name)
image_path("twemoji", name)
end
reading_icon = twemoji_path("drawer.svg")
contact_icon = twemoji_path("contact.svg")
thumb_up_icon = twemoji_path("thumb-up.svg")
smart_icon = twemoji_path("smart.svg")
look_icon = twemoji_path("look.svg")
listing_icon = twemoji_path("listing.svg")
mic_icon = twemoji_path("mic.svg")
camera_icon = twemoji_path("camera.svg")
tag_icon = twemoji_path("tag.svg")
bulb_icon = twemoji_path("bulb.svg")
shopping_icon = twemoji_path("shopping.svg")
heart_icon = twemoji_path("heart.svg")
rainbowdev = image_path("rainbowdev.svg")
desc "Create navigation links for new forem"
task create: :environment do
NavigationLink.where(url: "/readinglist").first_or_create(
name: "Reading List",
url: URL.url("readinglist"),
icon: reading_icon,
display_only_when_signed_in: true,
position: 0,
section: :default,
)
NavigationLink.where(url: "/contact").first_or_create(
name: "Contact",
url: URL.url("contact"),
icon: contact_icon,
display_only_when_signed_in: false,
position: 1,
section: :default,
)
NavigationLink.where(url: "/code-of-conduct").first_or_create(
name: "Code of Conduct",
url: URL.url("code-of-conduct"),
icon: thumb_up_icon,
display_only_when_signed_in: false,
position: 0,
section: :other,
)
NavigationLink.where(url: "/privacy").first_or_create(
name: "Privacy Policy",
url: URL.url("privacy"),
icon: smart_icon,
display_only_when_signed_in: false,
position: 1,
section: :other,
)
NavigationLink.where(url: "/terms").first_or_create(
name: "Terms of use",
url: URL.url("terms"),
icon: look_icon,
display_only_when_signed_in: false,
position: 2,
section: :other,
)
end
desc "Update DEV's navigation_links"
task update: :environment do
protocol = ApplicationConfig["APP_PROTOCOL"].freeze
domain = Rails.application&.initialized? ? Settings::General.app_domain : ApplicationConfig["APP_DOMAIN"]
base_url = "#{protocol}#{domain}".freeze
reading_icon = File.read(Rails.root.join("app/assets/images/twemoji/drawer.svg")).freeze
thumb_up_icon = File.read(Rails.root.join("app/assets/images/twemoji/thumb-up.svg")).freeze
smart_icon = File.read(Rails.root.join("app/assets/images/twemoji/smart.svg")).freeze
look_icon = File.read(Rails.root.join("app/assets/images/twemoji/look.svg")).freeze
contact_icon = File.read(Rails.root.join("app/assets/images/twemoji/contact.svg")).freeze
listing_icon = File.read(Rails.root.join("app/assets/images/twemoji/listing.svg")).freeze
mic_icon = File.read(Rails.root.join("app/assets/images/twemoji/mic.svg")).freeze
camera_icon = File.read(Rails.root.join("app/assets/images/twemoji/camera.svg")).freeze
tag_icon = File.read(Rails.root.join("app/assets/images/twemoji/tag.svg")).freeze
bulb_icon = File.read(Rails.root.join("app/assets/images/twemoji/bulb.svg")).freeze
shopping_icon = File.read(Rails.root.join("app/assets/images/twemoji/shopping.svg")).freeze
heart_icon = File.read(Rails.root.join("app/assets/images/twemoji/heart.svg")).freeze
rainbowdev = File.read(Rails.root.join("app/assets/images/rainbowdev.svg")).freeze
NavigationLink.where(url: "#{base_url}/readinglist").first_or_create(
name: "Reading List",
icon: reading_icon,
@ -59,56 +111,56 @@ namespace :navigation_links do
name: "Code of Conduct",
icon: thumb_up_icon,
display_only_when_signed_in: false,
position: 5,
position: 0,
section: :other,
)
NavigationLink.where(url: "#{base_url}/faq").first_or_create(
name: "FAQ",
icon: bulb_icon,
display_only_when_signed_in: false,
position: 6,
position: 5,
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,
position: 6,
section: :default,
)
NavigationLink.where(url: "#{base_url}/sponsors").first_or_create(
name: "Sponsors",
icon: heart_icon,
display_only_when_signed_in: false,
position: 8,
position: 7,
section: :default,
)
NavigationLink.where(url: "#{base_url}/about").first_or_create(
name: "About",
icon: rainbowdev,
display_only_when_signed_in: false,
position: 9,
position: 8,
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,
position: 1,
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,
position: 2,
section: :other,
)
NavigationLink.where(url: "#{base_url}/contact").first_or_create(
name: "Contact",
icon: contact_icon,
display_only_when_signed_in: false,
position: 12,
position: 9,
section: :default,
)
end

View file

@ -3,8 +3,7 @@ namespace :forem do
# Since we execute this tasks in bin/setup as well as app_initializer:setup
# everything happening here needs to be idempotent.
task setup: :environment do
# NOTE: There are currently no tasks here. This will change as soon as we
# add/update the task for seeding navigation links to support RFC #237.
Rake::Task["navigation_links:create"].invoke
end
task health_check_token: :environment do

View file

@ -0,0 +1,29 @@
require "rails_helper"
RSpec.describe "Navigation Links tasks", type: :task do
before do
Rake::Task.clear
PracticalDeveloper::Application.load_tasks
end
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)
end
context "when navigation links exist" do
before do
NavigationLink.destroy_all
create(:navigation_link,
name: "Reading List",
url: URL.url("readinglist"),
display_only_when_signed_in: true,
position: 0)
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)
end
end
end
end