search_cop 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.travis.yml +33 -0
- data/Appraisals +14 -0
- data/Gemfile +23 -0
- data/LICENSE.txt +22 -0
- data/MIGRATION.md +66 -0
- data/README.md +530 -0
- data/Rakefile +9 -0
- data/gemfiles/3.2.gemfile +26 -0
- data/gemfiles/4.0.gemfile +26 -0
- data/gemfiles/4.1.gemfile +26 -0
- data/lib/search_cop/arel/visitors.rb +223 -0
- data/lib/search_cop/arel.rb +4 -0
- data/lib/search_cop/grammar_parser.rb +22 -0
- data/lib/search_cop/hash_parser.rb +42 -0
- data/lib/search_cop/query_builder.rb +26 -0
- data/lib/search_cop/query_info.rb +13 -0
- data/lib/search_cop/search_scope.rb +62 -0
- data/lib/search_cop/version.rb +3 -0
- data/lib/search_cop.rb +71 -0
- data/lib/search_cop_grammar/attributes.rb +229 -0
- data/lib/search_cop_grammar/nodes.rb +183 -0
- data/lib/search_cop_grammar.rb +133 -0
- data/lib/search_cop_grammar.treetop +55 -0
- data/search_cop.gemspec +29 -0
- data/test/and_test.rb +27 -0
- data/test/boolean_test.rb +53 -0
- data/test/database.yml +17 -0
- data/test/date_test.rb +75 -0
- data/test/datetime_test.rb +76 -0
- data/test/error_test.rb +17 -0
- data/test/float_test.rb +67 -0
- data/test/fulltext_test.rb +27 -0
- data/test/hash_test.rb +97 -0
- data/test/integer_test.rb +67 -0
- data/test/not_test.rb +27 -0
- data/test/or_test.rb +29 -0
- data/test/scope_test.rb +35 -0
- data/test/search_cop_test.rb +131 -0
- data/test/string_test.rb +84 -0
- data/test/test_helper.rb +150 -0
- metadata +216 -0
data/test/test_helper.rb
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
|
2
|
+
require "search_cop"
|
3
|
+
|
4
|
+
begin
|
5
|
+
require "minitest"
|
6
|
+
|
7
|
+
class SearchCop::TestCase < MiniTest::Test; end
|
8
|
+
rescue LoadError
|
9
|
+
require "minitest/unit"
|
10
|
+
|
11
|
+
class SearchCop::TestCase < MiniTest::Unit::TestCase; end
|
12
|
+
end
|
13
|
+
|
14
|
+
require "minitest/autorun"
|
15
|
+
require "active_record"
|
16
|
+
require "factory_girl"
|
17
|
+
require "yaml"
|
18
|
+
|
19
|
+
DATABASE = ENV["DATABASE"] || "sqlite"
|
20
|
+
|
21
|
+
ActiveRecord::Base.establish_connection YAML.load_file(File.expand_path("../database.yml", __FILE__))[DATABASE]
|
22
|
+
|
23
|
+
class User < ActiveRecord::Base; end
|
24
|
+
|
25
|
+
class Comment < ActiveRecord::Base
|
26
|
+
include SearchCop
|
27
|
+
|
28
|
+
belongs_to :user
|
29
|
+
|
30
|
+
search_scope :search do
|
31
|
+
attributes :user => "user.username"
|
32
|
+
attributes :title, :message
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Product < ActiveRecord::Base
|
37
|
+
include SearchCop
|
38
|
+
|
39
|
+
search_scope :search do
|
40
|
+
attributes :title, :description, :brand, :notice, :stock, :price, :created_at, :created_on, :available
|
41
|
+
attributes :comment => ["comments.title", "comments.message"], :user => ["users.username", "users_products.username"]
|
42
|
+
attributes :primary => [:title, :description]
|
43
|
+
|
44
|
+
aliases :users_products => :user
|
45
|
+
|
46
|
+
if DATABASE != "sqlite"
|
47
|
+
options :title, :type => :fulltext
|
48
|
+
options :description, :type => :fulltext
|
49
|
+
options :comment, :type => :fulltext
|
50
|
+
end
|
51
|
+
|
52
|
+
if DATABASE == "postgres"
|
53
|
+
options :title, :dictionary => "english"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
search_scope :user_search do
|
58
|
+
attributes :title, :description
|
59
|
+
attributes :user => "users_products.username"
|
60
|
+
|
61
|
+
options :title, :default => true
|
62
|
+
aliases :users_products => :user
|
63
|
+
end
|
64
|
+
|
65
|
+
has_many :comments
|
66
|
+
has_many :users, :through => :comments
|
67
|
+
|
68
|
+
belongs_to :user
|
69
|
+
end
|
70
|
+
|
71
|
+
FactoryGirl.define do
|
72
|
+
factory :product do
|
73
|
+
end
|
74
|
+
|
75
|
+
factory :comment do
|
76
|
+
end
|
77
|
+
|
78
|
+
factory :user do
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS products"
|
83
|
+
ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS comments"
|
84
|
+
ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS users"
|
85
|
+
|
86
|
+
ActiveRecord::Base.connection.create_table :products do |t|
|
87
|
+
t.references :user
|
88
|
+
t.string :title
|
89
|
+
t.text :description
|
90
|
+
t.integer :stock
|
91
|
+
t.float :price
|
92
|
+
t.datetime :created_at
|
93
|
+
t.date :created_on
|
94
|
+
t.boolean :available
|
95
|
+
t.string :brand
|
96
|
+
t.string :notice
|
97
|
+
end
|
98
|
+
|
99
|
+
ActiveRecord::Base.connection.create_table :comments do |t|
|
100
|
+
t.references :product
|
101
|
+
t.references :user
|
102
|
+
t.string :title
|
103
|
+
t.text :message
|
104
|
+
end
|
105
|
+
|
106
|
+
ActiveRecord::Base.connection.create_table :users do |t|
|
107
|
+
t.string :username
|
108
|
+
end
|
109
|
+
|
110
|
+
if DATABASE == "mysql"
|
111
|
+
ActiveRecord::Base.connection.execute "ALTER TABLE products ENGINE=MyISAM"
|
112
|
+
ActiveRecord::Base.connection.execute "ALTER TABLE products ADD FULLTEXT INDEX(title), ADD FULLTEXT INDEX(description), ADD FULLTEXT INDEX(title, description)"
|
113
|
+
|
114
|
+
ActiveRecord::Base.connection.execute "ALTER TABLE comments ENGINE=MyISAM"
|
115
|
+
ActiveRecord::Base.connection.execute "ALTER TABLE comments ADD FULLTEXT INDEX(title, message)"
|
116
|
+
end
|
117
|
+
|
118
|
+
class SearchCop::TestCase
|
119
|
+
include FactoryGirl::Syntax::Methods
|
120
|
+
|
121
|
+
def teardown
|
122
|
+
Product.delete_all
|
123
|
+
Comment.delete_all
|
124
|
+
end
|
125
|
+
|
126
|
+
def with_options(scope, key, options = {})
|
127
|
+
opts = scope.reflection.options[key.to_s] || {}
|
128
|
+
|
129
|
+
scope.reflection.options[key.to_s] = opts.merge(options)
|
130
|
+
|
131
|
+
yield
|
132
|
+
ensure
|
133
|
+
scope.reflection.options[key.to_s] = opts
|
134
|
+
end
|
135
|
+
|
136
|
+
def with_scope(scope, blk)
|
137
|
+
orig = scope.reflection.scope
|
138
|
+
|
139
|
+
scope.reflection.scope = blk
|
140
|
+
|
141
|
+
yield
|
142
|
+
ensure
|
143
|
+
scope.reflection.scope = orig
|
144
|
+
end
|
145
|
+
|
146
|
+
def assert_not_nil(value)
|
147
|
+
assert value
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
metadata
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: search_cop
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Benjamin Vetter
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-07-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: treetop
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: activerecord
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 3.0.0
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 3.0.0
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: factory_girl
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: appraisal
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: minitest
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
description: Search engine like fulltext query support for ActiveRecord
|
127
|
+
email:
|
128
|
+
- vetter@flakks.com
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- .gitignore
|
134
|
+
- .travis.yml
|
135
|
+
- Appraisals
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- MIGRATION.md
|
139
|
+
- README.md
|
140
|
+
- Rakefile
|
141
|
+
- gemfiles/3.2.gemfile
|
142
|
+
- gemfiles/4.0.gemfile
|
143
|
+
- gemfiles/4.1.gemfile
|
144
|
+
- lib/search_cop.rb
|
145
|
+
- lib/search_cop/arel.rb
|
146
|
+
- lib/search_cop/arel/visitors.rb
|
147
|
+
- lib/search_cop/grammar_parser.rb
|
148
|
+
- lib/search_cop/hash_parser.rb
|
149
|
+
- lib/search_cop/query_builder.rb
|
150
|
+
- lib/search_cop/query_info.rb
|
151
|
+
- lib/search_cop/search_scope.rb
|
152
|
+
- lib/search_cop/version.rb
|
153
|
+
- lib/search_cop_grammar.rb
|
154
|
+
- lib/search_cop_grammar.treetop
|
155
|
+
- lib/search_cop_grammar/attributes.rb
|
156
|
+
- lib/search_cop_grammar/nodes.rb
|
157
|
+
- search_cop.gemspec
|
158
|
+
- test/and_test.rb
|
159
|
+
- test/boolean_test.rb
|
160
|
+
- test/database.yml
|
161
|
+
- test/date_test.rb
|
162
|
+
- test/datetime_test.rb
|
163
|
+
- test/error_test.rb
|
164
|
+
- test/float_test.rb
|
165
|
+
- test/fulltext_test.rb
|
166
|
+
- test/hash_test.rb
|
167
|
+
- test/integer_test.rb
|
168
|
+
- test/not_test.rb
|
169
|
+
- test/or_test.rb
|
170
|
+
- test/scope_test.rb
|
171
|
+
- test/search_cop_test.rb
|
172
|
+
- test/string_test.rb
|
173
|
+
- test/test_helper.rb
|
174
|
+
homepage: https://github.com/mrkamel/search_cop
|
175
|
+
licenses:
|
176
|
+
- MIT
|
177
|
+
post_install_message:
|
178
|
+
rdoc_options: []
|
179
|
+
require_paths:
|
180
|
+
- lib
|
181
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
182
|
+
none: false
|
183
|
+
requirements:
|
184
|
+
- - ! '>='
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
version: '0'
|
187
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
188
|
+
none: false
|
189
|
+
requirements:
|
190
|
+
- - ! '>='
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
version: '0'
|
193
|
+
requirements: []
|
194
|
+
rubyforge_project:
|
195
|
+
rubygems_version: 1.8.23
|
196
|
+
signing_key:
|
197
|
+
specification_version: 3
|
198
|
+
summary: Easily perform complex search engine like fulltext queries on your ActiveRecord
|
199
|
+
models
|
200
|
+
test_files:
|
201
|
+
- test/and_test.rb
|
202
|
+
- test/boolean_test.rb
|
203
|
+
- test/database.yml
|
204
|
+
- test/date_test.rb
|
205
|
+
- test/datetime_test.rb
|
206
|
+
- test/error_test.rb
|
207
|
+
- test/float_test.rb
|
208
|
+
- test/fulltext_test.rb
|
209
|
+
- test/hash_test.rb
|
210
|
+
- test/integer_test.rb
|
211
|
+
- test/not_test.rb
|
212
|
+
- test/or_test.rb
|
213
|
+
- test/scope_test.rb
|
214
|
+
- test/search_cop_test.rb
|
215
|
+
- test/string_test.rb
|
216
|
+
- test/test_helper.rb
|