Skip to content

Commit

Permalink
Merge c728ea4 into 92987c4
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Feb 7, 2014
2 parents 92987c4 + c728ea4 commit 5d33c55
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
23 changes: 21 additions & 2 deletions lib/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,30 @@ def index
format.html { }
format.rss { render :layout => false }
format.atom { render :layout => false }


format.json do
render json: render_search_results_as_json
end

additional_response_formats(format)
end
end

def additional_response_formats format
blacklight_config.index.respond_to.each do |key, config|
format.send key do
case config
when false
raise ActionController::RoutingError.new('Not Found')
when Hash
render config
when Proc
instance_exec &config
when Symbol, String
send config
else
# no-op, just render the page
end
end
end
end

Expand Down
7 changes: 6 additions & 1 deletion lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ def default_values
:document_solr_request_handler => nil,
:default_document_solr_params => {},
:show => ViewConfig::Show.new(:partials => [:show_header, :show]),
:index => ViewConfig::Index.new(:partials => [:index_header, :thumbnail, :index], :title_field => unique_key, :display_type_field => 'format', :group => false),
:index => ViewConfig::Index.new(:partials => [:index_header, :thumbnail, :index],
:title_field => unique_key,
:display_type_field => 'format',
:group => false,
:respond_to => OpenStructWithHashAccess.new()
),
:view => NestedOpenStructWithHashAccess.new(ViewConfig, 'list'),
:spell_max => 5,
:max_per_page => 100,
Expand Down
43 changes: 43 additions & 0 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,49 @@ def assigns_response
end
end

describe "with additional formats from configuration" do
let(:blacklight_config) { Blacklight::Configuration.new }

before :each do
@controller.stub blacklight_config: blacklight_config
@controller.stub get_search_results: [double, double]
end

it "should not render when the config is false" do
blacklight_config.index.respond_to.yaml = false
expect { get :index, format: 'yaml' }.to raise_error ActionController::RoutingError
end

it "should render the default when the config is true" do
# TODO: this should really stub a template and see if it gets rendered,
# but how to do that is non-obvious..
blacklight_config.index.respond_to.yaml = true
expect { get :index, format: 'yaml' }.to raise_error ActionView::MissingTemplate
end

it "should pass a hash to the render call" do
blacklight_config.index.respond_to.yaml = { nothing: true, layout: false }
get :index, format: 'yaml'
expect(response.body).to be_blank
end

it "should evaluate a proc" do
blacklight_config.index.respond_to.yaml = lambda { render text: "" }
get :index, format: 'yaml'
expect(response.body).to be_empty
end

it "with a symbol, it should call a controller method" do
subject.should_receive(:render_some_yaml) do
subject.render nothing: true, layout: false
end

blacklight_config.index.respond_to.yaml = :render_some_yaml
get :index, format: 'yaml'
expect(response.body).to be_blank
end
end

end # describe index action

describe "update action" do
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/blacklight/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@
end
end

describe "config.index.respond_to" do
it "should have a list of additional formats for index requests to respond to" do
@config.index.respond_to.xml = true

@config.index.respond_to.csv = { :layout => false }

@config.index.respond_to.yaml = lambda { render text: "" }

expect(@config.index.respond_to.keys).to eq [:xml, :csv, :yaml]
end
end

describe "spell_max" do
it "should default to 5" do
expect(Blacklight::Configuration.new.spell_max).to eq 5
Expand Down

0 comments on commit 5d33c55

Please sign in to comment.