Commit bf783fc4 authored by Peter Leitzen's avatar Peter Leitzen Committed by Mayra Cabrera

Allow subclasses of BasePipeline to resolved

Currently, one can only pass Symbols to `Pipeline.[]` which is
convenient in most cases assuming that the pipeline is namespaced to
`Banzai::Pipeline`.

In some cases, we want a namespace a pipeline to a specific domain
namespace (e.g `StatusPage::PostProcessPipeline`). In those cases we had
to pass a underscored version of the namespaced class name (e.g.
`:'_status_page::_post_process_pipeline'` or
`:'status_page/post_process_pipeline'`).

This commit allows passing a subclass of
`Banzai::Pipeline::BasePipeline` to `Pipeline.[]`.
parent 9bf7a9ba
...@@ -56,7 +56,7 @@ class ResourceLabelEvent < ResourceEvent ...@@ -56,7 +56,7 @@ class ResourceLabelEvent < ResourceEvent
end end
def banzai_render_context(field) def banzai_render_context(field)
super.merge(pipeline: 'label', only_path: true) super.merge(pipeline: :label, only_path: true)
end end
def refresh_invalid_reference def refresh_invalid_reference
......
...@@ -2,9 +2,33 @@ ...@@ -2,9 +2,33 @@
module Banzai module Banzai
module Pipeline module Pipeline
# Resolve a pipeline by name
#
# name - nil, Class or Symbol. The name to be resolved.
#
# Examples:
# Pipeline[nil] # => Banzai::Pipeline::FullPipeline
# Pipeline[:label] # => Banzai::Pipeline::LabelPipeline
# Pipeline[StatusPage::PostProcessPipeline] # => StatusPage::PostProcessPipeline
#
# Pipeline['label'] # => raises ArgumentError - unsupport type
# Pipeline[Project] # => raises ArgumentError - not a subclass of BasePipeline
#
# Returns a pipeline class which is a subclass of Banzai::Pipeline::BasePipeline.
def self.[](name) def self.[](name)
name ||= :full name ||= FullPipeline
const_get("#{name.to_s.camelize}Pipeline", false)
pipeline = case name
when Class
name
when Symbol
const_get("#{name.to_s.camelize}Pipeline", false)
end
return pipeline if pipeline && pipeline < BasePipeline
raise ArgumentError,
"unsupported pipeline name #{name.inspect} (#{name.class})"
end end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
describe Banzai::Pipeline do
describe '.[]' do
subject { described_class[name] }
shared_examples 'error' do |exception, message|
it do
expect { subject }.to raise_error(exception, message)
end
end
context 'for nil' do
let(:name) { nil }
it { is_expected.to eq(Banzai::Pipeline::FullPipeline) }
end
context 'for symbols' do
context 'when known' do
let(:name) { :full }
it { is_expected.to eq(Banzai::Pipeline::FullPipeline) }
end
context 'when unknown' do
let(:name) { :unknown }
it_behaves_like 'error', NameError,
'uninitialized constant Banzai::Pipeline::UnknownPipeline'
end
end
context 'for classes' do
let(:name) { klass }
context 'subclassing Banzai::Pipeline::BasePipeline' do
let(:klass) { Class.new(Banzai::Pipeline::BasePipeline) }
it { is_expected.to eq(klass) }
end
context 'subclassing other types' do
let(:klass) { Class.new(Banzai::RenderContext) }
before do
stub_const('Foo', klass)
end
it_behaves_like 'error', ArgumentError,
'unsupported pipeline name Foo (Class)'
end
end
context 'for other types' do
let(:name) { 'label' }
it_behaves_like 'error', ArgumentError,
'unsupported pipeline name "label" (String)'
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