Module: ActiveRecordSunspotter::SearchSunspotFor

Defined in:
lib/active_record_sunspotter/search_sunspot_for.rb

Instance Method Summary collapse

Instance Method Details

#search_orderObject



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/active_record_sunspotter/search_sunspot_for.rb', line 121

def search_order
	if params[:order] and @sunspot_search_class.sunspot_orderable_column_names.include?(
			params[:order].downcase )
		order_string = params[:order]
		dir = case params[:dir].try(:downcase)
			when 'desc' then 'desc'
			else 'asc'
		end
		return order_string.to_sym, dir.to_sym
	else
		return :id, :asc
	end
end

#search_sunspot_for(search_class) ⇒ Object



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/active_record_sunspotter/search_sunspot_for.rb', line 3

def search_sunspot_for( search_class )
	@sunspot_search_class = search_class

	#	Formerly a before_filter, but after being genericized,
	#	we don't know the search class until the search begins.
	@sunspot_search_class.methods.include?(:solr_search) ||
		access_denied("Sunspot server probably wasn't started first!", root_path)

#
#	Something has changed that causes this now when :search is stubbed.
#	Unstubbing makes testing raise and error and basically stop.
#
#/opt/local/lib/ruby2.0/gems/2.0.0/gems/mocha-0.13.3/lib/mocha/class_method.rb:80:in `public': undefined method `search' for class `Class' (NameError)
#
#	changing this to use solr_search rather than search seems to make it ok.
#	search is actually an alias to solr_search which may be the cause.
#	new ruby, new rules?  Perhaps a newer mocha would work, but I've had
#	a number of problems with newer versions.
#

	begin
		@search = @sunspot_search_class.solr_search do

			if params[:q].present?
				fulltext params[:q]
			end

			self.instance_variable_get('@setup').clazz.sunspot_all_filters.each do |f|	#	don't use |facet|

				p=f.name

				if f.range

					range_facet_and_filter_for(p,params.dup,f.range)

				elsif f.ranges	#	YES, PLURAL for an array of fixed ranges

					fixed_range_facet_and_filter_for(p,params.dup,f.ranges)

				else

					if params[p]
						#
						#	20130423 - be advised that false.blank? is true so the boolean attributes
						#						will not work correctly here.  Need to find another way.
						#			I don't use boolean columns anymore
						#
						params[p] = [params[p].dup].flatten.reject{|x|x.blank?}

						if params[p+'_op'] && params[p+'_op'].match(/AND/i).present?
							unless params[p].blank?	#	empty?	#	blank? works for arrays too
								with(p).all_of params[p]
							else
								params.delete(p)	#	remove the key so doesn't show in view
							end

						#
						#	NOTE This is an INTEGER SORT for the BETWEEN filter!
						#
						elsif params[p+'_op'] && params[p+'_op'].match(/BETWEEN/i).present?
							unless params[p].blank?	#empty?	# blank? works for arrays too
								#	between is expecting an array with a first and last (can be array of 1 really)
								with(p).between [params[p].sort_by(&:to_i)].flatten
							else
								params.delete(p)	#	remove the key so doesn't show in view
							end

						else	#	using 'OR'
							unless params[p].blank?	#empty?	# blank? works for arrays too
								with(p).any_of params[p]
							else
								params.delete(p)	#	remove the key so doesn't show in view
							end
						end	#	if params[p+'_op'] && params[p+'_op']=='AND'

					end	#	if params[p]

#if f.type == :dynamic
#	dynamic f.namespace { facet f.
#else
					#	facet.sort
					#	This param determines the ordering of the facet field constraints.
					#	    count - sort the constraints by count (highest count first)
					#	    index - to return the constraints sorted in their index order 
					#			(lexicographic by indexed term). For terms in the ascii range, 
					#				this will be alphabetically sorted. 
					#	The default is count if facet.limit is greater than 0, index otherwise.
					#	Prior to Solr1.4, one needed to use true instead of count and false instead of index.
					#	This parameter can be specified on a per field basis. 
					#
					#	put this inside the else condition as the if block is
					#	for ranges and it calls facet
					facet p.to_sym, :sort => :index if f.facetable
#end

				end

			end	#	@sunspot_search_class.sunspot_all_filters.each do |p|

			order_by *search_order

			if request.format.to_s.match(/csv|json/)
				#	don't paginate csv file.  Only way seems to be to make BIG query
				#	rather than the arbitrarily big number, I could possibly
				#	use the @search.total from the previous search sent as param?
				paginate :page => 1, :per_page => 1000000
			else
				paginate :page => params[:page], :per_page => params[:per_page]||=50
			end
		end	#	@search = @sunspot_search_class.solr_search do

	rescue Errno::ECONNREFUSED
		flash[:error] = "Solr seems to be down for the moment."
		redirect_to root_path
	end	#	begin

end