Commit b20a09f7 authored by Fatih Acet's avatar Fatih Acet

Merge branch 'add_body_data_elements_for_page_type_id_project_id_and_namespace_id' into 'master'

Add body data elements for pageview context

See merge request gitlab-org/gitlab!18450
parents f575896b cb3f40c9
......@@ -94,6 +94,25 @@ module ApplicationHelper
sanitize(str, tags: %w(a span))
end
def body_data
{
page: body_data_page,
page_type_id: controller.params[:id],
find_file: find_file_path,
group: "#{@group&.path}"
}.merge(project_data)
end
def project_data
return {} unless @project
{
project_id: @project.id,
project: @project.path,
namespace_id: @project.namespace&.id
}
end
def body_data_page
[*controller.controller_path.split('/'), controller.action_name].compact.join(':')
end
......
!!! 5
%html{ lang: I18n.locale, class: page_class }
= render "layouts/head"
%body{ class: "#{user_application_theme} #{@body_class} #{client_class_list}", data: { page: body_data_page, project: "#{@project.path if @project}", group: "#{@group.path if @group}", find_file: find_file_path } }
%body{ class: "#{user_application_theme} #{@body_class} #{client_class_list}", data: body_data }
= render "layouts/init_auto_complete" if @gfm_form
= render "layouts/init_client_detection_flags"
= render 'peek/bar'
......
---
title: Add body data elements for pageview context
merge_request: 18450
author:
type: added
......@@ -235,4 +235,88 @@ describe ApplicationHelper do
end
end
end
describe '#body_data' do
context 'when @project is not set' do
it 'does not include project data in the body data elements' do
expect(helper.body_data).to eq(
{
page: 'application',
page_type_id: nil,
find_file: nil,
group: ''
}
)
end
context 'when @group is set' do
it 'sets group in the body data elements' do
group = create(:group)
assign(:group, group)
expect(helper.body_data).to eq(
{
page: 'application',
page_type_id: nil,
find_file: nil,
group: group.path
}
)
end
end
end
context 'when @project is set' do
it 'includes all possible body data elements and associates the project elements with project' do
project = create(:project)
assign(:project, project)
expect(helper.body_data).to eq(
{
page: 'application',
page_type_id: nil,
find_file: nil,
group: '',
project_id: project.id,
project: project.name,
namespace_id: project.namespace.id
}
)
end
context 'when controller is issues' do
before do
stub_controller_method(:controller_path, 'projects:issues')
end
context 'when params[:id] is present and the issue exsits and action_name is show' do
it 'sets all project and id elements correctly related to the issue' do
issue = create(:issue)
stub_controller_method(:action_name, 'show')
stub_controller_method(:params, { id: issue.id })
assign(:project, issue.project)
expect(helper.body_data).to eq(
{
page: 'projects:issues:show',
page_type_id: issue.id,
find_file: nil,
group: '',
project_id: issue.project.id,
project: issue.project.name,
namespace_id: issue.project.namespace.id
}
)
end
end
end
end
def stub_controller_method(method_name, value)
allow(helper.controller).to receive(method_name).and_return(value)
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe 'layouts/application' do
let(:user) { create(:user) }
before do
allow(view).to receive(:current_application_settings).and_return(Gitlab::CurrentSettings.current_application_settings)
allow(view).to receive(:experiment_enabled?).and_return(false)
allow(view).to receive(:session).and_return({})
allow(view).to receive(:user_signed_in?).and_return(true)
allow(view).to receive(:current_user).and_return(user)
end
context 'body data elements for pageview context' do
let(:body_data) do
{
body_data_page: 'projects:issues:show',
body_data_page_type_id: '1',
body_data_project_id: '2',
body_data_namespace_id: '3'
}
end
before do
allow(view).to receive(:body_data).and_return(body_data)
render
end
it 'includes the body element page' do
expect(rendered).to include('data-page="projects:issues:show"')
end
it 'includes the body element page_type_id' do
expect(rendered).to include('data-page-type-id="1"')
end
it 'includes the body element project_id' do
expect(rendered).to include('data-project-id="2"')
end
it 'includes the body element namespace_id' do
expect(rendered).to include('data-namespace-id="3"')
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