@@ -4,6 +4,21 @@ module ThinkingSphinx::ActiveRecord::Base
4
4
extend ActiveSupport ::Concern
5
5
6
6
included do
7
+ # Avoid method collisions for public Thinking Sphinx methods added to all
8
+ # ActiveRecord models. The `sphinx_`-prefixed versions will always exist,
9
+ # and the non-prefixed versions will be added if a method of that name
10
+ # doesn't already exist.
11
+ #
12
+ # If a method is overwritten later by something else, that's also fine - the
13
+ # prefixed versions will still be there.
14
+ class_module = ThinkingSphinx ::ActiveRecord ::Base ::ClassMethods
15
+ class_module . public_instance_methods . each do |method_name |
16
+ short_method = method_name . to_s . delete_prefix ( "sphinx_" ) . to_sym
17
+ next if methods . include? ( short_method )
18
+
19
+ define_singleton_method ( short_method , method ( method_name ) )
20
+ end
21
+
7
22
if ActiveRecord ::VERSION ::STRING . to_i >= 5
8
23
[
9
24
::ActiveRecord ::Reflection ::HasManyReflection ,
@@ -25,19 +40,19 @@ def extensions
25
40
end
26
41
27
42
module ClassMethods
28
- def facets ( query = nil , options = { } )
43
+ def sphinx_facets ( query = nil , options = { } )
29
44
merge_search ThinkingSphinx . facets , query , options
30
45
end
31
46
32
- def search ( query = nil , options = { } )
47
+ def sphinx_search ( query = nil , options = { } )
33
48
merge_search ThinkingSphinx . search , query , options
34
49
end
35
50
36
- def search_count ( query = nil , options = { } )
51
+ def sphinx_search_count ( query = nil , options = { } )
37
52
search_for_ids ( query , options ) . total_entries
38
53
end
39
54
40
- def search_for_ids ( query = nil , options = { } )
55
+ def sphinx_search_for_ids ( query = nil , options = { } )
41
56
ThinkingSphinx ::Search ::Merger . new (
42
57
search ( query , options )
43
58
) . merge! nil , :ids_only => true
0 commit comments