Skip to content

Commit

Permalink
Add admin pages routes
Browse files Browse the repository at this point in the history
This is meant to be used from the admin pages preview frame
and always returns elements from the draft version.
  • Loading branch information
tvdeyen committed Mar 2, 2021
1 parent 92ccd0b commit 4507ec6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/controllers/alchemy/json_api/admin/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
51 changes: 51 additions & 0 deletions spec/requests/alchemy/json_api/admin/pages_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4507ec6

Please sign in to comment.