Skip to content

Commit

Permalink
Fix empty authors preview card serialization (#33151)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski authored Dec 4, 2024
1 parent 6fddf16 commit e5bea68
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/models/preview_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 134,7 @@ def history
end

def authors
@authors ||= [PreviewCard::Author.new(self)]
@authors ||= Array(serialized_authors)
end

class Author < ActiveModelSerializers::Model
Expand Down Expand Up @@ -169,6 169,13 @@ def image_styles(file)

private

def serialized_authors
if author_name? || author_url?
PreviewCard::Author
.new(self)
end
end

def extract_dimensions
file = image.queued_for_write[:original]

Expand Down
41 changes: 41 additions & 0 deletions spec/serializers/rest/preview_card_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 1,41 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe REST::PreviewCardSerializer do
subject do
serialized_record_json(
preview_card,
described_class
)
end

context 'when preview card does not have author data' do
let(:preview_card) { Fabricate.build :preview_card }

it 'includes empty authors array' do
expect(subject.deep_symbolize_keys)
.to include(
authors: be_an(Array).and(be_empty)
)
end
end

context 'when preview card has author data' do
let(:preview_card) { Fabricate.build :preview_card, author_name: 'Name', author_url: 'https://host.example/123' }

it 'includes populated authors array' do
expect(subject.deep_symbolize_keys)
.to include(
authors: be_an(Array).and(
contain_exactly(
include(
name: 'Name',
url: 'https://host.example/123'
)
)
)
)
end
end
end

0 comments on commit e5bea68

Please sign in to comment.