BigW Consortium Gitlab

Commit e5a29b45 by Rémy Coutable

Improve presenter factory

Signed-off-by: 's avatarRémy Coutable <remy@rymai.me>
parent bf789ff5
module Gitlab module Gitlab
module View module View
class PresenterFactory module Presenter
def initialize(subject, user: nil) class Factory
@subject = subject def initialize(subject, **attributes)
@user = user @subject = subject
end @attributes = attributes
end
def fabricate!
presenter =
if presenter_class.ancestors.include?(SimpleDelegator)
delegator_presenter
else
simple_presenter
end
presenter
.with_subject(subject)
.with_user(user)
end
private def fabricate!
presenter_class.new(@subject, @attributes)
end
attr_reader :subject, :user private
def presenter_class
"#{subject.class.name.demodulize}Presenter".constantize
end
def delegator_presenter
presenter_class.new(subject)
end
def simple_presenter def presenter_class
presenter_class.new @subject.class.const_get('Presenter')
end
end end
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::View::PresenterFactory do describe Gitlab::View::Presenter::Factory do
let(:appearance) { build(:appearance) } let(:variable) { create(:ci_variable) }
let(:broadcast_message) { build(:broadcast_message) }
before do
class AppearancePresenter
include Gitlab::View::Presenter
end
class BroadcastMessagePresenter < SimpleDelegator
include Gitlab::View::Presenter
end
end
describe '#initialize' do describe '#initialize' do
subject do context 'without optional parameters' do
described_class.new(appearance)
end
it 'takes a subject and optional params' do
expect { subject }.not_to raise_error
end
end
describe '#fabricate!' do
context 'without delegation' do
subject do subject do
described_class.new(appearance).fabricate! described_class.new(variable)
end end
it 'does not forward missing methods to subject' do it 'takes a subject and optional params' do
expect { subject.title }.to raise_error(NoMethodError) expect { subject }.not_to raise_error
end end
end end
context 'with delegation' do context 'with optional parameters' do
subject do subject do
described_class.new(broadcast_message).fabricate! described_class.new(variable, user: 'user')
end end
it 'forwards missing methods to subject' do it 'takes a subject and optional params' do
expect(subject.message).to eq(broadcast_message.message) expect { subject }.not_to raise_error
end end
end end
end end
describe '#fabricate!' do
subject do
described_class.new(variable, user: 'user', foo: 'bar').fabricate!
end
it 'exposes given params' do
expect(subject.user).to eq('user')
expect(subject.foo).to eq('bar')
end
it 'detects the presenter based on the given subject' do
expect(subject).to be_a(Ci::Variable::Presenter)
end
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