Add method to get the top entity to RelatedEntity

This commit is contained in:
Benjamin Neff 2017-09-05 03:19:39 +02:00
parent 3b3f6ad589
commit b25e21f980
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
2 changed files with 22 additions and 0 deletions

View file

@ -24,6 +24,14 @@ module DiasporaFederation
# @return [RelatedEntity] parent entity
entity :parent, Entities::RelatedEntity, default: nil
# The root entity, this entity is responsible for relaying relayables
# @return [RelatedEntity] absolute parent entity
def root
root = self
root = root.parent until root.parent.nil?
root
end
# Get related entity from the backend or fetch it from remote if not available locally
# @return [RelatedEntity] fetched related entity
def self.fetch(author, type, guid)

View file

@ -5,6 +5,20 @@ module DiasporaFederation
it_behaves_like "an Entity subclass"
describe "#root" do
it "returns self if it's already the root" do
entity = Fabricate(:related_entity, parent: nil)
expect(entity.root).to eq(entity)
end
it "returns the root entity if the current entity has parents" do
root = Fabricate(:related_entity, parent: nil)
parent = Fabricate(:related_entity, parent: root)
entity = Fabricate(:related_entity, parent: parent)
expect(entity.root).to eq(root)
end
end
describe ".fetch" do
let(:guid) { Fabricate.sequence(:guid) }
let(:entity) { Fabricate(:related_entity) }