Skip to content
Snippets Groups Projects
Commit 9a3e7751 authored by Baodong Cao's avatar Baodong Cao
Browse files

Add render_ee and override render_ce

Use `render_ee` and `render_ce` to achieve
layer-by-layer expansion of views:
1.  jh/app/views/layouts/_page.html.haml
2.  ee/app/views/layouts/_page.html.haml
3.  app/views/layouts/_page.html.haml

See: https://gitlab.com/gitlab-jh/gitlab/-/merge_requests/52
parent 6deeded0
No related merge requests found
......@@ -3,6 +3,7 @@
module JH
module ApplicationHelper
extend ActiveSupport::Concern
extend ::Gitlab::Utils::Override
class_methods do
extend ::Gitlab::Utils::Override
......@@ -12,5 +13,43 @@ def promo_host
'about.gitlab.cn'
end
end
def render_ee(partial, locals = {})
render template: find_ee_template(partial), locals: locals
end
# rubocop: disable CodeReuse/ActiveRecord
def find_ee_template(name)
prefixes = [] # So don't create extra [] garbage
if ee_lookup_context.exists?(name, prefixes, true)
ee_lookup_context.find(name, prefixes, true)
else
ee_lookup_context.find(name, prefixes, false)
end
end
# rubocop: enable CodeReuse/ActiveRecord
def ee_lookup_context
@ee_lookup_context ||= fetch_lookup_context(::Gitlab.extensions.last)
end
override :ce_lookup_context
def ce_lookup_context
@ce_lookup_context ||= fetch_lookup_context(::Gitlab.extensions.reverse)
end
private
def fetch_lookup_context(folders)
raise 'folders should not be blank' if folders.blank?
folder_paths = Array(folders).map { |folder| "#{Rails.root}/#{folder}" }
view_paths = lookup_context.view_paths.paths.reject do |resolver|
resolver.to_path.start_with?(*folder_paths)
end
ActionView::LookupContext.new(view_paths)
end
end
end
= render_ee("layouts/page")
......@@ -8,4 +8,27 @@
expect(helper.promo_host).to eq('about.gitlab.cn')
end
end
context 'when both JH and EE and CE has partials with the same name' do
let(:partial) { 'layouts/page' }
let(:ce_partial_path) { 'app/views/layouts/_page.html.haml' }
let(:jh_partial_path) { "jh/#{ce_partial_path}" }
describe 'find template' do
it 'finds the JH template' do
template = helper.lookup_context.find(partial, [], true)
expect(template.short_identifier).to eq(jh_partial_path)
end
it 'finds the CE template' do
template = helper.find_ce_template(partial)
expect(template.short_identifier).to eq(ce_partial_path)
end
it 'finds the nonexistent EE template' do
template = helper.find_ee_template(partial)
expect(template.short_identifier).to eq(ce_partial_path)
end
end
end
end
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