Cache the value of safe_message

Also, just for extra paranoia, only call safe_message once in the
decorator methods

Adds specs to make sure it still works
This commit is contained in:
Robert Speicher 2012-10-02 18:57:13 -04:00
parent 38ca52f33b
commit 5e3be9cda0
3 changed files with 51 additions and 10 deletions

View file

@ -16,13 +16,15 @@ class CommitDecorator < ApplicationDecorator
# In case this first line is longer than 80 characters, it is cut off
# after 70 characters and ellipses (`&hellp;`) are appended.
def title
return no_commit_message if safe_message.blank?
title = safe_message
title_end = safe_message.index(/\n/)
if (!title_end && safe_message.length > 80) || (title_end && title_end > 80)
safe_message[0..69] << "&hellip;".html_safe
return no_commit_message if title.blank?
title_end = title.index(/\n/)
if (!title_end && title.length > 80) || (title_end && title_end > 80)
title[0..69] << "&hellip;".html_safe
else
safe_message.split(/\n/, 2).first
title.split(/\n/, 2).first
end
end
@ -30,11 +32,13 @@ class CommitDecorator < ApplicationDecorator
#
# cut off, ellipses (`&hellp;`) are prepended to the commit message.
def description
title_end = safe_message.index(/\n/)
if (!title_end && safe_message.length > 80) || (title_end && title_end > 80)
"&hellip;".html_safe << safe_message[70..-1]
description = safe_message
title_end = description.index(/\n/)
if (!title_end && description.length > 80) || (title_end && title_end > 80)
"&hellip;".html_safe << description[70..-1]
else
safe_message.split(/\n/, 2)[1].try(:chomp)
description.split(/\n/, 2)[1].try(:chomp)
end
end

View file

@ -107,7 +107,7 @@ class Commit
end
def safe_message
utf8 message
@safe_message ||= utf8 message
end
def created_at