mirror of
https://github.com/mastodon/mastodon
synced 2025-04-12 00:56:38 +02:00
Add QuoteSerializer
This commit is contained in:
parent
5daa4eedf8
commit
6909c6bc9a
11
app/serializers/rest/quote_serializer.rb
Normal file
11
app/serializers/rest/quote_serializer.rb
Normal file
@ -0,0 +1,11 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::QuoteSerializer < ActiveModel::Serializer
|
||||
attributes :state
|
||||
|
||||
has_one :quoted_status, serializer: REST::ShallowStatusSerializer
|
||||
|
||||
def quoted_status
|
||||
object.quoted_status if object.accepted?
|
||||
end
|
||||
end
|
9
app/serializers/rest/shallow_quote_serializer.rb
Normal file
9
app/serializers/rest/shallow_quote_serializer.rb
Normal file
@ -0,0 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::ShallowQuoteSerializer < ActiveModel::Serializer
|
||||
attributes :state, :quoted_status_id
|
||||
|
||||
def quoted_status_id
|
||||
object.quoted_status&.id&.to_s if object.accepted?
|
||||
end
|
||||
end
|
8
app/serializers/rest/shallow_status_serializer.rb
Normal file
8
app/serializers/rest/shallow_status_serializer.rb
Normal file
@ -0,0 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::ShallowStatusSerializer < REST::StatusSerializer
|
||||
# It looks like defining one `has_one` requires redefining all of them
|
||||
has_one :quote, key: :quote, serializer: REST::ShallowQuoteSerializer
|
||||
has_one :preview_card, key: :card, serializer: REST::PreviewCardSerializer
|
||||
has_one :preloadable_poll, key: :poll, serializer: REST::PollSerializer
|
||||
end
|
@ -29,6 +29,7 @@ class REST::StatusSerializer < ActiveModel::Serializer
|
||||
has_many :tags
|
||||
has_many :emojis, serializer: REST::CustomEmojiSerializer
|
||||
|
||||
has_one :quote, key: :quote, serializer: REST::QuoteSerializer
|
||||
has_one :preview_card, key: :card, serializer: REST::PreviewCardSerializer
|
||||
has_one :preloadable_poll, key: :poll, serializer: REST::PollSerializer
|
||||
|
||||
|
54
spec/serializers/rest/quote_serializer_spec.rb
Normal file
54
spec/serializers/rest/quote_serializer_spec.rb
Normal file
@ -0,0 +1,54 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe REST::QuoteSerializer do
|
||||
subject do
|
||||
serialized_record_json(
|
||||
quote,
|
||||
described_class,
|
||||
options: {
|
||||
scope: current_user,
|
||||
scope_name: :current_user,
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
let(:current_user) { Fabricate(:user) }
|
||||
let(:quote) { Fabricate(:quote) }
|
||||
|
||||
context 'with a pending quote' do
|
||||
it 'returns expected values' do
|
||||
expect(subject.deep_symbolize_keys)
|
||||
.to include(
|
||||
quoted_status: nil,
|
||||
state: 'pending'
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with an accepted quote' do
|
||||
let(:quote) { Fabricate(:quote, state: :accepted) }
|
||||
|
||||
it 'returns expected values' do
|
||||
expect(subject.deep_symbolize_keys)
|
||||
.to include(
|
||||
quoted_status: be_a(Hash),
|
||||
state: 'accepted'
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with a recursive accepted quote' do
|
||||
let(:status) { Fabricate(:status) }
|
||||
let(:quote) { Fabricate(:quote, status: status, quoted_status: status, state: :accepted) }
|
||||
|
||||
it 'returns expected values' do
|
||||
expect(subject.deep_symbolize_keys)
|
||||
.to include(
|
||||
quoted_status: be_a(Hash),
|
||||
state: 'accepted'
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user