Skip to content

Commit

Permalink
Use association reflections for search calls.
Browse files Browse the repository at this point in the history
This seems to be the more modern approach, and avoids the flaky method cache memoisation that has cropped up in Rails 6. My one frustration is that the tests were green with the old approach, but that's not the case in my own upgraded-to-Rails-6 apps.
  • Loading branch information
pat committed Aug 21, 2019
1 parent 28ed4e3 commit d12f2e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
3 changes: 1 addition & 2 deletions lib/thinking_sphinx/active_record/association_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

module ThinkingSphinx::ActiveRecord::AssociationProxy
extend ActiveSupport::Concern

def search(query = nil, options = {})
perform_search super(*normalise_search_arguments(query, options))
end
Expand All @@ -12,6 +10,7 @@ def search_for_ids(query = nil, options = {})
end

private

def normalise_search_arguments(query, options)
query, options = nil, query if query.is_a?(Hash)
options[:ignore_scopes] = true
Expand Down
19 changes: 17 additions & 2 deletions lib/thinking_sphinx/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,23 @@ module ThinkingSphinx::ActiveRecord::Base
after_update ThinkingSphinx::ActiveRecord::Callbacks::UpdateCallbacks
after_commit ThinkingSphinx::ActiveRecord::Callbacks::DeltaCallbacks

::ActiveRecord::Associations::CollectionProxy.send :include,
ThinkingSphinx::ActiveRecord::AssociationProxy
if ActiveRecord::VERSION::STRING.to_i >= 5
[
::ActiveRecord::Reflection::HasManyReflection,
::ActiveRecord::Reflection::HasAndBelongsToManyReflection
].each do |reflection_class|
reflection_class.include DefaultReflectionAssociations
end
else
::ActiveRecord::Associations::CollectionProxy.send :include,
ThinkingSphinx::ActiveRecord::AssociationProxy
end
end

module DefaultReflectionAssociations
def extensions
super + [ThinkingSphinx::ActiveRecord::AssociationProxy]
end
end

module ClassMethods
Expand Down

0 comments on commit d12f2e1

Please sign in to comment.