fix #similar_stories to not include merged stories

This commit is contained in:
Peter Bhat Harkins 2018-11-21 21:06:30 -06:00
parent 0724c706fc
commit 1c04acec58
2 changed files with 22 additions and 0 deletions

View File

@ -219,8 +219,14 @@ class Story < ApplicationRecord
return [] unless self.url.present?
@_similar_stories ||= Story.find_similar_by_url(self.url).order("id DESC")
# do not include this story itself or any story merged into it
if self.id?
@_similar_stories = @_similar_stories.where.not(id: self.id)
.where('merged_story_id is null or merged_story_id != ?', self.id)
end
# do not include the story this one is merged into
if self.merged_story_id?
@_similar_stories = @_similar_stories.where('id != ?', self.merged_story_id)
end
@_similar_stories
end

View File

@ -174,4 +174,20 @@ describe Story do
expect(mod_log.action).to match(/title from "blah" to "changed title"/)
expect(mod_log.action).to match(/tags from "tag1 tag2" to "tag1"/)
end
describe "#similar_stories" do
it "finds stories with similar URLs" do
s1 = create(:story, url: 'https://example.com', created_at: (Story::RECENT_DAYS + 1).days.ago)
s2 = create(:story, url: 'https://example.com/')
expect(s1.similar_stories).to eq([s2])
expect(s2.similar_stories).to eq([s1])
end
it "does not include merges" do
s1 = create(:story, url: 'https://example.com', created_at: (Story::RECENT_DAYS + 1).days.ago)
s2 = create(:story, url: 'https://example.com/', merged_story_id: s1.id)
expect(s1.similar_stories).to eq([])
expect(s2.similar_stories).to eq([])
end
end
end