diff --git a/Gemfile b/Gemfile index 2cb16c6..7f6d44f 100644 --- a/Gemfile +++ b/Gemfile @@ -19,3 +19,4 @@ gem "sqlite3" gem "alchemy_cms", github: "AlchemyCMS/alchemy_cms", branch: "master" gem "rufo" gem "rubocop" +gem "pry-byebug" diff --git a/app/serializers/alchemy/json_api/essence_page_serializer.rb b/app/serializers/alchemy/json_api/essence_page_serializer.rb new file mode 100644 index 0000000..214669e --- /dev/null +++ b/app/serializers/alchemy/json_api/essence_page_serializer.rb @@ -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 diff --git a/spec/serializers/alchemy/json_api/essence_page_serializer_spec.rb b/spec/serializers/alchemy/json_api/essence_page_serializer_spec.rb new file mode 100644 index 0000000..b7e9f86 --- /dev/null +++ b/spec/serializers/alchemy/json_api/essence_page_serializer_spec.rb @@ -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