Skip to content

Commit

Permalink
Merge pull request #52 from AlchemyCMS/more-essence-serializers
Browse files Browse the repository at this point in the history
More essence serializers
  • Loading branch information
tvdeyen authored Jul 5, 2021
2 parents e6bd2e1 + 4f2c59e commit 25f13f4
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 19 deletions.
40 changes: 40 additions & 0 deletions app/serializers/alchemy/json_api/essence_audio_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require "alchemy/json_api/essence_serializer"

module Alchemy
module JsonApi
class EssenceAudioSerializer
include EssenceSerializer

attributes(
:autoplay,
:controls,
:muted,
:loop,
)

attribute :ingredient do |essence|
essence.attachment&.url
end

with_options if: ->(essence) { essence.attachment } do
attribute :audio_name do |essence|
essence.attachment.name
end

attribute :audio_file_name do |essence|
essence.attachment.file_name
end

attribute :audio_mime_type do |essence|
essence.attachment.file_mime_type
end

attribute :audio_file_size do |essence|
essence.attachment.file_size
end
end
end
end
end
13 changes: 13 additions & 0 deletions app/serializers/alchemy/json_api/essence_headline_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require "alchemy/json_api/essence_serializer"

module Alchemy
module JsonApi
class EssenceHeadlineSerializer
include EssenceSerializer

attributes :level, :size
end
end
end
42 changes: 42 additions & 0 deletions app/serializers/alchemy/json_api/essence_video_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require "alchemy/json_api/essence_serializer"

module Alchemy
module JsonApi
class EssenceVideoSerializer
include EssenceSerializer

attributes(
:width,
:height,
:allow_fullscreen,
:autoplay,
:controls,
:preload,
)

attribute :ingredient do |essence|
essence.attachment&.url
end

with_options if: ->(essence) { essence.attachment } do
attribute :video_name do |essence|
essence.attachment.name
end

attribute :video_file_name do |essence|
essence.attachment.file_name
end

attribute :video_mime_type do |essence|
essence.attachment.file_mime_type
end

attribute :video_file_size do |essence|
essence.attachment.file_size
end
end
end
end
end
55 changes: 55 additions & 0 deletions spec/serializers/alchemy/json_api/essence_audio_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::JsonApi::EssenceAudioSerializer do
let(:element) { FactoryBot.create(:alchemy_element) }
let(:content) { FactoryBot.create(:alchemy_content, element: element) }
let(:attachment) { FactoryBot.create(:alchemy_attachment) }
let(:essence) do
Alchemy::EssenceAudio.create(
content: content,
attachment: attachment,
)
end
let(:options) { {} }

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

it_behaves_like "an essence serializer"

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

it "has the right keys and values", :aggregate_failures do
expect(subject[:autoplay]).to eq(false)
expect(subject[:controls]).to eq(true)
expect(subject[:muted]).to eq(false)
expect(subject[:loop]).to eq(false)
expect(subject[:ingredient]).to match(/\/attachment\/#{attachment.id}\/show/o)
expect(subject[:audio_name]).to eq("image")
expect(subject[:audio_file_name]).to eq("image.png")
expect(subject[:audio_mime_type]).to eq("image/png")
expect(subject[:audio_file_size]).to eq(70)
end
end

context "With no video set" do
let(:essence) do
Alchemy::EssenceAudio.create(
content: content,
attachment: nil,
)
end

it_behaves_like "an essence serializer"

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

it "has the right keys and values" do
expect(subject[:ingredient]).to be nil
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::JsonApi::EssenceHeadlineSerializer do
let(:element) { FactoryBot.create(:alchemy_element) }
let(:content) { FactoryBot.create(:alchemy_content, element: element) }
let(:essence) do
Alchemy::EssenceHeadline.create(
content: content,
body: "Hello you world",
size: 2,
level: 3,
)
end

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

it_behaves_like "an essence serializer"

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

it do
is_expected.to match(
hash_including(
ingredient: "Hello you world",
level: 3,
size: 2,
)
)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
RSpec.describe Alchemy::JsonApi::EssencePictureSerializer do
let(:element) { FactoryBot.create(:alchemy_element) }
let(:content) { FactoryBot.create(:alchemy_content, element: element) }
let(:picture) { FactoryBot.create(:alchemy_picture, image_file_size: 301) }
let(:essence) do
FactoryBot.create(
:alchemy_essence_picture,
title: "Picture",
content: content,
link: "/hello",
picture: picture
)
end
let(:options) { {} }
Expand All @@ -27,23 +29,15 @@
expect(subject[:image_name]).to eq("image")
expect(subject[:image_file_name]).to eq("image.png")
expect(subject[:image_mime_type]).to eq("image/png")
expect(subject[:image_file_size]).to eq(70)
expect(subject[:image_file_size]).to eq(301)
expect(subject[:image_dimensions]).to eq(width: 1, height: 1)
end

describe "image_dimensions" do
let(:image_dimensions) { subject[:image_dimensions] }

context "without image" do
let(:essence) do
FactoryBot.create(
:alchemy_essence_picture,
title: "Picture",
content: content,
link: "/hello",
picture: nil,
)
end
let(:picture) { nil }

it { expect(image_dimensions).to be_nil }
end
Expand Down Expand Up @@ -91,15 +85,7 @@
end

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

it_behaves_like "an essence serializer"
let(:picture) { nil }

describe "attributes" do
subject { serializer.serializable_hash[:data][:attributes] }
Expand Down
57 changes: 57 additions & 0 deletions spec/serializers/alchemy/json_api/essence_video_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::JsonApi::EssenceVideoSerializer do
let(:element) { FactoryBot.create(:alchemy_element) }
let(:content) { FactoryBot.create(:alchemy_content, element: element) }
let(:attachment) { FactoryBot.create(:alchemy_attachment) }
let(:essence) do
Alchemy::EssenceVideo.create(
content: content,
attachment: attachment,
)
end
let(:options) { {} }

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

it_behaves_like "an essence serializer"

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

it "has the right keys and values", :aggregate_failures do
expect(subject[:width]).to eq(nil)
expect(subject[:height]).to eq(nil)
expect(subject[:allow_fullscreen]).to eq(true)
expect(subject[:autoplay]).to eq(false)
expect(subject[:controls]).to eq(true)
expect(subject[:preload]).to eq(nil)
expect(subject[:ingredient]).to match(/\/attachment\/#{attachment.id}\/show/o)
expect(subject[:video_name]).to eq("image")
expect(subject[:video_file_name]).to eq("image.png")
expect(subject[:video_mime_type]).to eq("image/png")
expect(subject[:video_file_size]).to eq(70)
end
end

context "With no video set" do
let(:essence) do
Alchemy::EssenceVideo.create(
content: content,
attachment: nil,
)
end

it_behaves_like "an essence serializer"

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

it "has the right keys and values" do
expect(subject[:ingredient]).to be nil
end
end
end
end

0 comments on commit 25f13f4

Please sign in to comment.