Show merged stories on duplicate submission

This commit is contained in:
Tiago Ilieve 2023-12-27 01:37:01 -03:00 committed by Peter Bhat Harkins
parent 9a9362f3bb
commit a5a41e11f1
3 changed files with 41 additions and 2 deletions

View File

@ -2,7 +2,7 @@
<div class="form_errors_header">
<% if story.errors.any? %>
<%= errors_for story %>
<% elsif !story.errors.any? && story.public_similar_stories(@user).any? %>
<% elsif !story.errors.any? && story.similar_stories.any? %>
<div class="flash-notice">
<h2>Note: This story was already submitted <%= time_ago_in_words_label(story.most_recent_similar.created_at) %></h2>
<p>
@ -20,7 +20,7 @@
<% end %>
<% end %>
<% if story.public_similar_stories(@user).any? %>
<% if story.similar_stories.any? %>
<p>Previous discussions for this story:</p>
<%= render partial: "stories/similar", locals: { similar: story.similar_stories } %>
<% end %>

View File

@ -2,6 +2,9 @@
<% similar.each do |story| %>
<li>
<a href="<%= story.url %>" target="_blank"><%= story.title %></a>
<% if story.merged_story_id %>
(merged into <a href="<%= story.merged_into_story.url %>" target="_blank"><%= story.merged_into_story.title %></a>)
<% end %>
<%= story.user_is_author? ? "authored by" : "via" %>
<%= styled_user_link story.user, story %>
<%= time_ago_in_words_label(story.created_at) %>

View File

@ -10,6 +10,42 @@ describe "stories", type: :request do
describe "#check_url_dupe" do
before { sign_in user }
context "html" do
it "returns an error when story URL is missing" do
expect {
post "/stories/check_url_dupe.html", params: {story: {url: ""}}
}.to raise_error(ActionController::ParameterMissing)
end
it "returns previous discussions for an existing story" do
post "/stories/check_url_dupe.html", params: {story: {url: story.url}}
expect(response).to be_successful
expect(response.body).to include("Previous discussions for this story")
expect(response.body).to include(story.title)
expect(response.body).to include(story.url)
expect(response.body).not_to include("merged into")
end
it "returns a story merged into an existing one" do
merged_story = create(:story, merged_story_id: story.id)
post "/stories/check_url_dupe.html", params: {story: {url: merged_story.url}}
expect(response).to be_successful
expect(response.body).to include("Previous discussions for this story")
expect(response.body).to include(merged_story.title)
expect(response.body).to include(merged_story.url)
expect(response.body).to include("merged into")
expect(response.body).to include(story.title)
expect(response.body).to include(story.url)
end
end
context "json" do
it "returns similar story matching URL" do
post "/stories/check_url_dupe.json",