-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is meant to be used from the admin pages preview frame and always returns elements from the draft version.
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
app/controllers/alchemy/json_api/admin/pages_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |