wvanbergen-scoped_search 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/TODO +1 -9
- data/lib/scoped_search/query_conditions_builder.rb +11 -4
- data/test/search_for_test.rb +12 -4
- data/test/tasks.rake +5 -0
- data/test/test_helper.rb +47 -6
- metadata +3 -3
- /data/{README.rdoc → README.textile} +0 -0
data/TODO
CHANGED
@@ -2,14 +2,6 @@ TODO items for named_scope
|
|
2
2
|
==========================
|
3
3
|
Contact willem AT vanbergen DOT org if you want to help out
|
4
4
|
|
5
|
-
0.6.0
|
6
|
-
- Add test for Postgres
|
7
|
-
- Add test for MySQL
|
8
|
-
|
9
|
-
|
10
|
-
0.7.0
|
11
|
-
|
12
|
-
|
13
5
|
0.8.0
|
14
6
|
|
15
7
|
|
@@ -17,7 +9,7 @@ Contact willem AT vanbergen DOT org if you want to help out
|
|
17
9
|
|
18
10
|
|
19
11
|
1.0.0
|
20
|
-
|
12
|
+
- Extensive testing
|
21
13
|
|
22
14
|
Documentation & testing:
|
23
15
|
- Add rdoc en comments to code
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module ScopedSearch
|
2
2
|
|
3
3
|
class QueryConditionsBuilder
|
4
|
+
## ActiveRecord::Base.connection.adapter_name
|
4
5
|
|
5
6
|
# Build the query
|
6
7
|
def self.build_query(search_conditions, query_fields)
|
@@ -10,6 +11,12 @@ module ScopedSearch
|
|
10
11
|
def initialize
|
11
12
|
@query_fields = nil
|
12
13
|
@query_params = {}
|
14
|
+
|
15
|
+
@sql_like = 'LIKE'
|
16
|
+
|
17
|
+
if ActiveRecord::Base.connected? and ActiveRecord::Base.connection.adapter_name.downcase == 'postgresql'
|
18
|
+
@sql_like = 'ILIKE'
|
19
|
+
end
|
13
20
|
end
|
14
21
|
|
15
22
|
|
@@ -66,7 +73,7 @@ module ScopedSearch
|
|
66
73
|
retVal = []
|
67
74
|
@query_fields.each do |field, field_type| #|key,value|
|
68
75
|
if field_type == :string or field_type == :text
|
69
|
-
retVal << "#{field}
|
76
|
+
retVal << "#{field} #{@sql_like} :#{keyword_name.to_s}"
|
70
77
|
end
|
71
78
|
end
|
72
79
|
"(#{retVal.join(' OR ')})"
|
@@ -77,7 +84,7 @@ module ScopedSearch
|
|
77
84
|
retVal = []
|
78
85
|
@query_fields.each do |field, field_type| #|key,value|
|
79
86
|
if field_type == :string or field_type == :text
|
80
|
-
retVal << "(#{field} NOT
|
87
|
+
retVal << "(#{field} NOT #{@sql_like} :#{keyword_name.to_s} OR #{field} IS NULL)"
|
81
88
|
end
|
82
89
|
end
|
83
90
|
"(#{retVal.join(' AND ')})"
|
@@ -92,7 +99,7 @@ module ScopedSearch
|
|
92
99
|
@query_params[keyword_name_b] = "%#{word2}%"
|
93
100
|
@query_fields.each do |field, field_type| #|key,value|
|
94
101
|
if field_type == :string or field_type == :text
|
95
|
-
retVal << "(#{field}
|
102
|
+
retVal << "(#{field} #{@sql_like} :#{keyword_name_a.to_s} OR #{field} #{@sql_like} :#{keyword_name_b.to_s})"
|
96
103
|
end
|
97
104
|
end
|
98
105
|
"(#{retVal.join(' OR ')})"
|
@@ -127,7 +134,7 @@ module ScopedSearch
|
|
127
134
|
@query_params[keyword_name_b] = "%#{value}%"
|
128
135
|
@query_fields.each do |field, field_type| #|key,value|
|
129
136
|
if field_type == :string or field_type == :text
|
130
|
-
retVal << "#{field}
|
137
|
+
retVal << "#{field} #{@sql_like} :#{keyword_name_b.to_s}"
|
131
138
|
end
|
132
139
|
end
|
133
140
|
|
data/test/search_for_test.rb
CHANGED
@@ -3,7 +3,15 @@ require File.dirname(__FILE__) + '/test_helper'
|
|
3
3
|
class ScopedSearchTest < Test::Unit::TestCase
|
4
4
|
|
5
5
|
def setup
|
6
|
-
|
6
|
+
case ENV['DATABASE']
|
7
|
+
when 'mysql'
|
8
|
+
create_mysql_connection
|
9
|
+
when 'postgresql'
|
10
|
+
create_postgresql_connection
|
11
|
+
else 'sqlite3'
|
12
|
+
create_sqlite3_connection
|
13
|
+
end
|
14
|
+
InitialSchema.up
|
7
15
|
SearchTestModel.create_corpus!
|
8
16
|
Group.create_corpus!
|
9
17
|
Location.create_corpus!
|
@@ -15,14 +23,14 @@ class ScopedSearchTest < Test::Unit::TestCase
|
|
15
23
|
end
|
16
24
|
|
17
25
|
def teardown
|
18
|
-
|
26
|
+
InitialSchema.down
|
19
27
|
end
|
20
28
|
|
21
29
|
def test_enabling
|
22
30
|
assert !SearchTestModel.respond_to?(:search_for)
|
23
31
|
SearchTestModel.searchable_on :string_field, :text_field, :date_field
|
24
32
|
assert SearchTestModel.respond_to?(:search_for)
|
25
|
-
|
33
|
+
|
26
34
|
assert_equal ActiveRecord::NamedScope::Scope, SearchTestModel.search_for('test').class
|
27
35
|
end
|
28
36
|
|
@@ -110,7 +118,7 @@ class ScopedSearchTest < Test::Unit::TestCase
|
|
110
118
|
|
111
119
|
def test_search_has_many_association
|
112
120
|
User.searchable_on :first_name, :last_name, :notes_title, :notes_content
|
113
|
-
|
121
|
+
|
114
122
|
assert_equal User.count, User.search_for('').count
|
115
123
|
assert_equal 2, User.search_for('Router').count
|
116
124
|
assert_equal 1, User.search_for('milk').count
|
data/test/tasks.rake
CHANGED
@@ -5,4 +5,9 @@ Rake::TestTask.new(:test) do |t|
|
|
5
5
|
t.pattern = 'test/**/*_test.rb'
|
6
6
|
t.verbose = true
|
7
7
|
t.libs << 'test'
|
8
|
+
|
9
|
+
# options are sqlite3, mysql or postgresql. The default
|
10
|
+
# is sqlite3 if not specified or if the parameter is invalid.
|
11
|
+
# If DATABASE is mysql then the MYSQLSOCKET can also be set if needed.
|
12
|
+
ENV['DATABASE'] = ENV['DATABASE'].nil? ? 'sqlite3' : ENV['DATABASE'].downcase
|
8
13
|
end
|
data/test/test_helper.rb
CHANGED
@@ -5,9 +5,9 @@ require 'ruby-debug'
|
|
5
5
|
|
6
6
|
require "#{File.dirname(__FILE__)}/../lib/scoped_search"
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
class InitialSchema < ActiveRecord::Migration
|
9
|
+
|
10
|
+
def self.up
|
11
11
|
create_table :search_test_models do |t|
|
12
12
|
t.string :string_field
|
13
13
|
t.text :text_field
|
@@ -52,14 +52,55 @@ def setup_db
|
|
52
52
|
|
53
53
|
create_table :addresses do |t|
|
54
54
|
t.string :street, :city, :state, :postal_code
|
55
|
-
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.down
|
59
|
+
drop_table :search_test_models
|
60
|
+
drop_table :users
|
61
|
+
drop_table :clients
|
62
|
+
drop_table :offices
|
63
|
+
drop_table :groups
|
64
|
+
drop_table :locations
|
65
|
+
drop_table :locations_users
|
66
|
+
drop_table :notes
|
67
|
+
drop_table :addresses
|
56
68
|
end
|
69
|
+
|
57
70
|
end
|
58
71
|
|
59
|
-
|
60
|
-
|
72
|
+
|
73
|
+
def create_sqlite3_connection
|
74
|
+
ActiveRecord::Base.establish_connection(
|
75
|
+
:adapter => "sqlite3",
|
76
|
+
:dbfile => ":memory:"
|
77
|
+
)
|
61
78
|
end
|
62
79
|
|
80
|
+
def create_postgresql_connection
|
81
|
+
ActiveRecord::Base.establish_connection(
|
82
|
+
:adapter => "postgresql",
|
83
|
+
:host => "localhost",
|
84
|
+
:username => "sstest",
|
85
|
+
:password => "sstest",
|
86
|
+
:database => "scoped_search_test"
|
87
|
+
)
|
88
|
+
end
|
89
|
+
|
90
|
+
def create_mysql_connection
|
91
|
+
# '/tmp/mysql.sock' is the default location and file rails looks for.
|
92
|
+
mysql_socket = ENV['MYSQLSOCKET'].nil? ? '/tmp/mysql.sock' : ENV['MYSQLSOCKET']
|
93
|
+
ActiveRecord::Base.establish_connection(
|
94
|
+
:adapter => "mysql",
|
95
|
+
:host => "localhost",
|
96
|
+
:username => "sstest",
|
97
|
+
:password => "sstest",
|
98
|
+
:database => "scoped_search_test",
|
99
|
+
:socket => mysql_socket
|
100
|
+
)
|
101
|
+
end
|
102
|
+
|
103
|
+
|
63
104
|
class SearchTestModel < ActiveRecord::Base
|
64
105
|
def self.create_corpus!
|
65
106
|
create!(:string_field => "Programmer 123", :text_field => nil, :ignored_field => "123456", :date_field => '2000-01-01')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wvanbergen-scoped_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Willem van Bergen
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2008-
|
13
|
+
date: 2008-10-11 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -27,7 +27,7 @@ extra_rdoc_files: []
|
|
27
27
|
files:
|
28
28
|
- CHANGELOG
|
29
29
|
- LICENSE
|
30
|
-
- README.
|
30
|
+
- README.textile
|
31
31
|
- Rakefile
|
32
32
|
- TODO
|
33
33
|
- init.rb
|
File without changes
|