Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |