Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass Best Bet keywords through "filter_query" method #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/quick_search/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/dummy/config/best_bets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions test/integration/best_bets_initializer_test.rb
Original file line number Diff line number Diff line change
@@ -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