Commit 7e1686d8 authored by Athar Hameed's avatar Athar Hameed Committed by Bob Van Landuyt

Show number of files in snippets list

parent 8ccc6836
......@@ -19,6 +19,7 @@ class Dashboard::SnippetsController < Dashboard::ApplicationController
.page(params[:page])
.inc_author
.inc_projects_namespace_route
.inc_statistics
return if redirect_out_of_range(@snippets)
......
......@@ -11,6 +11,7 @@ class Explore::SnippetsController < Explore::ApplicationController
.page(params[:page])
.without_count
.inc_author
.inc_statistics
@noteable_meta_data = noteable_meta_data(@snippets, 'Snippet')
end
......
......@@ -22,6 +22,7 @@ class Projects::SnippetsController < Projects::Snippets::ApplicationController
.execute
.page(params[:page])
.inc_author
.inc_statistics
return if redirect_out_of_range(@snippets)
......
......@@ -24,6 +24,7 @@ class SnippetsController < Snippets::ApplicationController
.execute
.page(params[:page])
.inc_author
.inc_statistics
return if redirect_out_of_range(@snippets)
......
......@@ -68,4 +68,18 @@ module SnippetsHelper
title: 'Download',
rel: 'noopener noreferrer')
end
def snippet_file_count(snippet)
file_count = snippet.statistics&.file_count
return unless file_count&.nonzero?
tooltip = n_('%d file', '%d files', file_count) % file_count
tag.span(class: 'file_count', title: tooltip, data: { toggle: 'tooltip', container: 'body' }) do
concat(sprite_icon('documents', css_class: 'gl-vertical-align-middle'))
concat(' ')
concat(file_count)
end
end
end
......@@ -82,6 +82,7 @@ class Snippet < ApplicationRecord
scope :fresh, -> { order("created_at DESC") }
scope :inc_author, -> { includes(:author) }
scope :inc_relations_for_view, -> { includes(author: :status) }
scope :inc_statistics, -> { includes(:statistics) }
scope :with_statistics, -> { joins(:statistics) }
scope :inc_projects_namespace_route, -> { includes(project: [:route, :namespace]) }
......
......@@ -9,6 +9,8 @@
= snippet.title
%ul.controls
%li
= snippet_file_count(snippet)
%li
= link_to gitlab_snippet_path(snippet, anchor: 'notes'), class: ('no-comments' if notes_count == 0) do
= sprite_icon('comments', css_class: 'gl-vertical-align-text-bottom')
......
---
title: Show number of files in snippet lists
merge_request: 55452
author:
type: changed
......@@ -203,6 +203,11 @@ msgid_plural "%d failed security jobs"
msgstr[0] ""
msgstr[1] ""
msgid "%d file"
msgid_plural "%d files"
msgstr[0] ""
msgstr[1] ""
msgid "%d fixed test result"
msgid_plural "%d fixed test results"
msgstr[0] ""
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'shared/snippets/_snippet.html.haml' do
let_it_be(:snippet) { create(:snippet) }
before do
allow(view).to receive(:current_application_settings).and_return(Gitlab::CurrentSettings.current_application_settings)
allow(view).to receive(:can?) { true }
@noteable_meta_data = Class.new { include Gitlab::NoteableMetadata }.new.noteable_meta_data([snippet], 'Snippet')
end
context 'snippet with statistics' do
it 'renders correct file count and tooltip' do
snippet.statistics.file_count = 3
render 'shared/snippets/snippet', snippet: snippet
expect(rendered).to have_selector("span.file_count", text: '3')
expect(rendered).to have_selector("span.file_count[title=\"3 files\"]")
end
it 'renders correct file count and tooltip when file_count is 1' do
snippet.statistics.file_count = 1
render 'shared/snippets/snippet', snippet: snippet
expect(rendered).to have_selector("span.file_count", text: '1')
expect(rendered).to have_selector("span.file_count[title=\"1 file\"]")
end
it 'does not render file count when file count is 0' do
snippet.statistics.file_count = 0
render 'shared/snippets/snippet', snippet: snippet
expect(rendered).not_to have_selector('span.file_count')
end
end
context 'snippet without statistics' do
it 'does not render file count if statistics are not present' do
snippet.statistics = nil
render 'shared/snippets/snippet', snippet: snippet
expect(rendered).not_to have_selector('span.file_count')
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment