Skip to content

Commit

Permalink
Add Ingredient Audio serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed Jul 5, 2021
1 parent 3d27940 commit bd9f687
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
40 changes: 40 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_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/ingredient_serializer"

module Alchemy
module JsonApi
class IngredientAudioSerializer
include IngredientSerializer

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

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

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

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

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

attribute :audio_file_size do |ingredient|
ingredient.attachment.file_size
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::JsonApi::IngredientAudioSerializer do
let(:element) { FactoryBot.build_stubbed(:alchemy_element) }
let(:attachment) { FactoryBot.build_stubbed(:alchemy_attachment) }
let(:ingredient) do
Alchemy::Ingredients::Audio.new(
role: "audio",
element: element,
attachment: attachment,
autoplay: false,
controls: true,
muted: true,
)
end

subject(:serializer) { described_class.new(ingredient) }

it_behaves_like "an ingredient 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(true)
expect(subject[:leep]).to eq(nil)
expect(subject[:value]).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 audio set" do
let(:attachment) { nil }

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 bd9f687

Please sign in to comment.