From 1fe499e893eceb07294b0adf6edd97c8c41798a6 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 13 Jun 2016 13:13:39 +0800 Subject: [PATCH] FIX: Don't include reflections when checking for duplication topic links. --- app/models/topic_link.rb | 3 ++- spec/models/topic_link_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb index b35ec1c76..4ba19ef7b 100644 --- a/app/models/topic_link.rb +++ b/app/models/topic_link.rb @@ -233,7 +233,8 @@ class TopicLink < ActiveRecord::Base results = TopicLink .includes(:post => :user) - .where(topic_id: topic.id).limit(200) + .where(topic_id: topic.id, reflection: false) + .limit(200) lookup = {} results.each do |tl| diff --git a/spec/models/topic_link_spec.rb b/spec/models/topic_link_spec.rb index 487c6852f..5fb86c7fc 100644 --- a/spec/models/topic_link_spec.rb +++ b/spec/models/topic_link_spec.rb @@ -16,6 +16,8 @@ describe TopicLink do topic.user end + let(:post) { Fabricate(:post) } + it "can't link to the same topic" do ftl = TopicLink.new(url: "/t/#{topic.id}", topic_id: topic.id, @@ -320,6 +322,31 @@ http://b.com/#{'a'*500} end end + + describe ".duplicate_lookup" do + let(:user) { Fabricate(:user, username: "junkrat") } + + let(:post_with_internal_link) do + Fabricate(:post, user: user, raw: "Check out this topic #{post.topic.url}/122131") + end + + it "should return the right response" do + TopicLink.extract_from(post_with_internal_link) + + result = TopicLink.duplicate_lookup(post_with_internal_link.topic) + expect(result.count).to eq(1) + + lookup = result["test.localhost/t/#{post.topic.slug}/#{post.topic.id}/122131"] + + expect(lookup[:domain]).to eq("test.localhost") + expect(lookup[:username]).to eq("junkrat") + expect(lookup[:posted_at].to_s).to eq(post_with_internal_link.created_at.to_s) + expect(lookup[:post_number]).to eq(1) + + result = TopicLink.duplicate_lookup(post.topic) + expect(result.count).to eq(0) + end + end end end