Skip to content

Commit

Permalink
Add an EssencePageSerializer
Browse files Browse the repository at this point in the history
This was missing from the essence serializers
  • Loading branch information
tvdeyen committed Oct 26, 2020
1 parent c5252de commit 5630d89
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
22 changes: 22 additions & 0 deletions app/serializers/alchemy/json_api/essence_page_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true
require "alchemy/json_api/essence_serializer"

module Alchemy::JsonApi
class EssencePageSerializer
include EssenceSerializer

attribute :ingredient do |essence|
essence.page&.url_path
end

attribute :page_name do |essence|
essence.page&.name
end

attribute :page_url do |essence|
essence.page&.url_path
end

has_one :page, record_type: :page, serializer: PageSerializer
end
end
72 changes: 72 additions & 0 deletions spec/serializers/alchemy/json_api/essence_page_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true
require "rails_helper"
require "alchemy/test_support/factories/content_factory"
require "alchemy/test_support/factories/essence_page_factory"

RSpec.describe Alchemy::JsonApi::EssencePageSerializer do
let(:element) { FactoryBot.create(:alchemy_element) }
let(:content) { FactoryBot.create(:alchemy_content, element: element) }
let(:page) { FactoryBot.create(:alchemy_page) }
let(:essence) do
FactoryBot.create(
:alchemy_essence_page,
content: content,
page: page,
)
end
let(:options) { {} }

let(:serializer) { described_class.new(essence, options) }

it_behaves_like "an essence"

subject { serializer.serializable_hash[:data][:attributes] }

describe "attributes" do
it "has page url as ingredient" do
expect(subject[:ingredient]).to eq(page.url_path)
end

it "has page_name" do
expect(subject[:page_name]).to eq(page.name)
end

it "has page_url" do
expect(subject[:page_url]).to eq(page.url_path)
end
end

describe "relationships" do
subject { serializer.serializable_hash[:data][:relationships] }

it "has page object" do
expect(subject[:page]).to eq(data: { id: page.id.to_s, type: :page })
end
end

context "With no page set" do
let(:essence) do
FactoryBot.create(
:alchemy_essence_page,
content: content,
page: nil,
)
end

it_behaves_like "an essence"

describe "attributes" do
it "has no ingredient" do
expect(subject[:ingredient]).to be_nil
end

it "has no page_name" do
expect(subject[:page_name]).to be_nil
end

it "has no page_url" do
expect(subject[:page_url]).to be_nil
end
end
end
end

0 comments on commit 5630d89

Please sign in to comment.