Skip to content

Commit

Permalink
WIP: Blacklight::PresenterFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
awead committed Jul 5, 2016
1 parent e25d118 commit 2d13d02
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app/presenters/blacklight/presenter_factory.rb
@@ -0,0 +1,37 @@
module Blacklight
class PresenterFactory
attr_reader :config, :action

def initialize(config, action=nil)
@config = config
@action = action
end

def build
case action
when nil
default_presenter_class
when 'show', 'citation'
show_presenter_class
when 'index'
index_presenter_class
else
raise RuntimeError, "Unable to determine presenter type for #{action}"
end
end

private

def default_presenter_class
Blacklight::DocumentPresenter
end

def show_presenter_class
config.show.document_presenter_class
end

def index_presenter_class
config.index.document_presenter_class
end
end
end
38 changes: 38 additions & 0 deletions spec/presenters/presenter_factory_spec.rb
@@ -0,0 +1,38 @@
require 'spec_helper'

describe Blacklight::PresenterFactory do
let(:config) { Blacklight::Configuration.new }
let(:custom_presenter_class) { double }
subject { described_class.new(config, action).build }

context "by default" do
let(:action) { nil }
it { is_expected.to eq(Blacklight::DocumentPresenter) }
end

context "with an index action" do
let(:action) { 'index' }
it { is_expected.to eq(Blacklight::IndexPresenter) }
context "when using the value defined in the blacklight configuration" do
before { config.index.document_presenter_class = custom_presenter_class }
it { is_expected.to eq(custom_presenter_class) }
end
end

context "with a show action" do
let(:action) { 'show' }
it { is_expected.to eq(Blacklight::ShowPresenter) }
context "when using the value defined in the blacklight configuration" do
before { config.show.document_presenter_class = custom_presenter_class }
it { is_expected.to eq(custom_presenter_class) }
end
end

context "with an unrecognized action" do
let(:action) { 'foo' }
it "raises an error" do
expect{ subject }.to raise_error(RuntimeError)
end
end

end

0 comments on commit 2d13d02

Please sign in to comment.