Skip to content

Commit

Permalink
Merge pull request #10 from AlchemyCMS/essence_page-serializer
Browse files Browse the repository at this point in the history
Add EssencePageSerializer
  • Loading branch information
tvdeyen authored Oct 26, 2020
2 parents d0e3695 + 5630d89 commit 2718328
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ gem "sqlite3"
gem "alchemy_cms", github: "AlchemyCMS/alchemy_cms", branch: "master"
gem "rufo"
gem "rubocop"
gem "pry-byebug"
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 2718328

Please sign in to comment.