Skip to content

Commit 23eae04

Browse files
Merge pull request #2082 from projectblacklight/allow_dots
Add configuration which enables custom routing constraints
2 parents f5f5165 + eb211fa commit 23eae04

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
put "saved_searches/save/:id", :to => "saved_searches#save", :as => "save_search"
88
delete "saved_searches/forget/:id", :to => "saved_searches#forget", :as => "forget_search"
99
post "saved_searches/forget/:id", :to => "saved_searches#forget"
10-
post "/catalog/:id/track", to: 'catalog#track', as: 'track_search_context'
10+
post "/catalog/:id/track", to: 'catalog#track', as: 'track_search_context', constraints: { id: Blacklight::Engine.config.routes.identifier_constraint }
1111

1212
resources :suggest, only: :index, defaults: { format: 'json' }
1313
end

lib/blacklight/engine.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Engine < Rails::Engine
77
require 'twitter-typeahead-rails'
88

99
# BlacklightHelper is needed by all helpers, so we inject it
10-
# into action view base here.
10+
# into action view base here.
1111
initializer 'blacklight.helpers' do |app|
1212
ActionView::Base.send :include, BlacklightHelper
1313
end
@@ -43,8 +43,12 @@ class Engine < Rails::Engine
4343
'Cricket' => 'mms.mycricket.com'
4444
}
4545

46+
config.routes = OpenStruct.new
47+
# Set identifier_constraint to enforce a format for the document identifiers
48+
config.routes.identifier_constraint = /[\w-]+/
49+
4650
config.bookmarks_http_method = :post
47-
51+
4852
config.email_regexp = defined?(Devise) ? Devise.email_regexp : /\A[^@\s]+@[^@\s]+\z/
4953
end
5054
end

spec/routing/catalog_routing_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@
2121
end
2222
end
2323

24+
describe 'tracking' do
25+
context 'when the routing constraint is set to allow periods' do
26+
before do
27+
if Rails.version < '5.2.0'
28+
skip 'requires https://github.com/rails/rails/pull/22435'
29+
end
30+
allow(Blacklight::Engine.config.routes).to receive(:identifier_constraint).and_return(%r{[^/]+})
31+
Rails.application.reload_routes!
32+
end
33+
34+
it 'routes to #track' do
35+
expect(post('/catalog/gallica.bnf.fr/track')).to route_to('catalog#track', id: 'gallica.bnf.fr')
36+
end
37+
end
38+
end
39+
2440

2541
describe "solr_document_path for SolrDocument", :test => true do
2642
it "routes correctly" do

0 commit comments

Comments
 (0)