-
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 #52 from AlchemyCMS/more-essence-serializers
More essence serializers
- Loading branch information
Showing
7 changed files
with
244 additions
and
19 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
app/serializers/alchemy/json_api/essence_audio_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,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
13
app/serializers/alchemy/json_api/essence_headline_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,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
42
app/serializers/alchemy/json_api/essence_video_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,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
55
spec/serializers/alchemy/json_api/essence_audio_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,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 |
32 changes: 32 additions & 0 deletions
32
spec/serializers/alchemy/json_api/essence_headline_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,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 |
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
57 changes: 57 additions & 0 deletions
57
spec/serializers/alchemy/json_api/essence_video_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,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 |