scoped_search 2.3.3 → 2.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -191,7 +191,8 @@ module ScopedSearch
191
191
  return complete_key_value(field, token, val) if field.key_field
192
192
 
193
193
  opts = value_conditions(field.field, val)
194
- opts.merge!(:limit => 20, :select => "DISTINCT #{field.field}")
194
+ table = field.klass.connection.quote_table_name(field.klass.table_name)
195
+ opts.merge!(:limit => 20, :select => "DISTINCT #{table}.#{field.field}")
195
196
 
196
197
  return completer_scope(field.klass).all(opts).map(&field.field).compact.map{|v| v.to_s =~ /\s+/ ? "\"#{v}\"" : v}
197
198
  end
@@ -165,7 +165,10 @@ module ScopedSearch
165
165
  # this method return definitions::field object from string
166
166
  def field_by_name(name)
167
167
  field = fields[name.to_sym]
168
- field ||= fields[name.to_s.split('.')[0].to_sym]
168
+ if field.nil?
169
+ dotted = name.to_s.split('.')[0]
170
+ field = fields[dotted.to_sym] unless dotted.nil?
171
+ end
169
172
  field
170
173
  end
171
174
 
@@ -44,6 +44,8 @@ module ScopedSearch
44
44
  # Actually builds the find parameters hash that should be used in the search_for
45
45
  # named scope.
46
46
  def build_find_params(options)
47
+ keyconditions = []
48
+ keyparameters = []
47
49
  parameters = []
48
50
  includes = []
49
51
  joins = []
@@ -55,6 +57,8 @@ module ScopedSearch
55
57
  # Store the parameters, includes, etc so that they can be added to
56
58
  # the find-hash later on.
57
59
  case notification
60
+ when :keycondition then keyconditions << value
61
+ when :keyparameter then keyparameters << value
58
62
  when :parameter then parameters << value
59
63
  when :include then includes << value
60
64
  when :joins then joins << value
@@ -70,10 +74,10 @@ module ScopedSearch
70
74
  else raise ScopedSearch::QueryNotSupported, "Cannot handle #{notification.inspect}: #{value.inspect}"
71
75
  end
72
76
  end
73
-
77
+ sql = (keyconditions + (sql.nil? ? [] : [sql]) ).map {|c| "(#{c})"}.join(" AND ")
74
78
  # Build hash for ActiveRecord::Base#find for the named scope
75
79
  find_attributes = {}
76
- find_attributes[:conditions] = [sql] + parameters unless sql.nil?
80
+ find_attributes[:conditions] = [sql] + keyparameters + parameters unless sql.blank?
77
81
  find_attributes[:include] = includes.uniq unless includes.empty?
78
82
  find_attributes[:joins] = joins.uniq unless joins.empty?
79
83
  find_attributes[:order] = order unless order.nil?
@@ -196,7 +200,7 @@ module ScopedSearch
196
200
  def sql_test(field, operator, value, lhs, &block) # :yields: finder_option_type, value
197
201
  return field.to_ext_method_sql(lhs, sql_operator(operator, field), value, &block) if field.ext_method
198
202
 
199
- yield(:parameter, lhs.sub(/^.*\./,'')) if field.key_field
203
+ yield(:keyparameter, lhs.sub(/^.*\./,'')) if field.key_field
200
204
 
201
205
  if [:like, :unlike].include?(operator)
202
206
  yield(:parameter, (value !~ /^\%|\*/ && value !~ /\%|\*$/) ? "%#{value}%" : value.tr_s('%*', '%'))
@@ -227,14 +231,14 @@ module ScopedSearch
227
231
  connection = klass.connection
228
232
  if key_relation
229
233
  yield(:joins, construct_join_sql(key_relation, num) )
234
+ yield(:keycondition, "#{key_klass.table_name}_#{num}.#{connection.quote_column_name(key_field.to_s)} = ?")
230
235
  klass_table_name = relation ? "#{klass.table_name}_#{num}" : connection.quote_table_name(klass.table_name)
231
- return "#{key_klass.table_name}_#{num}.#{connection.quote_column_name(key_field.to_s)} = ? AND " +
232
- "#{klass_table_name}.#{connection.quote_column_name(field.to_s)}"
236
+ return "#{klass_table_name}.#{connection.quote_column_name(field.to_s)}"
233
237
  elsif key_field
234
238
  yield(:joins, construct_simple_join_sql(num))
239
+ yield(:keycondition, "#{key_klass.table_name}_#{num}.#{connection.quote_column_name(key_field.to_s)} = ?")
235
240
  klass_table_name = relation ? "#{klass.table_name}_#{num}" : connection.quote_table_name(klass.table_name)
236
- return "#{key_klass.table_name}_#{num}.#{connection.quote_column_name(key_field.to_s)} = ? AND " +
237
- "#{klass_table_name}.#{connection.quote_column_name(field.to_s)}"
241
+ return "#{klass_table_name}.#{connection.quote_column_name(field.to_s)}"
238
242
  elsif relation
239
243
  yield(:include, relation)
240
244
  end
data/lib/scoped_search.rb CHANGED
@@ -14,7 +14,7 @@ module ScopedSearch
14
14
 
15
15
  # The current scoped_search version. Do not change thisvalue by hand,
16
16
  # because it will be updated automatically by the gem release script.
17
- VERSION = "2.3.3"
17
+ VERSION = "2.3.4"
18
18
 
19
19
  # The ClassMethods module will be included into the ActiveRecord::Base class
20
20
  # to add the <tt>ActiveRecord::Base.scoped_search</tt> method and the
@@ -3,8 +3,8 @@ Gem::Specification.new do |s|
3
3
 
4
4
  # Do not change the version and date fields by hand. This will be done
5
5
  # automatically by the gem release script.
6
- s.version = "2.3.3"
7
- s.date = "2011-09-06"
6
+ s.version = "2.3.4"
7
+ s.date = "2011-10-03"
8
8
 
9
9
  s.summary = "Easily search you ActiveRecord models with a simple query language using a named scope."
10
10
  s.description = <<-EOS
@@ -56,6 +56,10 @@ require "#{File.dirname(__FILE__)}/../spec_helper"
56
56
  Bar.search_for('facts.color = green').should have(1).items
57
57
  end
58
58
 
59
+ it "should find all bars with a fact name color and fact value gold" do
60
+ Bar.search_for('facts.color = gold').first.name.should eql('barbary')
61
+ end
62
+
59
63
  it "should find all bars with a fact name size and fact value 5" do
60
64
  Bar.search_for('facts.size = 5').should have(1).items
61
65
  end
@@ -64,6 +68,10 @@ require "#{File.dirname(__FILE__)}/../spec_helper"
64
68
  Bar.search_for('facts.color = green and facts.size = 5').should have(1).items
65
69
  end
66
70
 
71
+ it "should find all bars with a fact color gold or green" do
72
+ Bar.search_for('facts.color = gold or facts.color = green').should have(2).items
73
+ end
74
+
67
75
  it "should find all bars that has size value" do
68
76
  Bar.search_for('has facts.size').should have(1).items
69
77
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scoped_search
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 3
9
- - 3
10
- version: 2.3.3
9
+ - 4
10
+ version: 2.3.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Willem van Bergen
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-09-06 00:00:00 +03:00
19
+ date: 2011-10-03 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency