tweaks to display of similar stories

This commit is contained in:
Peter Bhat Harkins 2018-11-07 11:42:40 -06:00
parent 5eb0a40aee
commit 7498867255
7 changed files with 51 additions and 66 deletions

View File

@ -18,7 +18,7 @@ class StoriesController < ApplicationController
@story = Story.new(story_params)
@story.user_id = @user.id
if @story.valid? && !(@story.is_duplicate? && !@story.seen_previous)
if @story.valid? && !(@story.is_similar? && !@story.seen_previous)
if @story.save
ReadRibbon.where(user: @user, story: @story).first_or_create
return redirect_to @story.comments_path
@ -136,8 +136,6 @@ class StoriesController < ApplicationController
@title = @story.title
@short_url = @story.short_id_url
@similar = Story.find_similar_by_url(@story.url)
.where("short_id != ?", @story.short_id)
respond_to do |format|
format.html {

View File

@ -131,9 +131,8 @@ class Story < ApplicationRecord
# Dingbats, emoji, and other graphics https://www.unicode.org/charts/
GRAPHICS_RE = /[\u{0000}-\u{001F}\u{2190}-\u{27BF}\u{1F000}-\u{1F9FF}]/.freeze
attr_accessor :editing_from_suggestions, :editor,
:fetching_ip, :is_hidden_by_cur_user, :is_saved_by_cur_user,
:moderation_reason, :previewing, :seen_previous, :vote
attr_accessor :editing_from_suggestions, :editor, :fetching_ip, :is_hidden_by_cur_user,
:is_saved_by_cur_user, :moderation_reason, :previewing, :seen_previous, :vote
attr_writer :fetched_content
before_validation :assign_short_id_and_upvote, :on => :create
@ -168,13 +167,10 @@ class Story < ApplicationRecord
end
def check_already_posted
return unless self.url.present? && self.new_record?\
return unless self.url.present? && self.new_record?
return unless self.is_duplicate?
if self.get_duplicates.first.is_recent?
errors.add(:url, "has already been submitted within the past " <<
"#{RECENT_DAYS} days")
if self.is_similar? && self.most_recent_similar.is_recent?
errors.add(:url, "has already been submitted within the past #{RECENT_DAYS} days")
end
end
@ -186,14 +182,6 @@ class Story < ApplicationRecord
end
end
def is_duplicate?
return Story.find_similar_by_url(self.url).any?
end
def get_duplicates
return Story.find_similar_by_url(self.url).order("id DESC")
end
# returns a story or nil
def self.find_similar_by_url(url)
urls = [url.to_s]
@ -227,6 +215,24 @@ class Story < ApplicationRecord
.where("is_expired = ? OR is_moderated = ?", false, true)
end
def similar_stories
return [] unless self.url.present?
@_similar_stories ||= Story.find_similar_by_url(self.url).order("id DESC")
if self.id?
@_similar_stories = @_similar_stories.where.not(id: self.id)
end
@_similar_stories
end
def is_similar?
similar_stories.any?
end
def most_recent_similar
similar_stories.first
end
def self.recalculate_all_hotnesses!
# do the front page first, since find_each can't take an order
Story.order("id DESC").limit(100).each(&:recalculate_hotness!)

View File

@ -1,26 +1,17 @@
<div>
<div class="form_errors_header">
<% if story.errors.count == 1 && story.is_duplicate? %>
<% if story.errors.count == 1 && story.is_similar? %>
<div class="flash-error">
<h2>Error: This story was submitted <%=
time_ago_in_words_label(story.get_duplicates.first.created_at) %></h2>
<p>
Please view the previous discussions for this story. <br>
</p>
<h2>Error: This story was submitted <%= time_ago_in_words_label(story.most_recent_similar.created_at) %></h2>
</div>
<% elsif story.errors.any? && !story.is_duplicate?%>
<% elsif story.errors.any? && !story.is_similar? %>
<%= error_messages_for story %>
<% elsif !story.errors.any? && story.is_duplicate? %>
<% elsif !story.errors.any? && story.is_similar? %>
<div class="flash-notice">
<h2>Note: This story was already submitted <%=
time_ago_in_words_label(story.get_duplicates.first.created_at) %>, but
may be submitted again.</h2>
<p>
Please view the previous discussions for this story.
<br>
<h2>Note: This story was already submitted <%= time_ago_in_words_label(story.most_recent_similar.created_at) %>, but may be submitted again.</h2>
<p>
If the content has changed or warrants new discussion, you may submit it again.
</p>
</p>
</div>
<% if defined?(f) %>
@ -32,8 +23,9 @@
<% end %>
<% end %>
</div>
<% if story.is_duplicate? %>
<%= render :partial => "stories/similar", :locals => { :similar => story.get_duplicates } %>
<br>
<% if story.is_similar? %>
<p>Previous discussions for this story:</p>
<%= render partial: "stories/similar", locals: { similar: story.similar_stories } %>
<% end %>
</div>
</div>

View File

@ -170,8 +170,7 @@ class="story <%= story.vote && story.vote[:vote] == 1 ? "upvoted" : "" %>
<% if story.comments_count == 0 %>
no comments
<% else %>
<%= story.comments_count %>
comment<%= story.comments_count == 1 ? "" : "s" %>
<%= story.comments_count %> <%= 'comment'.pluralize(story.comments_count) %></a>
<% end %>
</a>
</span>

View File

@ -1,28 +1,23 @@
<ol>
<% similar.each do |story| %>
<li>
<a href="<%= story.url %>" target="_blank">
<%= story.title %></a>
<a href="<%= story.url %>" target="_blank"><%= story.title %></a>
<%= story.user_is_author? ? "authored by" : "via" %>
<a href="/u/<%= story.user.username %>" class="<%= story.html_class_for_user %>"><%= story.user.username %></a>
<%= time_ago_in_words_label(story.created_at) %>
<span>
|
<%= story.score %> <%= 'point'.pluralize(story.score) %>
</span>
<span class="comments_label">
|
<a href="<%= story.comments_path %>">
<% if story.comments_count == 0 %>
no comments</a>
<% else %>
<%= story.comments_count %>
comment<%= story.comments_count == 1 ? "" : "s"%></a>
<%= story.comments_count %> <%= 'comment'.pluralize(story.comments_count) %></a>
<% end %>
</span>
<span>
|
<%= story.upvotes - story.downvotes %>
point<%= story.comments_count == 1 ? "" : "s" %>
</span>
-
<span>
<em>published on </em><%=story.created_at.strftime("%m/%d/%Y") %>
<em> at </em> <%= story.created_at.strftime("%I:%M%p") %></em>
</span>
</li>
<% end %>
</ol>
</ol>

View File

@ -5,7 +5,6 @@
<div id="story_holder">
<%= form_for @story do |f| %>
<%= render :partial => "stories/form", :locals => { :story => @story, :f => f } %>
<p></p>

View File

@ -77,13 +77,9 @@
</ol>
<% end %>
<% if @similar.any? %>
<div>
<p>
<h4>Discussions from the same URL:</h4>
<% if @similar && @similar.length > 0 %>
<%= render :partial => "stories/similar", :locals => { :similar => @similar } %>
<% end %>
</p>
<% if !@story.previewing && @story.similar_stories.any? %>
<div class="box wide">
<h4>Stories with similar links:</h4>
<%= render partial: "stories/similar", locals: { similar: @story.similar_stories } %>
</div>
<% end %>