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 Layout Pages Controller #2

Merged
merged 1 commit into from
May 9, 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
11 changes: 11 additions & 0 deletions app/controllers/alchemy/json_api/layout_pages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Alchemy
module JsonApi
class LayoutPagesController < JsonApi::PagesController
private

def page_scope
base_page_scope.layoutpages
end
end
end
end
6 changes: 5 additions & 1 deletion app/controllers/alchemy/json_api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class PagesController < JsonApi::BaseController
before_action :load_page, only: :show

def index
allowed = [:page_layout, :layoutpage]
allowed = [:page_layout]

jsonapi_filter(page_scope, allowed) do |filtered|
render jsonapi: filtered.result
Expand All @@ -31,6 +31,10 @@ def load_page_by_urlname
end

def page_scope
base_page_scope.contentpages
end

def base_page_scope
::Alchemy::Page.
with_language(Language.current).
published.
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Alchemy::JsonApi::Engine.routes.draw do
resources :pages, only: [:show, :index]
resources :layout_pages, only: [:show, :index]
end
70 changes: 70 additions & 0 deletions spec/requests/alchemy/json_api/layout_pages_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'rails_helper'
require "alchemy/test_support/factories/page_factory"
require "alchemy/test_support/factories/element_factory"

RSpec.describe "Alchemy::JsonApi::Pages", type: :request do
let(:page) do
FactoryBot.create(
:alchemy_page,
:public,
urlname: nil,
title: "Footer",
layoutpage: true
)
end

describe "GET /alchemy/json_api/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)
document = JSON.parse(response.body)
expect(document['data']).to have_id(page.id.to_s)
expect(document['data']).to have_type("page")
end

context 'when requesting a content page' do
let(:page) { FactoryBot.create(:alchemy_page, :public, elements: [element]) }
let(:element) { FactoryBot.create(:alchemy_element, name: "article", autogenerate_contents: true) }

it "returns a 404" do
get alchemy_json_api.layout_page_path(page)
expect(response).to have_http_status(404)
end
end

context "when requesting a URL" do
it "finds the page" do
get alchemy_json_api.layout_page_path(page.urlname)
expect(response).to have_http_status(200)
document = JSON.parse(response.body)
expect(document['data']).to have_id(page.id.to_s)
expect(document['data']).to have_type("page")
end
end

context 'when the language is incorrect' do
let!(:language) { FactoryBot.create(:alchemy_language) }
let!(:other_language) { FactoryBot.create(:alchemy_language, :english) }
let(:page) { FactoryBot.create(:alchemy_page, :public, layoutpage: true, language: other_language) }

it 'returns a 404' do
get alchemy_json_api.layout_page_path(page.urlname)
expect(response).to have_http_status(404)
end
end
end

describe "GET /alchemy/json_api/pages" do
let!(:layoutpage) { FactoryBot.create(:alchemy_page, :layoutpage, :public) }
let!(:non_public_layout_page) { FactoryBot.create(:alchemy_page, :layoutpage) }
let!(:public_page) { FactoryBot.create(:alchemy_page, :public) }

it "displays the layoutpage and the public page" do
get alchemy_json_api.layout_pages_path
document = JSON.parse(response.body)
expect(document['data']).to include(have_id(layoutpage.id.to_s))
expect(document['data']).not_to include(have_id(non_public_layout_page.id.to_s))
expect(document['data']).not_to include(have_id(public_page.id.to_s))
end
end
end
12 changes: 1 addition & 11 deletions spec/requests/alchemy/json_api/pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,9 @@
it "displays the layoutpage and the public page" do
get alchemy_json_api.pages_path
document = JSON.parse(response.body)
expect(document['data']).to include(have_id(layoutpage.id.to_s))
expect(document['data']).not_to include(have_id(layoutpage.id.to_s))
expect(document['data']).not_to include(have_id(non_public_page.id.to_s))
expect(document['data']).to include(have_id(public_page.id.to_s))
end

context "with a filter parameter" do
it "displays the layoutpage but not the public page" do
get alchemy_json_api.pages_path(filter: {layoutpage_true: "1"})
document = JSON.parse(response.body)
expect(document['data']).to include(have_id(layoutpage.id.to_s))
expect(document['data']).not_to include(have_id(non_public_page.id.to_s))
expect(document['data']).not_to include(have_id(public_page.id.to_s))
end
end
end
end