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 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') do |p|
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
- # 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
80
- terms = options[key].split.compact
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
- #this gets all of the options from the quick_search named scopes and builds a quick_search from them
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
- quick_search_scopes.each do |scope|
102
- term_conditions = []
104
+ if split_terms
103
105
  terms = quick_search_terms.split.compact
104
- terms.each_with_index do |term,index|
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(' OR ')
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{search_scope}
5
- s.version = "0.1.5"
5
+ s.version = "0.1.6"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Ryan Owens"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: search_scope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Owens