diff --git a/app/controllers/alchemy/json_api/admin/layout_pages_controller.rb b/app/controllers/alchemy/json_api/admin/layout_pages_controller.rb new file mode 100644 index 0000000..0bce071 --- /dev/null +++ b/app/controllers/alchemy/json_api/admin/layout_pages_controller.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true +module Alchemy + module JsonApi + module Admin + class LayoutPagesController < JsonApi::Admin::PagesController + private + + def page_scope + page_scope_with_includes(page_version: :draft_version).layoutpages + end + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index a22f539..c284720 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,5 +7,6 @@ namespace :admin do get "pages/*path" => "pages#show", as: :page + get "layout_pages/*path" => "layout_pages#show", as: :layout_page end end diff --git a/spec/requests/alchemy/json_api/admin/layout_pages_spec.rb b/spec/requests/alchemy/json_api/admin/layout_pages_spec.rb new file mode 100644 index 0000000..4ab0434 --- /dev/null +++ b/spec/requests/alchemy/json_api/admin/layout_pages_spec.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true +require "rails_helper" +require "alchemy/devise/test_support/factories" + +RSpec.describe "Alchemy::JsonApi::Admin::LayoutPagesController", type: :request do + let(:page) do + FactoryBot.create(:alchemy_page, :layoutpage) + end + + describe "GET /alchemy/json_api/admin/layout_pages/:id" do + subject { get alchemy_json_api.admin_layout_page_path(page) } + + context "as anonymous user" do + it "returns 404" do + get alchemy_json_api.page_path(page.urlname) + expect(response).to have_http_status(404) + end + end + + context "as author user" do + before do + allow_any_instance_of(ApplicationController).to receive(:current_user) do + FactoryBot.create(:alchemy_author_user) + end + end + + let(:document) { JSON.parse(response.body) } + + it "gets a valid JSON:API document" do + subject + expect(response).to have_http_status(200) + expect(document["data"]).to have_id(page.id.to_s) + expect(document["data"]).to have_type("page") + end + + it "loads elements from draft version" do + element = FactoryBot.create(:alchemy_element, page_version: page.draft_version) + subject + document = JSON.parse(response.body) + expect(document["data"]["relationships"]["elements"]).to eq( + { + "data" => [{ + "id" => element.id.to_s, + "type" => "element", + }], + } + ) + end + end + end + + describe "GET /alchemy/json_api/admin/layout_pages/:urlpath" do + subject { get alchemy_json_api.admin_layout_page_path(page.urlname) } + + context "as author user" do + before do + allow_any_instance_of(ApplicationController).to receive(:current_user) do + FactoryBot.create(:alchemy_author_user) + end + end + + it "gets a valid JSON:API document" do + subject + expect(response).to have_http_status(200) + end + end + end +end