Skip to content

Commit

Permalink
fix load ordering of view paths
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeswick committed Jul 25, 2016
1 parent 1604fd1 commit bc1516f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/customizing_searchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ This file is automatically generated by Rails, and contains information about th

This is another place where you should define any of the dependencies to your gem, or development dependencies.

#### app/searchers/quicksearch/[my_searcher_name]_searcher.rb
#### app/searchers/quick_search/[my_searcher_name]_searcher.rb

This is where you would write actual searcher code. This file should contain a class called “[MySearcherName]Searcher” that subclasses Quicksearch::Searcher. This class should be inside the Quicksearch module.
This is where you would write actual searcher code. This file should contain a class called “[MySearcherName]Searcher” that subclasses QuickSearch::Searcher. This class should be inside the QuickSearch module.

A searcher is only required to have two methods implemented (if these are not implemented, you will get errors):

Expand Down
32 changes: 30 additions & 2 deletions lib/quick_search/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,45 @@ module QuickSearch
class Engine < ::Rails::Engine
isolate_namespace QuickSearch

# quick_search initializer
#
# We want to load the quick_search_config file, and set the loading order of the view paths as follows:
# 1. Application which imports gem engine/theme
# 2. Theme
# 3. 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)
QuickSearch::Engine::APP_CONFIG = YAML.load_file(config_file)[Rails.env]
ActiveSupport.on_load(:action_controller) do
theme_engine_class = "#{QuickSearch::Engine::APP_CONFIG['theme'].classify}::Engine".constantize
prepend_view_path theme_engine_class.root.join('app', 'views', QuickSearch::Engine::APP_CONFIG['theme'])
# get theme / core engine classes
theme_engine_class = "#{QuickSearch::Engine::APP_CONFIG['theme'].classify}::Engine".constantize
core_engine_class = "QuickSearch::Engine".constantize

# get the correct view paths for the application, theme, and engine
core_view_path = core_engine_class.root.join('app', 'views')
theme_view_path = theme_engine_class.root.join('app', 'views', QuickSearch::Engine::APP_CONFIG['theme'])
app_view_path = Rails.root.join('app', 'views')

# prepend to the existing view path ordering
prepend_view_path(core_view_path)
prepend_view_path(theme_view_path)
prepend_view_path(app_view_path)
end
end
end

# best_bets initializer
#
# Here we set up Best Bets. If there is a solr_url defined in the config, then we don't need to do anything,
# as Best Bets will be assumed to be in Solr, and the BestBetsSearcher will look there. Otherwise, load the
# best_bets.yml file into memory, and search that directly. The latter is only recommended for smaller amounts of
# Best Bets. The reason for this dual approach is that it removes Solr as an absolute requirement for QuickSearch.

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")
Expand Down

0 comments on commit bc1516f

Please sign in to comment.