Add method to get the top entity to RelatedEntity
This commit is contained in:
parent
3b3f6ad589
commit
b25e21f980
2 changed files with 22 additions and 0 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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) }
|
||||
|
|
|
|||
Loading…
Reference in a new issue