diff --git a/app/controllers/alchemy/json_api/admin/pages_controller.rb b/app/controllers/alchemy/json_api/admin/pages_controller.rb new file mode 100644 index 0000000..c4b0983 --- /dev/null +++ b/app/controllers/alchemy/json_api/admin/pages_controller.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true +module Alchemy + module JsonApi + module Admin + class PagesController < JsonApi::PagesController + authorize_resource + + def show + render jsonapi: Page.new(@page, page_version: :draft_version) + end + + private + + def load_page + @page = page_scope.find(params[:id]) + end + + def base_page_scope + ::Alchemy::Page.all + end + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 7f2d21f..29f5e44 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,4 +4,8 @@ get "pages/*path" => "pages#show", as: :page resources :layout_pages, only: [:index] get "layout_pages/*path" => "layout_pages#show", as: :layout_page + + namespace :admin do + resources :pages, only: [:show] + end end diff --git a/spec/requests/alchemy/json_api/admin/pages_spec.rb b/spec/requests/alchemy/json_api/admin/pages_spec.rb new file mode 100644 index 0000000..94668d4 --- /dev/null +++ b/spec/requests/alchemy/json_api/admin/pages_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true +require "rails_helper" +require "alchemy/devise/test_support/factories" + +RSpec.describe "Alchemy::JsonApi::Admin::PagesController", type: :request do + let(:page) do + FactoryBot.create(:alchemy_page) + end + + describe "GET /alchemy/json_api/admin/pages/:id" do + subject { get alchemy_json_api.admin_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 +end