Commit 4cbfe9427ba84a05a444373809a6b7aa7906db34

Authored by Dmitriy Zaporozhets
2 parents dc53a4f7 5e3be9cd

Merge pull request #1613 from tsigo/performance

Improve performance of Commits#show
app/decorators/commit_decorator.rb
... ... @@ -16,13 +16,15 @@ class CommitDecorator < ApplicationDecorator
16 16 # In case this first line is longer than 80 characters, it is cut off
17 17 # after 70 characters and ellipses (`&hellp;`) are appended.
18 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] << "&hellip;".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] << "&hellip;".html_safe
24 26 else
25   - safe_message.split(/\n/, 2).first
  27 + title.split(/\n/, 2).first
26 28 end
27 29 end
28 30  
... ... @@ -30,11 +32,13 @@ class CommitDecorator &lt; ApplicationDecorator
30 32 #
31 33 # cut off, ellipses (`&hellp;`) are prepended to the commit message.
32 34 def description
33   - title_end = safe_message.index(/\n/)
34   - if (!title_end && safe_message.length > 80) || (title_end && title_end > 80)
35   - "&hellip;".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 + "&hellip;".html_safe << description[70..-1]
36 40 else
37   - safe_message.split(/\n/, 2)[1].try(:chomp)
  41 + description.split(/\n/, 2)[1].try(:chomp)
38 42 end
39 43 end
40 44  
... ...
app/models/commit.rb
... ... @@ -107,7 +107,7 @@ class Commit
107 107 end
108 108  
109 109 def safe_message
110   - utf8 message
  110 + @safe_message ||= utf8 message
111 111 end
112 112  
113 113 def created_at
... ...
app/views/commits/_head.html.haml
... ... @@ -9,12 +9,12 @@
9 9 = nav_link(html_options: {class: branches_tab_class}) do
10 10 = link_to project_repository_path(@project) do
11 11 Branches
12   - %span.badge= @project.repo.branch_count
  12 + %span.badge= @project.branches.length
13 13  
14 14 = nav_link(controller: :repositories, action: :tags) do
15 15 = link_to tags_project_repository_path(@project) do
16 16 Tags
17   - %span.badge= @project.repo.tag_count
  17 + %span.badge= @project.tags.length
18 18  
19 19 - if current_controller?(:commits) && current_user.private_token
20 20 %li.right
... ...
spec/models/commit_spec.rb 0 → 100644
... ... @@ -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]}&hellip;"
  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]}&hellip;"
  34 + end
  35 + end
  36 + end
  37 +end
... ...