Skip to content

Commit

Permalink
Show merged stories on duplicate submission
Browse files Browse the repository at this point in the history
  • Loading branch information
myhro authored and pushcx committed Dec 27, 2023
1 parent 9a9362f commit a5a41e1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/views/stories/_form_errors.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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 %>
Expand Down
3 changes: 3 additions & 0 deletions app/views/stories/_similar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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) %>
Expand Down
36 changes: 36 additions & 0 deletions spec/requests/stories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,42 @@
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",
Expand Down

0 comments on commit a5a41e1

Please sign in to comment.