Story#description_or_story_cache: handle truncation properly

This commit is contained in:
joshua stein 2015-07-30 18:12:57 -05:00
parent 81b3be6165
commit 09f65774c2
2 changed files with 11 additions and 4 deletions

View file

@ -221,12 +221,19 @@ class Story < ActiveRecord::Base
self.markeddown_description = self.generated_markeddown_description
end
def description_or_story_cache
if self.description.present?
def description_or_story_cache(chars = 0)
s = if self.description.present?
self.markeddown_description.gsub(/<[^>]*>/, "")
else
self.story_cache
end
if chars > 0
# remove last truncated word
s = s.to_s[0, chars].gsub(/ [^ ]*$/, "")
end
HTMLEntities.new.decode(s.to_s)
end
def domain

View file

@ -81,9 +81,9 @@ class="story <%= story.vote && story.vote[:vote] == 1 ? "upvoted" : "" %>
<% if !(defined?(single_story) && single_story) && @user &&
@user.show_story_previews? %>
<% if (sc = story.description_or_story_cache).present? %>
<% if (sc = story.description_or_story_cache(500)).present? %>
<div class="story_content">
<%= break_long_words(sc[0, 500]) %>
<%= break_long_words(sc) %>
</div>
<% end %>
<% end %>