Commit 5e3be9cda0e23e2c62a3e3b08791357ce572d998
1 parent
38ca52f3
Exists in
master
and in
4 other branches
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
Showing
3 changed files
with
51 additions
and
10 deletions
Show diff stats
app/decorators/commit_decorator.rb
@@ -16,13 +16,15 @@ class CommitDecorator < ApplicationDecorator | @@ -16,13 +16,15 @@ class CommitDecorator < ApplicationDecorator | ||
16 | # In case this first line is longer than 80 characters, it is cut off | 16 | # In case this first line is longer than 80 characters, it is cut off |
17 | # after 70 characters and ellipses (`&hellp;`) are appended. | 17 | # after 70 characters and ellipses (`&hellp;`) are appended. |
18 | def title | 18 | def title |
19 | - return no_commit_message if safe_message.blank? | 19 | + title = safe_message |
20 | 20 | ||
21 | - title_end = safe_message.index(/\n/) | ||
22 | - if (!title_end && safe_message.length > 80) || (title_end && title_end > 80) | ||
23 | - safe_message[0..69] << "…".html_safe | 21 | + return no_commit_message if title.blank? |
22 | + | ||
23 | + title_end = title.index(/\n/) | ||
24 | + if (!title_end && title.length > 80) || (title_end && title_end > 80) | ||
25 | + title[0..69] << "…".html_safe | ||
24 | else | 26 | else |
25 | - safe_message.split(/\n/, 2).first | 27 | + title.split(/\n/, 2).first |
26 | end | 28 | end |
27 | end | 29 | end |
28 | 30 | ||
@@ -30,11 +32,13 @@ class CommitDecorator < ApplicationDecorator | @@ -30,11 +32,13 @@ class CommitDecorator < ApplicationDecorator | ||
30 | # | 32 | # |
31 | # cut off, ellipses (`&hellp;`) are prepended to the commit message. | 33 | # cut off, ellipses (`&hellp;`) are prepended to the commit message. |
32 | def description | 34 | def description |
33 | - title_end = safe_message.index(/\n/) | ||
34 | - if (!title_end && safe_message.length > 80) || (title_end && title_end > 80) | ||
35 | - "…".html_safe << safe_message[70..-1] | 35 | + description = safe_message |
36 | + | ||
37 | + title_end = description.index(/\n/) | ||
38 | + if (!title_end && description.length > 80) || (title_end && title_end > 80) | ||
39 | + "…".html_safe << description[70..-1] | ||
36 | else | 40 | else |
37 | - safe_message.split(/\n/, 2)[1].try(:chomp) | 41 | + description.split(/\n/, 2)[1].try(:chomp) |
38 | end | 42 | end |
39 | end | 43 | end |
40 | 44 |
app/models/commit.rb
@@ -0,0 +1,37 @@ | @@ -0,0 +1,37 @@ | ||
1 | +require 'spec_helper' | ||
2 | + | ||
3 | +describe Commit do | ||
4 | + let(:commit) { create(:project).commit } | ||
5 | + | ||
6 | + describe CommitDecorator do | ||
7 | + let(:decorator) { CommitDecorator.new(commit) } | ||
8 | + | ||
9 | + describe '#title' do | ||
10 | + it "returns no_commit_message when safe_message is blank" do | ||
11 | + decorator.stub(:safe_message).and_return('') | ||
12 | + decorator.title.should == "--no commit message" | ||
13 | + end | ||
14 | + | ||
15 | + it "truncates a message without a newline at 70 characters" do | ||
16 | + message = commit.safe_message * 10 | ||
17 | + | ||
18 | + decorator.stub(:safe_message).and_return(message) | ||
19 | + decorator.title.should == "#{message[0..69]}…" | ||
20 | + end | ||
21 | + | ||
22 | + it "truncates a message with a newline before 80 characters at the newline" do | ||
23 | + message = commit.safe_message.split(" ").first | ||
24 | + | ||
25 | + decorator.stub(:safe_message).and_return(message + "\n" + message) | ||
26 | + decorator.title.should == message | ||
27 | + end | ||
28 | + | ||
29 | + it "truncates a message with a newline after 80 characters at 70 characters" do | ||
30 | + message = (commit.safe_message * 10) + "\n" | ||
31 | + | ||
32 | + decorator.stub(:safe_message).and_return(message) | ||
33 | + decorator.title.should == "#{message[0..69]}…" | ||
34 | + end | ||
35 | + end | ||
36 | + end | ||
37 | +end |