search_scope 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +1 -1
- data/lib/search_scope.rb +19 -11
- data/search_scope.gemspec +1 -1
- metadata +1 -1
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('search_scope', '0.1.
|
5
|
+
Echoe.new('search_scope', '0.1.6') do |p|
|
6
6
|
p.description = "Simplify searching a model by defining custom named_scopes."
|
7
7
|
p.project = 'search-scope'
|
8
8
|
p.url = "http://rubyforge.org/projects/search-scope"
|
data/lib/search_scope.rb
CHANGED
@@ -72,12 +72,18 @@ module SearchScope
|
|
72
72
|
|
73
73
|
def search_scopes(options={})
|
74
74
|
scopes = []
|
75
|
+
options = options.clone
|
76
|
+
split_terms = options.delete :split_terms
|
75
77
|
search_scope_keys.each do |key|
|
76
78
|
|
77
79
|
#if the scope key isn't in the params, don't include it
|
78
80
|
next unless options[key]
|
79
|
-
|
80
|
-
|
81
|
+
if split_terms
|
82
|
+
# add the scope once for each term passed in (space delimited). this allows a search for 'star wars' to return only items where both terms match
|
83
|
+
terms = options[key].split.compact
|
84
|
+
else
|
85
|
+
terms = [options[key]]
|
86
|
+
end
|
81
87
|
terms.each do |term|
|
82
88
|
scopes << [key, term]
|
83
89
|
end
|
@@ -90,18 +96,19 @@ module SearchScope
|
|
90
96
|
scope = object.send(scope_name, *args)
|
91
97
|
end
|
92
98
|
|
93
|
-
|
94
|
-
#the quick_search is one that matches any of the named scopes, not all of them.
|
95
|
-
#TODO look into better ways of doing this, and figure out the proper name.
|
96
|
-
def quick_search_scope_options(quick_search_terms)
|
99
|
+
def quick_search_scope_options(quick_search_terms, split_terms)
|
97
100
|
conditions = []
|
98
101
|
includes = []
|
99
102
|
aggregate_scope = self
|
100
103
|
|
101
|
-
|
102
|
-
term_conditions = []
|
104
|
+
if split_terms
|
103
105
|
terms = quick_search_terms.split.compact
|
104
|
-
|
106
|
+
else
|
107
|
+
terms = [quick_search_terms]
|
108
|
+
end
|
109
|
+
terms.each_with_index do |term,index|
|
110
|
+
term_conditions = []
|
111
|
+
quick_search_scopes.each do |scope|
|
105
112
|
# quick_search_scope = self.send(scope, term)
|
106
113
|
quick_search_scope = get_search_scope_from_object(self, scope, term)
|
107
114
|
query_options = quick_search_scope.proxy_options
|
@@ -116,12 +123,13 @@ module SearchScope
|
|
116
123
|
conditions << term_conditions.collect{|c|"(#{c})"}.join(' OR ') #ORing makes sure any of the terms exist somewhere in any of the fields. I think this is what we actually need, plus "relevance" (does that mean sphinx?)
|
117
124
|
# conditions << term_conditions.collect{|c|"(#{c})"}.join(' AND ') #ANDing this will make it so that all the terms MUST appear in one field, eg author first and last name
|
118
125
|
end
|
119
|
-
conditions_sql = conditions.collect{|c|"(#{c})"}.join('
|
126
|
+
conditions_sql = conditions.collect{|c|"(#{c})"}.join(' AND ')
|
120
127
|
{:conditions => conditions_sql, :include => includes}
|
121
128
|
end
|
122
129
|
|
123
130
|
#this searches by chaining all of the named_scopes (search_scopes) that were included in the params
|
124
131
|
def search(params={})
|
132
|
+
params.reverse_merge! :split_terms => true
|
125
133
|
paginate = params.delete :paginate
|
126
134
|
aggregate_scope = self
|
127
135
|
search_scopes(params).each do |scope|
|
@@ -136,7 +144,7 @@ module SearchScope
|
|
136
144
|
end
|
137
145
|
end
|
138
146
|
unless params[:quick_search].blank?
|
139
|
-
aggregate_scope = aggregate_scope.scoped quick_search_scope_options(params[:quick_search])
|
147
|
+
aggregate_scope = aggregate_scope.scoped quick_search_scope_options(params[:quick_search], params[:split_terms])
|
140
148
|
end
|
141
149
|
|
142
150
|
params[:sort_by] ||= default_sort_choice
|
data/search_scope.gemspec
CHANGED