-
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.
Merge pull request #10 from AlchemyCMS/essence_page-serializer
Add EssencePageSerializer
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
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
22 changes: 22 additions & 0 deletions
22
app/serializers/alchemy/json_api/essence_page_serializer.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,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
72
spec/serializers/alchemy/json_api/essence_page_serializer_spec.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,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 |