Skip to content

Commit

Permalink
[PLAY-1426] Redirect If Guide Doesn't Exist (#3653)
Browse files Browse the repository at this point in the history
**What does this PR do?**

- ✅ Redirect to parent guide if child guide doesn't exist
(`guides/#{@parent}/#{@page}`)
- ✅ Redirect to home if parent guide doesn't exist (`guides/#{@parent}`)
- ✅ Add spec tests for guides controller
- ✅ Fix TypeError by adding conditional

**How to test?** Steps to confirm the desired behavior:
1. Go to /guides/getting_started/nonsense
2. You should be redirected to /guides/getting_started/
3. Go to /guides/design_guidelines/nonsense
4. You should be redirected to /guides/design_guidelines/
5. Go to /guides/nonsense
6. You should be redirected to the home page
7. Go to /guides/design_guidelines/typography
8. You should see a document on Typography
9. Go to /guides/getting_started/rails_&_react_setup
10. You should see a document on Ruby & React Setup

#### Checklist:
- [x] **LABELS** Add a label: `enhancement`, `bug`, `improvement`, `new
kit`, `deprecated`, or `breaking`. See [Changelog &
Labels](https://github.com/powerhome/playbook/wiki/Changelog-&-Labels)
for details.
- [x] **DEPLOY** I have added the `milano` label to show I'm ready for a
review.
- [x] **TESTS** I have added test coverage to my code.

---------

Co-authored-by: Sam Duncan <[email protected]>
  • Loading branch information
kangaree and skduncan authored Sep 6, 2024
1 parent e7f0645 commit 0babb59
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
20 changes: 15 additions & 5 deletions playbook-website/app/controllers/guides_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ class GuidesController < ApplicationController

def md_doc
@show_sidebar = true
if @parent
if @guide_exists
if @page
render template: "guides/#{@parent}/#{@page}"
else
render template: "guides/#{@parent}"
end
else
elsif @parent_not_found
redirect_to root_path, flash: { error: "That doc does not exist" }
else
redirect_to "/guides/#{@parent}", flash: { error: "That doc does not exist" }
end
end

Expand All @@ -30,8 +32,16 @@ def set_page_vars
else
Dir.glob("#{Dir[search_path].first}*.md").first
end
@link_extension = File.basename(file)
@front_matter = render_frontmatter(file)
@page_title = @front_matter["title"] || File.basename(file, ".*").split(".")[0].tr("_", " ")
if file == "README.md"
@parent_not_found = true
@guide_exists = false
elsif file
@link_extension = File.basename(file)
@front_matter = render_frontmatter(file)
@page_title = @front_matter["title"] || File.basename(file, ".*").split(".")[0].tr("_", " ")
@guide_exists = true
else
@guide_exists = false
end
end
end
30 changes: 30 additions & 0 deletions playbook-website/spec/controllers/guides_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# spec/controllers/guides_controller_spec.rb
require "rails_helper"

RSpec.describe GuidesController, type: :controller do
describe "GET #md_doc" do
it "renders 'child' guide" do
get :md_doc, params: { parent: "getting_started", page: "rails_&_react_setup" }
expect(response).to be_successful
expect(response).to render_template("guides/getting_started/rails_&_react_setup")
end

it "renders 'parent' guide" do
get :md_doc, params: { parent: "design_guidelines" }
expect(response).to be_successful
expect(response).to render_template("guides/design_guidelines")
end

it "redirects to 'parent' guide if 'child' guide does not exist" do
get :md_doc, params: { parent: "getting_started", page: "johncena" }
expect(response).to redirect_to("/guides/getting_started")
end

it "redirects to home if 'parent' guide does not exist" do
get :md_doc, params: { parent: "johncena" }
expect(response).to redirect_to(root_path)
end
end
end

0 comments on commit 0babb59

Please sign in to comment.