Skip to content

Commit

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

module Alchemy
module JsonApi
class IngredientVideoSerializer
include IngredientSerializer

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

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

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

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

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

attribute :video_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,52 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::JsonApi::IngredientVideoSerializer do
let(:element) { FactoryBot.build_stubbed(:alchemy_element) }
let(:attachment) { FactoryBot.build_stubbed(:alchemy_attachment) }
let(:ingredient) do
Alchemy::Ingredients::Video.new(
role: "video",
element: element,
attachment: attachment,
allow_fullscreen: true,
autoplay: false,
controls: 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[: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[:value]).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(: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 3d27940

Please sign in to comment.