summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Chao <mchao@gitlab.com>2018-09-06 17:48:57 +0800
committerMark Chao <mchao@gitlab.com>2018-10-30 15:44:55 +0800
commit32f9cf8ce3dd337bf3b1683c5872171c253f0d27 (patch)
treeda6f8077d458283ae1da56cbc9e282c87cdeba0a
parent6580de78bb52323fcafaaf4d2e770590e4f1c586 (diff)
downloadgitlab-ce-32f9cf8ce3dd337bf3b1683c5872171c253f0d27.tar.gz
Add BlobPresenter for highlighting
Force FoundBlob to use BlobPresenter
-rw-r--r--app/models/blob.rb4
-rw-r--r--app/presenters/blob_presenter.rb14
-rw-r--r--lib/gitlab/search_results.rb5
-rw-r--r--spec/presenters/blob_presenter_spec.rb60
4 files changed, 82 insertions, 1 deletions
diff --git a/app/models/blob.rb b/app/models/blob.rb
index e5854415dd2..425ba69c073 100644
--- a/app/models/blob.rb
+++ b/app/models/blob.rb
@@ -1,7 +1,9 @@
# frozen_string_literal: true
-# Blob is a Rails-specific wrapper around Gitlab::Git::Blob objects
+# Blob is a Rails-specific wrapper around Gitlab::Git::Blob, SnippetBlob and Ci::ArtifactBlob
class Blob < SimpleDelegator
+ include Presentable
+
CACHE_TIME = 60 # Cache raw blobs referred to by a (mutable) ref for 1 minute
CACHE_TIME_IMMUTABLE = 3600 # Cache blobs referred to by an immutable reference for 1 hour
diff --git a/app/presenters/blob_presenter.rb b/app/presenters/blob_presenter.rb
new file mode 100644
index 00000000000..2bca413876c
--- /dev/null
+++ b/app/presenters/blob_presenter.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+class BlobPresenter < Gitlab::View::Presenter::Simple
+ presents :blob
+
+ def highlight(plain: nil)
+ Gitlab::Highlight.highlight(
+ blob.path,
+ blob.data,
+ language: blob.language_from_gitattributes,
+ plain: (plain || blob.no_highlighting?)
+ )
+ end
+end
diff --git a/lib/gitlab/search_results.rb b/lib/gitlab/search_results.rb
index f2795c739f5..3dbb0608a4f 100644
--- a/lib/gitlab/search_results.rb
+++ b/lib/gitlab/search_results.rb
@@ -4,6 +4,7 @@ module Gitlab
class SearchResults
class FoundBlob
include EncodingHelper
+ include Presentable
attr_reader :id, :filename, :basename, :ref, :startline, :data, :project_id
@@ -31,6 +32,10 @@ module Gitlab
def language_from_gitattributes
nil
end
+
+ def present
+ super(presenter_class: BlobPresenter)
+ end
end
attr_reader :current_user, :query, :per_page
diff --git a/spec/presenters/blob_presenter_spec.rb b/spec/presenters/blob_presenter_spec.rb
new file mode 100644
index 00000000000..5c83fa39d80
--- /dev/null
+++ b/spec/presenters/blob_presenter_spec.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe BlobPresenter, :seed_helper do
+ let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
+
+ let(:git_blob) do
+ Gitlab::Git::Blob.find(
+ repository,
+ 'fa1b1e6c004a68b7d8763b86455da9e6b23e36d6',
+ 'files/ruby/regex.rb'
+ )
+ end
+ let(:blob) { Blob.new(git_blob) }
+
+ describe '#highlight' do
+ subject { described_class.new(blob) }
+
+ it 'returns highlighted content' do
+ expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: false, language: nil)
+
+ subject.highlight
+ end
+
+ context 'with :plain' do
+ it 'returns plain content when no_highlighting? is true' do
+ allow(blob).to receive(:no_highlighting?).and_return(true)
+
+ subject.highlight
+ end
+
+ it 'returns plain content when :plain is true' do
+ expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: true, language: nil)
+
+ subject.highlight(plain: true)
+ end
+
+ it 'returns plain content when :plain is false, but no_highlighting? is true' do
+ allow(blob).to receive(:no_highlighting?).and_return(true)
+
+ expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: true, language: nil)
+
+ subject.highlight(plain: false)
+ end
+ end
+
+ context 'gitlab-language contains a match' do
+ before do
+ allow(blob).to receive(:language_from_gitattributes).and_return('ruby')
+ end
+
+ it 'passes language to inner call' do
+ expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: false, language: 'ruby')
+
+ subject.highlight
+ end
+ end
+ end
+end