Add id to collection serializers (#37157)

This commit is contained in:
David Roetzel
2025-12-08 12:40:17 +01:00
committed by GitHub
parent 55becaa1b5
commit cf23f0414f
3 changed files with 10 additions and 2 deletions

View File

@@ -1,11 +1,15 @@
# frozen_string_literal: true # frozen_string_literal: true
class REST::BaseCollectionSerializer < ActiveModel::Serializer class REST::BaseCollectionSerializer < ActiveModel::Serializer
attributes :uri, :name, :description, :local, :sensitive, :discoverable, attributes :id, :uri, :name, :description, :local, :sensitive,
:item_count, :created_at, :updated_at :discoverable, :item_count, :created_at, :updated_at
belongs_to :tag, serializer: REST::StatusSerializer::TagSerializer belongs_to :tag, serializer: REST::StatusSerializer::TagSerializer
def id
object.id.to_s
end
def item_count def item_count
object.respond_to?(:item_count) ? object.item_count : object.collection_items.count object.respond_to?(:item_count) ? object.item_count : object.collection_items.count
end end

View File

@@ -15,6 +15,7 @@ RSpec.describe REST::BaseCollectionSerializer do
let(:tag) { Fabricate(:tag, name: 'discovery') } let(:tag) { Fabricate(:tag, name: 'discovery') }
let(:collection) do let(:collection) do
Fabricate(:collection, Fabricate(:collection,
id: 2342,
name: 'Exquisite follows', name: 'Exquisite follows',
description: 'Always worth a follow', description: 'Always worth a follow',
local: true, local: true,
@@ -26,6 +27,7 @@ RSpec.describe REST::BaseCollectionSerializer do
it 'includes the relevant attributes' do it 'includes the relevant attributes' do
expect(subject) expect(subject)
.to include( .to include(
'id' => '2342',
'name' => 'Exquisite follows', 'name' => 'Exquisite follows',
'description' => 'Always worth a follow', 'description' => 'Always worth a follow',
'local' => true, 'local' => true,

View File

@@ -15,6 +15,7 @@ RSpec.describe REST::CollectionSerializer do
let(:tag) { Fabricate(:tag, name: 'discovery') } let(:tag) { Fabricate(:tag, name: 'discovery') }
let(:collection) do let(:collection) do
Fabricate(:collection, Fabricate(:collection,
id: 2342,
name: 'Exquisite follows', name: 'Exquisite follows',
description: 'Always worth a follow', description: 'Always worth a follow',
local: true, local: true,
@@ -27,6 +28,7 @@ RSpec.describe REST::CollectionSerializer do
expect(subject) expect(subject)
.to include( .to include(
'account' => an_instance_of(Hash), 'account' => an_instance_of(Hash),
'id' => '2342',
'name' => 'Exquisite follows', 'name' => 'Exquisite follows',
'description' => 'Always worth a follow', 'description' => 'Always worth a follow',
'local' => true, 'local' => true,