summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Chao <mchao@gitlab.com>2018-10-04 17:16:51 +0800
committerMark Chao <mchao@gitlab.com>2018-10-30 15:44:55 +0800
commita4ba973e24ef6767d635c0291c9b6ce8085aef28 (patch)
tree28efdf13b834db6267fd8bd64d798d8e694b0109
parentbc14e4ed1024efa1e0a411bd59e1339fb1af20c0 (diff)
downloadgitlab-ce-a4ba973e24ef6767d635c0291c9b6ce8085aef28.tar.gz
Allow FoundBlob to access language from gitattributes
Extract language_from_git_attributes as a concern so it can ben included in two blob classes.
-rw-r--r--app/models/blob.rb8
-rw-r--r--app/models/concerns/blob_language_from_git_attributes.rb13
-rw-r--r--lib/gitlab/project_search_results.rb2
-rw-r--r--lib/gitlab/search_results.rb11
-rw-r--r--spec/features/search/user_searches_for_wiki_pages_spec.rb2
-rw-r--r--spec/models/blob_spec.rb16
-rw-r--r--spec/models/concerns/blob_language_from_git_attributes_spec.rb25
7 files changed, 46 insertions, 31 deletions
diff --git a/app/models/blob.rb b/app/models/blob.rb
index 57c813e3775..4f310e70f4f 100644
--- a/app/models/blob.rb
+++ b/app/models/blob.rb
@@ -3,6 +3,7 @@
# Blob is a Rails-specific wrapper around Gitlab::Git::Blob, SnippetBlob and Ci::ArtifactBlob
class Blob < SimpleDelegator
include Presentable
+ include BlobLanguageFromGitAttributes
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
@@ -180,13 +181,6 @@ class Blob < SimpleDelegator
Gitlab::FileDetector.type_of(path) || Gitlab::FileDetector.type_of(name)
end
- def language_from_gitattributes
- return nil unless project
-
- repository = project.repository
- repository.gitattribute(path, 'gitlab-language')
- end
-
def video?
UploaderHelper::VIDEO_EXT.include?(extension)
end
diff --git a/app/models/concerns/blob_language_from_git_attributes.rb b/app/models/concerns/blob_language_from_git_attributes.rb
new file mode 100644
index 00000000000..70213d22147
--- /dev/null
+++ b/app/models/concerns/blob_language_from_git_attributes.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+# Applicable for blob classes with project attribute
+module BlobLanguageFromGitAttributes
+ extend ActiveSupport::Concern
+
+ def language_from_gitattributes
+ return nil unless project
+
+ repository = project.repository
+ repository.gitattribute(path, 'gitlab-language')
+ end
+end
diff --git a/lib/gitlab/project_search_results.rb b/lib/gitlab/project_search_results.rb
index 3a202d915e3..04df881bf03 100644
--- a/lib/gitlab/project_search_results.rb
+++ b/lib/gitlab/project_search_results.rb
@@ -82,7 +82,7 @@ module Gitlab
ref: ref,
startline: startline,
data: data.join,
- project_id: project ? project.id : nil
+ project: project
)
end
diff --git a/lib/gitlab/search_results.rb b/lib/gitlab/search_results.rb
index 6c86ad11385..b46c18c4364 100644
--- a/lib/gitlab/search_results.rb
+++ b/lib/gitlab/search_results.rb
@@ -5,8 +5,9 @@ module Gitlab
class FoundBlob
include EncodingHelper
include Presentable
+ include BlobLanguageFromGitAttributes
- attr_reader :id, :filename, :basename, :ref, :startline, :data, :project_id
+ attr_reader :id, :filename, :basename, :ref, :startline, :data, :project
def initialize(opts = {})
@id = opts.fetch(:id, nil)
@@ -16,17 +17,15 @@ module Gitlab
@startline = opts.fetch(:startline, nil)
@data = encode_utf8(opts.fetch(:data, nil))
@per_page = opts.fetch(:per_page, 20)
- @project_id = opts.fetch(:project_id, nil)
+ @project = opts.fetch(:project, nil)
end
def path
filename
end
- # Since search results often contain many items,
- # not triggering lookup can avoid n+1 queries.
- def language_from_gitattributes
- nil
+ def project_id
+ @project&.id
end
def present
diff --git a/spec/features/search/user_searches_for_wiki_pages_spec.rb b/spec/features/search/user_searches_for_wiki_pages_spec.rb
index 3ee753b7d23..7225ca65492 100644
--- a/spec/features/search/user_searches_for_wiki_pages_spec.rb
+++ b/spec/features/search/user_searches_for_wiki_pages_spec.rb
@@ -2,7 +2,7 @@ require 'spec_helper'
describe 'User searches for wiki pages', :js do
let(:user) { create(:user) }
- let(:project) { create(:project, :wiki_repo, namespace: user.namespace) }
+ let(:project) { create(:project, :repository, :wiki_repo, namespace: user.namespace) }
let!(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: { title: 'test_wiki', content: 'Some Wiki content' }) }
before do
diff --git a/spec/models/blob_spec.rb b/spec/models/blob_spec.rb
index da3b29dffb6..81e35e6c931 100644
--- a/spec/models/blob_spec.rb
+++ b/spec/models/blob_spec.rb
@@ -224,22 +224,6 @@ describe Blob do
end
end
- describe '#language_from_gitattributes' do
- subject(:blob) { fake_blob(path: 'file.md') }
-
- it 'returns return value from gitattribute' do
- expect(blob.project.repository).to receive(:gitattribute).with(blob.path, 'gitlab-language').and_return('erb?parent=json')
-
- expect(blob.language_from_gitattributes).to eq('erb?parent=json')
- end
-
- it 'returns nil if project is absent' do
- allow(blob).to receive(:project).and_return(nil)
-
- expect(blob.language_from_gitattributes).to eq(nil)
- end
- end
-
describe '#simple_viewer' do
context 'when the blob is empty' do
it 'returns an empty viewer' do
diff --git a/spec/models/concerns/blob_language_from_git_attributes_spec.rb b/spec/models/concerns/blob_language_from_git_attributes_spec.rb
new file mode 100644
index 00000000000..7f05073b08e
--- /dev/null
+++ b/spec/models/concerns/blob_language_from_git_attributes_spec.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe BlobLanguageFromGitAttributes do
+ include FakeBlobHelpers
+
+ let(:project) { build(:project, :repository) }
+
+ describe '#language_from_gitattributes' do
+ subject(:blob) { fake_blob(path: 'file.md') }
+
+ it 'returns return value from gitattribute' do
+ expect(blob.project.repository).to receive(:gitattribute).with(blob.path, 'gitlab-language').and_return('erb?parent=json')
+
+ expect(blob.language_from_gitattributes).to eq('erb?parent=json')
+ end
+
+ it 'returns nil if project is absent' do
+ allow(blob).to receive(:project).and_return(nil)
+
+ expect(blob.language_from_gitattributes).to eq(nil)
+ end
+ end
+end