acts_as_searchable.rb
2.54 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
70
71
72
73
74
75
76
77
78
module ActsAsSearchable
module ClassMethods
ACTS_AS_SEARCHABLE_ENABLED = true unless defined? ACTS_AS_SEARCHABLE_ENABLED
def acts_as_searchable(options = {})
return if !ACTS_AS_SEARCHABLE_ENABLED
if (!options[:fields])
options[:additional_fields] ||= []
options[:additional_fields] |= [{:schema_name => :string}]
else
options[:fields] << {:schema_name => :string}
end
acts_as_solr options
extend FindByContents
send :include, InstanceMethods
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 = {})
pg_options[:page] ||= 1
pg_options[:per_page] ||= 20
options[:page] = pg_options[:page].to_i
options[:per_page] = pg_options[:per_page].to_i
options[:scores] ||= true
options[:filter_queries] ||= []
options[:filter_queries] << "schema_name:\"#{schema_name}\"" unless schema_name.empty?
all_facets_enabled = options.delete(:all_facets)
options[:per_page] = options.delete(:extra_limit) if options[:extra_limit]
results = []
facets = all_facets = {}
solr_result = find_by_solr(query, options)
if all_facets_enabled
options[:facets][:browse] = nil
all_facets = find_by_solr(query, options.merge(:per_page => 0)).facets
end
if !solr_result.nil?
facets = options.include?(:facets) ? solr_result.facets : []
if db_options.empty?
results = solr_result
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
{:results => results, :facets => facets, :all_facets => all_facets}
end
end
end
end
ActiveRecord::Base.send(:extend, ActsAsSearchable::ClassMethods)