Skip to content

Commit

Permalink
update #url_for_document to use explicit configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Mar 18, 2014
1 parent 4238a88 commit 93d8c57
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
7 changes: 5 additions & 2 deletions app/helpers/blacklight/url_helper_behavior.rb
Expand Up @@ -8,9 +8,12 @@ module Blacklight::UrlHelperBehavior
# to provide more interesting routing to
# documents
def url_for_document doc
if controller.is_a? Blacklight::Catalog and doc.is_a? SolrDocument and
if respond_to?(:blacklight_config) and
blacklight_config.show.route and
(!doc.respond_to?(:to_model) or doc.to_model.is_a? SolrDocument)
{ controller: controller_name, action: :show, id: doc }
route = blacklight_config.show.route.merge(action: :show, id: doc)
route[:controller] = controller_name if route[:controller] == :current
route
else
doc
end
Expand Down
10 changes: 9 additions & 1 deletion lib/blacklight/configuration.rb
Expand Up @@ -61,7 +61,15 @@ def default_values
:respond_to => OpenStructWithHashAccess.new()
),
# Additional configuration when displaying a single document
:show => ViewConfig::Show.new(:partials => [:show_header, :show]),
:show => ViewConfig::Show.new(
# default route parameters for 'show' requests
# set this to a hash with additional arguments to merge into
# the route, or set `controller: :current` to route to the
# current controller.
route: nil,
# partials to render for each document(see #render_document_partials)
partials: [:show_header, :show]
),
# Configurations for specific types of index views
:view => NestedOpenStructWithHashAccess.new(ViewConfig, 'list'),
# Maxiumum number of spelling suggestions to offer
Expand Down
20 changes: 16 additions & 4 deletions spec/helpers/url_helper_spec.rb
Expand Up @@ -27,19 +27,31 @@
end

it "should be a polymorphic routing-ready object" do
doc = double
doc = SolrDocument.new
expect(helper.url_for_document(doc)).to eq doc
end

it "should be a catalog controller-specific route" do
it "should allow for custom show routes" do
doc = SolrDocument.new
helper.blacklight_config.show.route = { controller: 'catalog' }
expect(helper.url_for_document(doc)).to eq({controller: 'catalog', action: :show, id: doc})
end

context "within bookmarks" do
let(:controller_class) { ::BookmarksController.new }

it "should use polymorphic routing" do
doc = SolrDocument.new
expect(helper.url_for_document(doc)).to eq doc
end
end

context "within an alternative catalog controller" do
let(:controller_class) { ::AlternateController.new }

it "should be a catalog controller-specific route" do
before do
helper.blacklight_config.show.route = { controller: :current }
end
it "should support the :current controller configuration" do
doc = SolrDocument.new
expect(helper.url_for_document(doc)).to eq({controller: 'alternate', action: :show, id: doc})
end
Expand Down

0 comments on commit 93d8c57

Please sign in to comment.