parent
2e5ec8b7c8
commit
367ab6764f
@ -0,0 +1,30 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ActivityPub::Serializer < ActiveModel::Serializer
|
||||
with_options instance_writer: false, instance_reader: true do |serializer|
|
||||
serializer.class_attribute :_named_contexts
|
||||
serializer.class_attribute :_context_extensions
|
||||
|
||||
self._named_contexts ||= {}
|
||||
self._context_extensions ||= {}
|
||||
end
|
||||
|
||||
def self.inherited(base)
|
||||
super
|
||||
|
||||
base._named_contexts = _named_contexts.dup
|
||||
base._context_extensions = _context_extensions.dup
|
||||
end
|
||||
|
||||
def self.context(*named_contexts)
|
||||
named_contexts.each do |context|
|
||||
_named_contexts[context] = true
|
||||
end
|
||||
end
|
||||
|
||||
def self.context_extensions(*extension_names)
|
||||
extension_names.each do |extension_name|
|
||||
_context_extensions[extension_name] = true
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,88 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe ActivityPub::Adapter do
|
||||
class TestObject < ActiveModelSerializers::Model
|
||||
attributes :foo
|
||||
end
|
||||
|
||||
class TestWithBasicContextSerializer < ActivityPub::Serializer
|
||||
attributes :foo
|
||||
end
|
||||
|
||||
class TestWithNamedContextSerializer < ActivityPub::Serializer
|
||||
context :security
|
||||
attributes :foo
|
||||
end
|
||||
|
||||
class TestWithNestedNamedContextSerializer < ActivityPub::Serializer
|
||||
attributes :foo
|
||||
|
||||
has_one :virtual_object, key: :baz, serializer: TestWithNamedContextSerializer
|
||||
|
||||
def virtual_object
|
||||
object
|
||||
end
|
||||
end
|
||||
|
||||
class TestWithContextExtensionSerializer < ActivityPub::Serializer
|
||||
context_extensions :sensitive
|
||||
attributes :foo
|
||||
end
|
||||
|
||||
class TestWithNestedContextExtensionSerializer < ActivityPub::Serializer
|
||||
context_extensions :manually_approves_followers
|
||||
attributes :foo
|
||||
|
||||
has_one :virtual_object, key: :baz, serializer: TestWithContextExtensionSerializer
|
||||
|
||||
def virtual_object
|
||||
object
|
||||
end
|
||||
end
|
||||
|
||||
describe '#serializable_hash' do
|
||||
let(:serializer_class) {}
|
||||
|
||||
subject { ActiveModelSerializers::SerializableResource.new(TestObject.new(foo: 'bar'), serializer: serializer_class, adapter: described_class).as_json }
|
||||
|
||||
context 'when serializer defines no context' do
|
||||
let(:serializer_class) { TestWithBasicContextSerializer }
|
||||
|
||||
it 'renders a basic @context' do
|
||||
expect(subject).to include({ '@context' => 'https://www.w3.org/ns/activitystreams' })
|
||||
end
|
||||
end
|
||||
|
||||
context 'when serializer defines a named context' do
|
||||
let(:serializer_class) { TestWithNamedContextSerializer }
|
||||
|
||||
it 'renders a @context with both items' do
|
||||
expect(subject).to include({ '@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'] })
|
||||
end
|
||||
end
|
||||
|
||||
context 'when serializer has children that define a named context' do
|
||||
let(:serializer_class) { TestWithNestedNamedContextSerializer }
|
||||
|
||||
it 'renders a @context with both items' do
|
||||
expect(subject).to include({ '@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'] })
|
||||
end
|
||||
end
|
||||
|
||||
context 'when serializer defines context extensions' do
|
||||
let(:serializer_class) { TestWithContextExtensionSerializer }
|
||||
|
||||
it 'renders a @context with the extension' do
|
||||
expect(subject).to include({ '@context' => ['https://www.w3.org/ns/activitystreams', { 'sensitive' => 'as:sensitive' }] })
|
||||
end
|
||||
end
|
||||
|
||||
context 'when serializer has children that define context extensions' do
|
||||
let(:serializer_class) { TestWithNestedContextExtensionSerializer }
|
||||
|
||||
it 'renders a @context with both extensions' do
|
||||
expect(subject).to include({ '@context' => ['https://www.w3.org/ns/activitystreams', { 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers', 'sensitive' => 'as:sensitive' }] })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue