From 1c04acec58c81e5a6ce4962deb565673747ebae2 Mon Sep 17 00:00:00 2001 From: Peter Bhat Harkins Date: Wed, 21 Nov 2018 21:06:30 -0600 Subject: [PATCH] fix #similar_stories to not include merged stories --- app/models/story.rb | 6 ++++++ spec/models/story_spec.rb | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/models/story.rb b/app/models/story.rb index 0644fcbf..bb661ed9 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -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 diff --git a/spec/models/story_spec.rb b/spec/models/story_spec.rb index c79b877e..d3b0be16 100644 --- a/spec/models/story_spec.rb +++ b/spec/models/story_spec.rb @@ -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