Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nested urlnames #7

Merged
merged 1 commit into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/controllers/alchemy/json_api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ def load_page
end

def load_page_by_id
page_scope.find_by(id: params[:id])
page_scope.find_by(id: params[:path])
end

def load_page_by_urlname
# The route param is called :id although it might be a string
page_scope.find_by(urlname: params[:id])
page_scope.find_by(urlname: params[:path])
end

def page_scope
Expand Down
6 changes: 4 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true
Alchemy::JsonApi::Engine.routes.draw do
resources :pages, only: [:show, :index]
resources :layout_pages, only: [:show, :index]
resources :pages, only: [:index]
get "pages/*path" => "pages#show", as: :page
resources :layout_pages, only: [:index]
get "layout_pages/*path" => "layout_pages#show", as: :layout_page
end
6 changes: 3 additions & 3 deletions spec/requests/alchemy/json_api/layout_pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "alchemy/test_support/factories/page_factory"
require "alchemy/test_support/factories/element_factory"

RSpec.describe "Alchemy::JsonApi::Pages", type: :request do
RSpec.describe "Alchemy::JsonApi::LayoutPagesController", type: :request do
let(:page) do
FactoryBot.create(
:alchemy_page,
Expand All @@ -14,7 +14,7 @@
)
end

describe "GET /alchemy/json_api/pages/:id" do
describe "GET /alchemy/json_api/layout_pages/:id" do
it "gets a valid JSON:API document" do
get alchemy_json_api.layout_page_path(page)
expect(response).to have_http_status(200)
Expand Down Expand Up @@ -55,7 +55,7 @@
end
end

describe "GET /alchemy/json_api/pages" do
describe "GET /alchemy/json_api/layout_pages" do
context "with contentpages and unpublished layout pages" do
let!(:layoutpage) { FactoryBot.create(:alchemy_page, :layoutpage, :public) }
let!(:non_public_layout_page) { FactoryBot.create(:alchemy_page, :layoutpage) }
Expand Down
15 changes: 15 additions & 0 deletions spec/requests/alchemy/json_api/pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@
end
end

context "when requesting a nested URL" do
let(:page) do
FactoryBot.create(
:alchemy_page,
:public,
urlname: "a-nested/page",
)
end

it "finds the page" do
get alchemy_json_api.page_path(page.urlname)
expect(response).to have_http_status(200)
end
end

context "when the language is incorrect" do
let!(:language) { FactoryBot.create(:alchemy_language) }
let!(:other_language) { FactoryBot.create(:alchemy_language, :german) }
Expand Down
38 changes: 38 additions & 0 deletions spec/routing/layout_page_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Page Routing" do
routes { Alchemy::JsonApi::Engine.routes }

it "routes layout_pages/" do
expect(get: "/layout_pages").to route_to(
controller: "alchemy/json_api/layout_pages",
action: "index",
)
end

it "routes layout_pages/:id" do
expect(get: "/layout_pages/1").to route_to(
controller: "alchemy/json_api/layout_pages",
action: "show",
path: "1",
)
end

it "routes layout_pages/:urlname" do
expect(get: "/layout_pages/a-page").to route_to(
controller: "alchemy/json_api/layout_pages",
action: "show",
path: "a-page",
)
end

it "routes layout_pages/:nested/urlname" do
expect(get: "/layout_pages/a-nested/page").to route_to(
controller: "alchemy/json_api/layout_pages",
action: "show",
path: "a-nested/page",
)
end
end
38 changes: 38 additions & 0 deletions spec/routing/page_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Page Routing" do
routes { Alchemy::JsonApi::Engine.routes }

it "routes pages/" do
expect(get: "/pages").to route_to(
controller: "alchemy/json_api/pages",
action: "index",
)
end

it "routes pages/:id" do
expect(get: "/pages/1").to route_to(
controller: "alchemy/json_api/pages",
action: "show",
path: "1",
)
end

it "routes pages/:urlname" do
expect(get: "/pages/a-page").to route_to(
controller: "alchemy/json_api/pages",
action: "show",
path: "a-page",
)
end

it "routes pages/:nested/urlname" do
expect(get: "/pages/a-nested/page").to route_to(
controller: "alchemy/json_api/pages",
action: "show",
path: "a-nested/page",
)
end
end