Commit 9df241b3 authored by Vasilii Iakliushin's avatar Vasilii Iakliushin Committed by James Lopez

Provide payload to initialize StaticSiteEditor

Contributes to https://gitlab.com/gitlab-org/gitlab/-/issues/212560

* Add `Gitlab::StaticSiteEditor::Config` entity to generate payload
* Populate `data` attribute with required attributes
parent 602431a6
# frozen_string_literal: true
class Projects::StaticSiteEditorController < Projects::ApplicationController
include ExtractsPath
layout 'fullscreen'
prepend_before_action :authenticate_user!, only: [:show]
before_action :assign_ref_and_path, only: [:show]
def show
@config = Gitlab::StaticSiteEditor::Config.new(@repository, @ref, @path, params[:return_url])
end
private
def assign_ref_and_path
@ref, @path = extract_ref(params[:id])
render_404 if @ref.blank? || @path.blank?
end
end
#static-site-editor{ data: { project_id: '8', path: 'README.md' } }
#static-site-editor{ data: @config.payload }
---
title: Provide configuration options for Static Site Editor
merge_request: 29058
author:
type: added
# frozen_string_literal: true
module Gitlab
module StaticSiteEditor
class Config
def initialize(repository, ref, file_path, return_url)
@repository = repository
@ref = ref
@file_path = file_path
@return_url = return_url
end
def payload
{
branch: ref,
path: file_path,
commit: commit.id,
project_id: project.id,
project: project.path,
namespace: project.namespace.path,
return_url: return_url
}
end
private
attr_reader :repository, :ref, :file_path, :return_url
delegate :project, to: :repository
def commit
repository.commit(ref)
end
end
end
end
......@@ -10,7 +10,8 @@ describe Projects::StaticSiteEditorController do
{
namespace_id: project.namespace,
project_id: project,
id: 'master/README.md'
id: 'master/README.md',
return_url: 'http://example.com'
}
end
......@@ -38,6 +39,18 @@ describe Projects::StaticSiteEditorController do
it 'renders the edit page' do
expect(response).to render_template(:show)
end
it 'assigns a config variable' do
expect(assigns(:config)).to be_a(Gitlab::StaticSiteEditor::Config)
end
context 'when combination of ref and file path is incorrect' do
let(:default_params) { super().merge(id: 'unknown') }
it 'responds with 404 page' do
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::StaticSiteEditor::Config do
subject(:config) { described_class.new(repository, ref, file_path, return_url) }
let(:project) { create(:project, :public, :repository, name: 'project', namespace: namespace) }
let(:namespace) { create(:namespace, name: 'namespace') }
let(:repository) { project.repository }
let(:ref) { 'master' }
let(:file_path) { 'README.md' }
let(:return_url) { 'http://example.com' }
describe '#payload' do
subject { config.payload }
it 'returns data for the frontend component' do
is_expected.to eq(
branch: 'master',
commit: repository.commit.id,
namespace: 'namespace',
path: 'README.md',
project: 'project',
project_id: project.id,
return_url: 'http://example.com'
)
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