Allow FoundBlob to access language from gitattributes
Extract language_from_git_attributes as a concern so it can ben included in two blob classes.
This commit is contained in:
parent
bc14e4ed10
commit
a4ba973e24
7 changed files with 46 additions and 31 deletions
|
@ -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
|
||||
|
|
13
app/models/concerns/blob_language_from_git_attributes.rb
Normal file
13
app/models/concerns/blob_language_from_git_attributes.rb
Normal file
|
@ -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
|
|
@ -82,7 +82,7 @@ module Gitlab
|
|||
ref: ref,
|
||||
startline: startline,
|
||||
data: data.join,
|
||||
project_id: project ? project.id : nil
|
||||
project: project
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
Loading…
Reference in a new issue