From 8a599e1c1dca39c4dbfe58caa9bef4a69f3d19ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonne=20Ha=C3=9F?= Date: Wed, 14 May 2014 18:50:52 +0200 Subject: Use absolute_root more consistently in Reshare Closes #4956 --- app/models/reshare.rb | 41 +++++++++++++---------------------------- spec/models/reshare_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/app/models/reshare.rb b/app/models/reshare.rb index da902dc3d..5bb5326d5 100644 --- a/app/models/reshare.rb +++ b/app/models/reshare.rb @@ -29,28 +29,24 @@ class Reshare < Post self.root.author.diaspora_handle end - def o_embed_cache - self.root ? root.o_embed_cache : super - end - - def open_graph_cache - self.root ? root.open_graph_cache : super - end + delegate :o_embed_cache, :open_graph_cache, + :message, :nsfw, + to: :absolute_root, allow_nil: true def raw_message - self.root ? root.raw_message : super - end - - def message - absolute_root.message if root.present? + absolute_root.try(:raw_message) || super end def mentioned_people - self.root ? root.mentioned_people : super + absolute_root.try(:mentioned_people) || super end def photos - self.root ? root.photos : [] + absolute_root.try(:photos) || super + end + + def address + absolute_root.try(:location).try(:address) end def receive(recipient, sender) @@ -69,21 +65,10 @@ class Reshare < Post Notifications::Reshared if root.author == user.person end - def nsfw - root.try(:nsfw) - end - def absolute_root - current = self - while( current.is_a?(Reshare) ) - current = current.root - end - - current - end - - def address - absolute_root.try(:location).try(:address) + @absolute_root ||= self + @absolute_root = @absolute_root.root while @absolute_root.is_a? Reshare + @absolute_root end private diff --git a/spec/models/reshare_spec.rb b/spec/models/reshare_spec.rb index 52a484785..63e4295b8 100644 --- a/spec/models/reshare_spec.rb +++ b/spec/models/reshare_spec.rb @@ -83,11 +83,38 @@ describe Reshare do rs1 = FactoryGirl.build(:reshare, :root=>@sm) rs2 = FactoryGirl.build(:reshare, :root=>rs1) @rs3 = FactoryGirl.build(:reshare, :root=>rs2) + + sm = FactoryGirl.create(:status_message, :author => alice.person, :public => true) + rs1 = FactoryGirl.create(:reshare, :root => sm) + @of_deleted = FactoryGirl.build(:reshare, :root => rs1) + sm.destroy + rs1.reload end it 'resolves root posts to the top level' do @rs3.absolute_root.should == @sm end + + it 'can handle deleted reshares' do + expect(@of_deleted.absolute_root).to be_nil + end + + it 'is used everywhere' do + expect(@rs3.message).to eq @sm.message + expect(@of_deleted.message).to be_nil + expect(@rs3.photos).to eq @sm.photos + expect(@of_deleted.photos).to be_empty + expect(@rs3.o_embed_cache).to eq @sm.o_embed_cache + expect(@of_deleted.o_embed_cache).to be_nil + expect(@rs3.open_graph_cache).to eq @sm.open_graph_cache + expect(@of_deleted.open_graph_cache).to be_nil + expect(@rs3.mentioned_people).to eq @sm.mentioned_people + expect(@of_deleted.mentioned_people).to be_empty + expect(@rs3.nsfw).to eq @sm.nsfw + expect(@of_deleted.nsfw).to be_nil + expect(@rs3.address).to eq @sm.location.try(:address) + expect(@of_deleted.address).to be_nil + end end describe "XML" do -- cgit v1.2.3