Skip to content

Commit

Permalink
Add blacklight configuration for registering additional response form…
Browse files Browse the repository at this point in the history
…ats for the #index action

Options include:

- do the default
    config.index.respond_to.yaml = true

- don't render the format
    config.index.respond_to.yaml = true

- options for render
    config.index.respond_to.yaml = { layout: 'custom-layout' }

- custom proc to render
    config.index.respond_to.yaml = lambda { render text: "stuff" }

- controller method to render
    config.index.respond_to.yaml = :my_custom_yaml_serialization
  • Loading branch information
cbeer committed Feb 10, 2014
1 parent d1a9955 commit 4dd0ff6
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

9 comments on commit 4dd0ff6

@jrochkind
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, this is useful, nice work, thanks.

@jrochkind
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there docs anywhere but this commit message? In the docs in this commit message, the examples for 'do the default' and 'don't render the format' are identical.

@cbeer
Copy link
Member Author

@cbeer cbeer commented on 4dd0ff6 Feb 10, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrochkind what are you looking at when you say they "are identical"?

I'll be adding docs to the wiki shortly, now that the commits are merged.

@jrochkind
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the docs on this commit message:

- do the default
    config.index.respond_to.yaml = true

- don't render the format
    config.index.respond_to.yaml = true

The first two options given are "do the default", and "don't render the format". The examples of how you do these two things are identical, even though the labels are different, they are both: config.index.respond_to.yaml = true. This is probably not right, but I don't know.

That is what I mean by "the examples for 'do the default' and 'don't render the format' are identical."

@jrochkind
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible the default is "don't render the format", so these are the same? I'm not really sure what the default would be -- I'd think 'the default' is what would happen if you didn't supply any config at all, I'm not sure if that's true in this case, if true will always be the same as supplying no config at all.

@cbeer
Copy link
Member Author

@cbeer cbeer commented on 4dd0ff6 Feb 10, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, in the commit message. Sorry, I was looking in the tests. default (by which, I meant whatever Rails does) should be true, don't render should be false.

@jrochkind
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Normally the default, whatever Rails does, will be raise an exception, I'd imagine.

@cbeer
Copy link
Member Author

@cbeer cbeer commented on 4dd0ff6 Feb 10, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or render a template in the appropriate look up path (e.g. index.yaml.erb)

@jrochkind
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right on. And you could actually suppress/override that with false, to, I guess, prevent rendering for that format even if Blacklight or another engine provided a proper template. I guess maybe you might want to do that sometimes.

Please sign in to comment.