From 4d3cc9ed6aa5b9ea17325436849c7c7b93785cfd Mon Sep 17 00:00:00 2001 From: "David P. Steelman" Date: Fri, 31 May 2019 11:12:51 -0400 Subject: [PATCH] Pass Best Bet keywords through "filter_query" method When loading the Best Bet keywords, pass them through the "filter_query" method in the "QuickSearch::QueryFilter" concern, so that they will be matched with a query term passed through the same method. This takes care of things like a dash (hyphen) being removed from the query term. The implementation was somewhat complicated by the fact that the "filter_query" method is a private method on a Rails concern. This was handled by creating a helper class in the initializer that delegates to the concern. It is not clear if this is the best way to accomplish this, but it does appear to work. Modified "test/dummy/config/best_bets.yml" to add a new Best Bet entry with a keyword including a dash for use in "test/integration/best_bets_initializer_test.rb" Added "xhr_search" route to "test/dummy/config/routes.rb" so it would be available as a "*_path" helper in the test. --- lib/quick_search/engine.rb | 18 +++++++++++++++++- test/dummy/config/best_bets.yml | 6 ++++++ test/dummy/config/routes.rb | 1 + test/integration/best_bets_initializer_test.rb | 15 +++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 test/integration/best_bets_initializer_test.rb diff --git a/lib/quick_search/engine.rb b/lib/quick_search/engine.rb index 103c11f..e8f1b89 100644 --- a/lib/quick_search/engine.rb +++ b/lib/quick_search/engine.rb @@ -11,7 +11,7 @@ class Engine < ::Rails::Engine # # This way, we can make application specific overrides of views, otherwise fall back to theme, # and finally fall back to the QS core if needed. - + initializer :quick_search, :after => :add_view_paths do config_file = File.join(Rails.root, "/config/quick_search_config.yml") if File.exist?(config_file) @@ -44,12 +44,28 @@ class Engine < ::Rails::Engine initializer :best_bets, :after => :quick_search do if defined? QuickSearch::Engine::APP_CONFIG and QuickSearch::Engine::APP_CONFIG['best_bets']['solr_url'].empty? best_bets_file = File.join(Rails.root, "/config/best_bets.yml") + if File.exist?(best_bets_file) + + # Helper class for enabling access to the "filter_query" method + # in QuickSearch::QueryFilter concern + class BestBetFilter + include QuickSearch::QueryFilter + + def filter(keyword) + filter_query(keyword) + end + end + + best_bet_filter = BestBetFilter.new QuickSearch::Engine::BEST_BETS = YAML.load_file(best_bets_file)['best_bets'] QuickSearch::Engine::BEST_BETS_INDEX = {} QuickSearch::Engine::BEST_BETS.each do |best_bet_name, best_bet| QuickSearch::Engine::BEST_BETS[best_bet_name]['id'] = best_bet_name best_bet['keywords'].each do |keyword| + # Pass keyword through "filter_query" method so that keyword + # will match query term passed through the same method + keyword = best_bet_filter.filter(keyword) QuickSearch::Engine::BEST_BETS_INDEX[keyword.downcase] = best_bet_name end end diff --git a/test/dummy/config/best_bets.yml b/test/dummy/config/best_bets.yml index 93decc2..f4f482d 100644 --- a/test/dummy/config/best_bets.yml +++ b/test/dummy/config/best_bets.yml @@ -84,3 +84,9 @@ best_bets: keywords: - pubmed - pub med + testbestbetdash: + title: Test Best Bet Entry with Dash in Keyword + description: This is a test Best Bet entry to test keywords containing a dash + url: http://example.org + keywords: + - 123-456 diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb index 00de79a..24707b0 100644 --- a/test/dummy/config/routes.rb +++ b/test/dummy/config/routes.rb @@ -3,4 +3,5 @@ mount QuickSearch::Engine => "/" root :to => 'quick_search/search#index' + get 'xhr_search' => 'quick_search/search#xhr_search', :defaults => { :format => 'html' } end diff --git a/test/integration/best_bets_initializer_test.rb b/test/integration/best_bets_initializer_test.rb new file mode 100644 index 0000000..75e734b --- /dev/null +++ b/test/integration/best_bets_initializer_test.rb @@ -0,0 +1,15 @@ +require 'test_helper' + +class BestBestInitializerTest < ActionDispatch::IntegrationTest + test 'best bet keyword with dash should be found' do + # Clear the solr_url so we don't try to get Best Bet from Solr + QuickSearch::Engine::APP_CONFIG['best_bets']['solr_url'] = '' + + # Keyword for "testbestbet" entry in test/dummy/config/best_bets.yml + visit xhr_search_path(q: '123-456', endpoint: 'best_bets', format: 'json') + + json = JSON.parse(page.html) + first_result = json['results'][0] + assert_equal 'testbestbetdash', first_result['id'] + end +end