diff --git a/lib/blacklight/catalog.rb b/lib/blacklight/catalog.rb index 13b910d426..e4e8ce1bec 100644 --- a/lib/blacklight/catalog.rb +++ b/lib/blacklight/catalog.rb @@ -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 diff --git a/lib/blacklight/configuration.rb b/lib/blacklight/configuration.rb index 06389613b7..c40c1f6199 100644 --- a/lib/blacklight/configuration.rb +++ b/lib/blacklight/configuration.rb @@ -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, diff --git a/spec/controllers/catalog_controller_spec.rb b/spec/controllers/catalog_controller_spec.rb index 512990ee0e..ee7f565cbe 100644 --- a/spec/controllers/catalog_controller_spec.rb +++ b/spec/controllers/catalog_controller_spec.rb @@ -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 diff --git a/spec/lib/blacklight/configuration_spec.rb b/spec/lib/blacklight/configuration_spec.rb index 15b849a084..999b402d3b 100644 --- a/spec/lib/blacklight/configuration_spec.rb +++ b/spec/lib/blacklight/configuration_spec.rb @@ -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