acts_as_searchable.rb
2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module ActsAsSearchable
module ClassMethods
ACTS_AS_SEARCHABLE_ENABLED = true unless defined? ACTS_AS_SEARCHABLE_ENABLED
def acts_as_searchable(options = {})
if ACTS_AS_SEARCHABLE_ENABLED
if (!options[:fields])
options[:additional_fields] |= [{:schema_name => :string}]
else
options[:fields] << {:schema_name => :string}
end
acts_as_solr options
extend FindByContents
send :include, InstanceMethods
end
end
module InstanceMethods
def schema_name
(Noosfero::MultiTenancy.on? and ActiveRecord::Base.postgresql?) ? ActiveRecord::Base.connection.schema_search_path : ''
end
end
module FindByContents
def schema_name
(Noosfero::MultiTenancy.on? and ActiveRecord::Base.postgresql?) ? ActiveRecord::Base.connection.schema_search_path : ''
end
def find_by_contents(query, pg_options = {}, options = {}, db_options = {})
options[:limit] = 1000000;
options[:scores] = true;
query = !schema_name.empty? ? "+schema_name:\"#{schema_name}\" AND #{query}" : query
solr_result = find_by_solr(query, options)
if solr_result.nil?
results = facets = []
else
facets = options.include?(:facets) ? solr_result.facets : {}
if db_options.empty?
results = solr_result.results
else
ids = solr_result.results.map{|r|r[:id].to_i}
if ids.empty?
ids << -1
end
if db_options[:conditions]
db_options[:conditions] = sanitize_sql_for_conditions(db_options[:conditions]) + " and #{table_name}.id in (#{ids.join(', ')})"
else
db_options[:conditions] = "#{table_name}.id in (#{ids.join(', ')})"
end
results = find(:all, db_options)
end
end
if !pg_options.empty?
pg_options[:page] ||= 1
results = results.paginate(pg_options)
end
{:results => results, :facets => facets}
end
end
end
end
ActiveRecord::Base.send(:extend, ActsAsSearchable::ClassMethods)