japetheape-scoped_search 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +78 -0
- data/Rakefile +5 -0
- data/init.rb +1 -0
- data/lib/scoped_search.rb +108 -0
- data/lib/scoped_search/query_conditions_builder.rb +209 -0
- data/lib/scoped_search/query_language_parser.rb +117 -0
- data/lib/scoped_search/reg_tokens.rb +51 -0
- data/tasks/database_tests.rake +20 -0
- data/tasks/documentation.rake +33 -0
- data/tasks/github-gem.rake +250 -0
- data/test/database.yml +17 -0
- data/test/integration/api_test.rb +53 -0
- data/test/lib/test_models.rb +148 -0
- data/test/lib/test_schema.rb +68 -0
- data/test/test_helper.rb +44 -0
- data/test/unit/query_conditions_builder_test.rb +410 -0
- data/test/unit/query_language_test.rb +155 -0
- data/test/unit/search_for_test.rb +124 -0
- metadata +89 -0
@@ -0,0 +1,51 @@
|
|
1
|
+
# Regular expression tokens to be used for parsing.
|
2
|
+
module RegTokens
|
3
|
+
|
4
|
+
WORD = '[\w-]+'
|
5
|
+
SPACE = '[ ]'
|
6
|
+
STRING = '["][\w ]+["]'
|
7
|
+
OR = 'OR'
|
8
|
+
POSSIBLY_NEGATED = '[-]?'
|
9
|
+
MONTH = '[\d]{1,2}'
|
10
|
+
DAY = '[\d]{1,2}'
|
11
|
+
FULL_YEAR = '[\d]{4}'
|
12
|
+
LESS_THAN = '[<][ ]'
|
13
|
+
GREATER_THAN = '[>][ ]'
|
14
|
+
LESS_THAN_OR_EQUAL_TO = '[<][=][ ]'
|
15
|
+
GREATER_THAN_OR_EQUAL_TO = '[>][=][ ]'
|
16
|
+
TO = 'TO'
|
17
|
+
|
18
|
+
WordOrWord = "(#{WORD}#{SPACE}#{OR}#{SPACE}#{WORD})"
|
19
|
+
WordOrString = "(#{WORD}#{SPACE}#{OR}#{SPACE}#{STRING})"
|
20
|
+
StringOrWord = "(#{STRING}#{SPACE}#{OR}#{SPACE}#{WORD})"
|
21
|
+
StringOrString = "(#{STRING}#{SPACE}#{OR}#{SPACE}#{STRING})"
|
22
|
+
PossiblyNegatedWord = "(#{POSSIBLY_NEGATED}#{WORD})"
|
23
|
+
PossiblyNegatedString = "(#{POSSIBLY_NEGATED}#{STRING})"
|
24
|
+
|
25
|
+
DateFormatMMDDYYYY = "(#{MONTH}/#{DAY}/#{FULL_YEAR})" # This would be the same for DD/MM/YYYY
|
26
|
+
DateFormatYYYYMMDD = "(#{FULL_YEAR}/#{MONTH}/#{DAY})"
|
27
|
+
DatabaseFormat = "(#{FULL_YEAR}-#{MONTH}-#{DAY})"
|
28
|
+
|
29
|
+
LessThanDateFormatMMDDYYYY = "(#{LESS_THAN}#{MONTH}/#{DAY}/#{FULL_YEAR})"
|
30
|
+
LessThanDateFormatYYYYMMDD = "(#{LESS_THAN}#{FULL_YEAR}/#{MONTH}/#{DAY})"
|
31
|
+
LessThanDatabaseFormat = "(#{LESS_THAN}#{FULL_YEAR}-#{MONTH}-#{DAY})"
|
32
|
+
|
33
|
+
GreaterThanDateFormatMMDDYYYY = "(#{GREATER_THAN}#{MONTH}/#{DAY}/#{FULL_YEAR})"
|
34
|
+
GreaterThanDateFormatYYYYMMDD = "(#{GREATER_THAN}#{FULL_YEAR}/#{MONTH}/#{DAY})"
|
35
|
+
GreaterThanDatabaseFormat = "(#{GREATER_THAN}#{FULL_YEAR}-#{MONTH}-#{DAY})"
|
36
|
+
|
37
|
+
LessThanOrEqualToDateFormatMMDDYYYY = "(#{LESS_THAN_OR_EQUAL_TO}#{MONTH}/#{DAY}/#{FULL_YEAR})"
|
38
|
+
LessThanOrEqualToDateFormatYYYYMMDD = "(#{LESS_THAN_OR_EQUAL_TO}#{FULL_YEAR}/#{MONTH}/#{DAY})"
|
39
|
+
LessThanOrEqualToDatabaseFormat = "(#{LESS_THAN_OR_EQUAL_TO}#{FULL_YEAR}-#{MONTH}-#{DAY})"
|
40
|
+
|
41
|
+
GreaterThanOrEqualToDateFormatMMDDYYYY = "(#{GREATER_THAN_OR_EQUAL_TO}#{MONTH}/#{DAY}/#{FULL_YEAR})"
|
42
|
+
GreaterThanOrEqualToDateFormatYYYYMMDD = "(#{GREATER_THAN_OR_EQUAL_TO}#{FULL_YEAR}/#{MONTH}/#{DAY})"
|
43
|
+
GreaterThanOrEqualToDatabaseFormat = "(#{GREATER_THAN_OR_EQUAL_TO}#{FULL_YEAR}-#{MONTH}-#{DAY})"
|
44
|
+
|
45
|
+
BetweenDateFormatMMDDYYYY = "(#{MONTH}/#{DAY}/#{FULL_YEAR}#{SPACE}#{TO}#{SPACE}#{MONTH}/#{DAY}/#{FULL_YEAR})"
|
46
|
+
BetweenDateFormatYYYYMMDD = "(#{FULL_YEAR}/#{MONTH}/#{DAY}#{SPACE}#{TO}#{SPACE}#{FULL_YEAR}/#{MONTH}/#{DAY})"
|
47
|
+
BetweenDatabaseFormat = "(#{FULL_YEAR}-#{MONTH}-#{DAY}#{SPACE}#{TO}#{SPACE}#{FULL_YEAR}-#{MONTH}-#{DAY})"
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'yaml' unless Object::const_defined?('YAML')
|
2
|
+
|
3
|
+
namespace :test do
|
4
|
+
|
5
|
+
databases = YAML.load(File.read(File.dirname(__FILE__) + '/../test/database.yml'))
|
6
|
+
|
7
|
+
desc "Run testsuite on all configured databases in test/database.yml"
|
8
|
+
task(:all => databases.keys.map { |db| db.to_sym }) do
|
9
|
+
puts "\nFinished testing on all configured databases!"
|
10
|
+
puts "(Configure databases by adjusting test/database.yml)"
|
11
|
+
end
|
12
|
+
|
13
|
+
databases.each do |database, config|
|
14
|
+
desc "Run testsuite on #{database} database."
|
15
|
+
task database.to_sym do
|
16
|
+
puts "Running testsuite on #{database} database...\n\n"
|
17
|
+
sh "rake test DATABASE=#{database}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
|
4
|
+
desc 'Generate documentation for the acts_as_callback_logger plugin.'
|
5
|
+
Rake::RDocTask.new do |rdoc|
|
6
|
+
rdoc.rdoc_dir = 'doc/html'
|
7
|
+
rdoc.title = 'scoped_search'
|
8
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
9
|
+
rdoc.main = 'README'
|
10
|
+
rdoc.rdoc_files.include('LICENSE',
|
11
|
+
'lib/')
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
begin
|
16
|
+
require 'rcov/rcovtask'
|
17
|
+
Rcov::RcovTask.new do |t|
|
18
|
+
t.test_files = Dir[ "test/**/*_test.rb" ]
|
19
|
+
end
|
20
|
+
rescue LoadError
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
|
24
|
+
begin
|
25
|
+
require 'rcov/rcovtask'
|
26
|
+
desc 'Runs spec:rcov and then displays the coverage/index.html file in the browswer.'
|
27
|
+
task :rcov_display => [:clobber_rcov, :rcov] do
|
28
|
+
system("open coverage/index.html")
|
29
|
+
end
|
30
|
+
rescue LoadError
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,250 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rubyforge'
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/tasklib'
|
5
|
+
require 'date'
|
6
|
+
|
7
|
+
module Rake
|
8
|
+
|
9
|
+
class GithubGem < TaskLib
|
10
|
+
|
11
|
+
attr_accessor :name
|
12
|
+
attr_accessor :specification
|
13
|
+
|
14
|
+
def self.define_tasks!
|
15
|
+
gem_task_builder = Rake::GithubGem.new
|
16
|
+
gem_task_builder.register_all_tasks!
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
reload_gemspec!
|
22
|
+
end
|
23
|
+
|
24
|
+
def register_all_tasks!
|
25
|
+
namespace(:gem) do
|
26
|
+
desc "Updates the file lists for this gem"
|
27
|
+
task(:manifest) { manifest_task }
|
28
|
+
|
29
|
+
desc "Releases a new version of #{@name}"
|
30
|
+
task(:build => [:manifest]) { build_task }
|
31
|
+
|
32
|
+
|
33
|
+
release_dependencies = [:check_clean_master_branch, :version, :build, :create_tag]
|
34
|
+
release_dependencies.push 'doc:publish' if has_rdoc?
|
35
|
+
release_dependencies.unshift 'test' if has_tests?
|
36
|
+
release_dependencies.unshift 'spec' if has_specs?
|
37
|
+
|
38
|
+
desc "Releases a new version of #{@name}"
|
39
|
+
task(:release => release_dependencies) { release_task }
|
40
|
+
|
41
|
+
# helper task for releasing
|
42
|
+
task(:check_clean_master_branch) { verify_clean_status('master') }
|
43
|
+
task(:check_version) { verify_version(ENV['VERSION'] || @specification.version) }
|
44
|
+
task(:version => [:check_version]) { set_gem_version! }
|
45
|
+
task(:create_tag) { create_version_tag! }
|
46
|
+
end
|
47
|
+
|
48
|
+
# Register RDoc tasks
|
49
|
+
if has_rdoc?
|
50
|
+
require 'rake/rdoctask'
|
51
|
+
|
52
|
+
namespace(:doc) do
|
53
|
+
desc 'Generate documentation for request-log-analyzer'
|
54
|
+
Rake::RDocTask.new(:compile) do |rdoc|
|
55
|
+
rdoc.rdoc_dir = 'doc'
|
56
|
+
rdoc.title = @name
|
57
|
+
rdoc.options += @specification.rdoc_options
|
58
|
+
rdoc.rdoc_files.include(@specification.extra_rdoc_files)
|
59
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
60
|
+
end
|
61
|
+
|
62
|
+
desc "Publish RDoc files for #{@name} to Github"
|
63
|
+
task(:publish => :compile) do
|
64
|
+
sh 'git checkout gh-pages'
|
65
|
+
sh 'git pull origin gh-pages'
|
66
|
+
sh 'cp -rf doc/* .'
|
67
|
+
sh "git commit -am \"Publishing newest RDoc documentation for #{@name}\""
|
68
|
+
sh "git push origin gh-pages"
|
69
|
+
sh "git checkout master"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Setup :spec task if RSpec files exist
|
75
|
+
if has_specs?
|
76
|
+
require 'spec/rake/spectask'
|
77
|
+
|
78
|
+
desc "Run all specs for #{@name}"
|
79
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
80
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Setup :test task if unit test files exist
|
85
|
+
if has_tests?
|
86
|
+
require 'rake/testtask'
|
87
|
+
|
88
|
+
desc "Run all unit tests for #{@name}"
|
89
|
+
Rake::TestTask.new(:test) do |t|
|
90
|
+
t.pattern = 'test/**/*_test.rb'
|
91
|
+
t.verbose = true
|
92
|
+
t.libs << 'test'
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
protected
|
98
|
+
|
99
|
+
def has_rdoc?
|
100
|
+
@specification.has_rdoc
|
101
|
+
end
|
102
|
+
|
103
|
+
def has_specs?
|
104
|
+
Dir['spec/**/*_spec.rb'].any?
|
105
|
+
end
|
106
|
+
|
107
|
+
def has_tests?
|
108
|
+
Dir['test/**/*_test.rb'].any?
|
109
|
+
end
|
110
|
+
|
111
|
+
def reload_gemspec!
|
112
|
+
raise "No gemspec file found!" if gemspec_file.nil?
|
113
|
+
spec = File.read(gemspec_file)
|
114
|
+
@specification = eval(spec)
|
115
|
+
@name = specification.name
|
116
|
+
end
|
117
|
+
|
118
|
+
def run_command(command)
|
119
|
+
lines = []
|
120
|
+
IO.popen(command) { |f| lines = f.readlines }
|
121
|
+
return lines
|
122
|
+
end
|
123
|
+
|
124
|
+
def git_modified?(file)
|
125
|
+
return !run_command('git status').detect { |line| Regexp.new(Regexp.quote(file)) =~ line }.nil?
|
126
|
+
end
|
127
|
+
|
128
|
+
def git_commit_file(file, message, branch = nil)
|
129
|
+
verify_current_branch(branch) unless branch.nil?
|
130
|
+
if git_modified?(file)
|
131
|
+
sh "git add #{file}"
|
132
|
+
sh "git commit -m \"#{message}\""
|
133
|
+
else
|
134
|
+
raise "#{file} is not modified and cannot be committed!"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def git_create_tag(tag_name, message)
|
139
|
+
sh "git tag -a \"#{tag_name}\" -m \"#{message}\""
|
140
|
+
end
|
141
|
+
|
142
|
+
def git_push(remote = 'origin', branch = 'master', options = [])
|
143
|
+
verify_clean_status(branch)
|
144
|
+
options_str = options.map { |o| "--#{o}"}.join(' ')
|
145
|
+
sh "git push #{options_str} #{remote} #{branch}"
|
146
|
+
end
|
147
|
+
|
148
|
+
def gemspec_version=(new_version)
|
149
|
+
spec = File.read(gemspec_file)
|
150
|
+
spec.gsub!(/^(\s*s\.version\s*=\s*)('|")(.+)('|")(\s*)$/) { "#{$1}'#{new_version}'#{$5}" }
|
151
|
+
spec.gsub!(/^(\s*s\.date\s*=\s*)('|")(.+)('|")(\s*)$/) { "#{$1}'#{Date.today.strftime('%Y-%m-%d')}'#{$5}" }
|
152
|
+
File.open(gemspec_file, 'w') { |f| f << spec }
|
153
|
+
reload_gemspec!
|
154
|
+
end
|
155
|
+
|
156
|
+
def gemspec_date=(new_date)
|
157
|
+
spec = File.read(gemspec_file)
|
158
|
+
spec.gsub!(/^(\s*s\.date\s*=\s*)('|")(.+)('|")(\s*)$/) { "#{$1}'#{new_date.strftime('%Y-%m-%d')}'#{$5}" }
|
159
|
+
File.open(gemspec_file, 'w') { |f| f << spec }
|
160
|
+
reload_gemspec!
|
161
|
+
end
|
162
|
+
|
163
|
+
def gemspec_file
|
164
|
+
@gemspec_file ||= Dir['*.gemspec'].first
|
165
|
+
end
|
166
|
+
|
167
|
+
def verify_current_branch(branch)
|
168
|
+
run_command('git branch').detect { |line| /^\* (.+)/ =~ line }
|
169
|
+
raise "You are currently not working in the master branch!" unless branch == $1
|
170
|
+
end
|
171
|
+
|
172
|
+
def verify_clean_status(on_branch = nil)
|
173
|
+
sh "git fetch"
|
174
|
+
lines = run_command('git status')
|
175
|
+
raise "You don't have the most recent version available. Run git pull first." if /^\# Your branch is behind/ =~ lines[1]
|
176
|
+
raise "You are currently not working in the #{on_branch} branch!" unless on_branch.nil? || (/^\# On branch (.+)/ =~ lines.first && $1 == on_branch)
|
177
|
+
raise "Your master branch contains modifications!" unless /^nothing to commit \(working directory clean\)/ =~ lines.last
|
178
|
+
end
|
179
|
+
|
180
|
+
def verify_version(new_version)
|
181
|
+
newest_version = run_command('git tag').map { |tag| tag.split(name + '-').last }.compact.map { |v| Gem::Version.new(v) }.max
|
182
|
+
raise "This version number (#{new_version}) is not higher than the highest tagged version (#{newest_version})" if !newest_version.nil? && newest_version >= Gem::Version.new(new_version.to_s)
|
183
|
+
end
|
184
|
+
|
185
|
+
def set_gem_version!
|
186
|
+
# update gemspec file
|
187
|
+
self.gemspec_version = ENV['VERSION'] if Gem::Version.correct?(ENV['VERSION'])
|
188
|
+
self.gemspec_date = Date.today
|
189
|
+
end
|
190
|
+
|
191
|
+
def manifest_task
|
192
|
+
verify_current_branch('master')
|
193
|
+
|
194
|
+
list = Dir['**/*'].sort
|
195
|
+
list -= [gemspec_file]
|
196
|
+
|
197
|
+
if File.exist?('.gitignore')
|
198
|
+
File.read('.gitignore').each_line do |glob|
|
199
|
+
glob = glob.chomp.sub(/^\//, '')
|
200
|
+
list -= Dir[glob]
|
201
|
+
list -= Dir["#{glob}/**/*"] if File.directory?(glob) and !File.symlink?(glob)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
# update the spec file
|
206
|
+
spec = File.read(gemspec_file)
|
207
|
+
spec.gsub! /^(\s* s.(test_)?files \s* = \s* )( \[ [^\]]* \] | %w\( [^)]* \) )/mx do
|
208
|
+
assignment = $1
|
209
|
+
bunch = $2 ? list.grep(/^(test.*_test\.rb|spec.*_spec.rb)$/) : list
|
210
|
+
'%s%%w(%s)' % [assignment, bunch.join(' ')]
|
211
|
+
end
|
212
|
+
|
213
|
+
File.open(gemspec_file, 'w') { |f| f << spec }
|
214
|
+
reload_gemspec!
|
215
|
+
end
|
216
|
+
|
217
|
+
def build_task
|
218
|
+
sh "gem build #{gemspec_file}"
|
219
|
+
Dir.mkdir('pkg') unless File.exist?('pkg')
|
220
|
+
sh "mv #{name}-#{specification.version}.gem pkg/#{name}-#{specification.version}.gem"
|
221
|
+
end
|
222
|
+
|
223
|
+
def install_task
|
224
|
+
raise "#{name} .gem file not found" unless File.exist?("pkg/#{name}-#{specification.version}.gem")
|
225
|
+
sh "gem install pkg/#{name}-#{specification.version}.gem"
|
226
|
+
end
|
227
|
+
|
228
|
+
def uninstall_task
|
229
|
+
raise "#{name} .gem file not found" unless File.exist?("pkg/#{name}-#{specification.version}.gem")
|
230
|
+
sh "gem uninstall #{name}"
|
231
|
+
end
|
232
|
+
|
233
|
+
def create_version_tag!
|
234
|
+
# commit the gemspec file
|
235
|
+
git_commit_file(gemspec_file, "Released #{@name} version #{@specification.version}") if git_modified?(gemspec_file)
|
236
|
+
|
237
|
+
# create tag and push changes
|
238
|
+
git_create_tag("#{@name}-#{@specification.version}", "Tagged #{@name} version #{@specification.version}")
|
239
|
+
git_push('origin', 'master', [:tags])
|
240
|
+
end
|
241
|
+
|
242
|
+
def release_task
|
243
|
+
puts
|
244
|
+
puts '------------------------------------------------------------'
|
245
|
+
puts "Released #{@name} - version #{@specification.version}"
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
Rake::GithubGem.define_tasks!
|
data/test/database.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
sqlite3:
|
2
|
+
adapter: "sqlite3"
|
3
|
+
database: ":memory:"
|
4
|
+
|
5
|
+
mysql:
|
6
|
+
adapter: "mysql"
|
7
|
+
host: "localhost"
|
8
|
+
user: "root"
|
9
|
+
password:
|
10
|
+
database: "scoped_search_test"
|
11
|
+
|
12
|
+
postgresql:
|
13
|
+
adapter: "postgresql"
|
14
|
+
host: "localhost"
|
15
|
+
username: "sstest"
|
16
|
+
password: "sstest"
|
17
|
+
database: "scoped_search_test"
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/../test_helper.rb"
|
2
|
+
|
3
|
+
class ScopedSearch::Test::API < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def self.const_missing(const)
|
6
|
+
ScopedSearch::Test::Models.const_get(const)
|
7
|
+
end
|
8
|
+
|
9
|
+
def setup
|
10
|
+
ScopedSearch::Test::establish_connection
|
11
|
+
ScopedSearch::Test::DatabaseSchema.up
|
12
|
+
end
|
13
|
+
|
14
|
+
def teardown
|
15
|
+
ScopedSearch::Test::DatabaseSchema.down
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_enabling
|
19
|
+
assert !Foo.respond_to?(:search_for)
|
20
|
+
Foo.searchable_on :string_field, :text_field, :date_field
|
21
|
+
assert Foo.respond_to?(:search_for)
|
22
|
+
|
23
|
+
assert_equal ActiveRecord::NamedScope::Scope, Foo.search_for('test').class
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_search_only_fields
|
27
|
+
Foo.searchable_on :only => [:string_field, :text_field, :date_field]
|
28
|
+
assert Foo.respond_to?(:search_for)
|
29
|
+
assert_equal Foo.scoped_search_fields.size, 3
|
30
|
+
assert Foo.scoped_search_fields.include?(:string_field)
|
31
|
+
assert Foo.scoped_search_fields.include?(:text_field)
|
32
|
+
assert Foo.scoped_search_fields.include?(:date_field)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_search_except_fields
|
36
|
+
Foo.searchable_on :except => [:id, :ignored_field, :created_at, :updated_at]
|
37
|
+
assert Foo.respond_to?(:search_for)
|
38
|
+
assert_equal Foo.scoped_search_fields.size, 4
|
39
|
+
assert Foo.scoped_search_fields.include?(:string_field)
|
40
|
+
assert Foo.scoped_search_fields.include?(:text_field)
|
41
|
+
assert Foo.scoped_search_fields.include?(:date_field)
|
42
|
+
assert Foo.scoped_search_fields.include?(:some_int_field)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_search_with_only_and_except
|
46
|
+
# :except should be ignored if :only is specified.
|
47
|
+
Foo.searchable_on({:only => [:text_field], :except => [:text_field]})
|
48
|
+
assert Foo.respond_to?(:search_for)
|
49
|
+
assert_equal Foo.scoped_search_fields.size, 1
|
50
|
+
assert Foo.scoped_search_fields.include?(:text_field), ':except should be ignored if :only is specified'
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
module ScopedSearch::Test::Models
|
2
|
+
|
3
|
+
class Foo < ActiveRecord::Base
|
4
|
+
def self.create_corpus!
|
5
|
+
create!(:string_field => "Programmer 123", :text_field => nil, :ignored_field => "123456", :some_int_field => 111, :date_field => '2000-01-01')
|
6
|
+
create!(:string_field => "Jim", :text_field => "Henson", :ignored_field => "123456a", :some_int_field => 222, :date_field => '2001-04-15')
|
7
|
+
create!(:string_field => "Jim", :text_field => "Bush", :ignored_field => "123456b", :some_int_field => 333, :date_field => '2001-04-17')
|
8
|
+
create!(:string_field => "Wes", :text_field => "Hays", :ignored_field => "123456c", :some_int_field => 444, :date_field => '1980-09-27')
|
9
|
+
create!(:string_field => "Bob", :text_field => "Hays", :ignored_field => "123456d", :some_int_field => 555, :date_field => '2002-11-09')
|
10
|
+
create!(:string_field => "Dogs", :text_field => "Pit Bull", :ignored_field => "123456e", :some_int_field => 666, :date_field => '2002-12-26')
|
11
|
+
create!(:string_field => "Dogs", :text_field => "Eskimo", :ignored_field => "123456f", :some_int_field => 777, :date_field => '2003-03-19')
|
12
|
+
create!(:string_field => "Cows", :text_field => "Farms", :ignored_field => "123456g", :some_int_field => 888, :date_field => '2004-05-01')
|
13
|
+
create!(:string_field => "Hello World", :text_field => "Hello Moon", :ignored_field => "123456h", :some_int_field => 999, :date_field => '2004-07-11')
|
14
|
+
create!(:string_field => "Hello World", :text_field => "Goodnight Moon", :ignored_field => "123456i", :some_int_field => 100, :date_field => '2004-09-12')
|
15
|
+
create!(:string_field => "Happy Cow", :text_field => "Sad Cow", :ignored_field => "123456j", :some_int_field => 200, :date_field => '2005-02-05')
|
16
|
+
create!(:string_field => "Happy Frog", :text_field => "Sad Frog", :ignored_field => "123456k", :some_int_field => 300, :date_field => '2006-03-09')
|
17
|
+
create!(:string_field => "Excited Frog", :text_field => "Sad Frog", :ignored_field => "123456l", :some_int_field => 400, :date_field => '2006-07-15')
|
18
|
+
create!(:string_field => "Man made", :text_field => "Woman made", :ignored_field => "123456m", :some_int_field => 500, :date_field => '2007-06-13')
|
19
|
+
create!(:string_field => "Cat Toys", :text_field => "Frog Toys", :ignored_field => "123456n", :some_int_field => 600, :date_field => '2008-03-04')
|
20
|
+
create!(:string_field => "Happy Toys", :text_field => "Sad Toys", :ignored_field => "123456n", :some_int_field => 700, :date_field => '2008-05-12')
|
21
|
+
|
22
|
+
create!(:string_field => "My son was born on 7/15/2006 and weighed 5.5 lbs",
|
23
|
+
:text_field => "Sad Toys",
|
24
|
+
:ignored_field => "123456n",
|
25
|
+
:date_field => '2008-09-22')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class User < ActiveRecord::Base
|
30
|
+
belongs_to :group
|
31
|
+
belongs_to :address
|
32
|
+
has_many :notes
|
33
|
+
has_and_belongs_to_many :locations
|
34
|
+
|
35
|
+
has_many :offices, :dependent => :destroy
|
36
|
+
has_many :clients, :through => :offices
|
37
|
+
|
38
|
+
def self.create_corpus!
|
39
|
+
create!(:first_name => 'Willem', :last_name => 'Van Bergen', :login => 'wvanbergen', :age => 25, :group_id => 1, :address_id => 1)
|
40
|
+
create!(:first_name => 'Wes', :last_name => 'Hays', :login => 'weshays', :age => 26, :group_id => 1, :address_id => 2)
|
41
|
+
create!(:first_name => 'John', :last_name => 'Dell', :login => 'jdell', :age => 27, :group_id => 2, :address_id => 3)
|
42
|
+
create!(:first_name => 'Ray', :last_name => 'York', :login => 'ryork', :age => 28, :group_id => 3, :address_id => 4)
|
43
|
+
create!(:first_name => 'Anna', :last_name => 'Landis', :login => 'alandis', :age => 29, :group_id => 4, :address_id => 5)
|
44
|
+
|
45
|
+
user = self.find_by_first_name('Willem')
|
46
|
+
user.locations << ScopedSearch::Test::Models::Location.find_by_name('Office')
|
47
|
+
|
48
|
+
user = self.find_by_first_name('Wes')
|
49
|
+
user.locations << ScopedSearch::Test::Models::Location.find_by_name('Store')
|
50
|
+
|
51
|
+
user = self.find_by_first_name('John')
|
52
|
+
user.locations << ScopedSearch::Test::Models::Location.find_by_name('Office')
|
53
|
+
|
54
|
+
user = self.find_by_first_name('Ray')
|
55
|
+
user.locations << ScopedSearch::Test::Models::Location.find_by_name('Home')
|
56
|
+
|
57
|
+
user = self.find_by_first_name('Anna')
|
58
|
+
user.locations << ScopedSearch::Test::Models::Location.find_by_name('Beach')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class Client < ActiveRecord::Base
|
63
|
+
has_many :offices, :dependent => :destroy
|
64
|
+
has_many :users, :through => :offices
|
65
|
+
def self.create_corpus!
|
66
|
+
create!(:first_name => 'Bob', :last_name => 'Smith')
|
67
|
+
create!(:first_name => 'Sam', :last_name => 'Lovett')
|
68
|
+
create!(:first_name => 'Sally', :last_name => 'May')
|
69
|
+
create!(:first_name => 'Mary', :last_name => 'Smith')
|
70
|
+
create!(:first_name => 'Darren', :last_name => 'Johnson')
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class Office < ActiveRecord::Base
|
75
|
+
belongs_to :client
|
76
|
+
belongs_to :user
|
77
|
+
def self.create_corpus!
|
78
|
+
create!(:name => 'California Office', :user_id => 1, :client_id => 1)
|
79
|
+
create!(:name => 'California Office', :user_id => 2, :client_id => 2)
|
80
|
+
create!(:name => 'California Office', :user_id => 3, :client_id => 3)
|
81
|
+
create!(:name => 'Reno Office', :user_id => 4, :client_id => 4)
|
82
|
+
create!(:name => 'Reno Office', :user_id => 5, :client_id => 5)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class Group < ActiveRecord::Base
|
87
|
+
has_many :users
|
88
|
+
def self.create_corpus!
|
89
|
+
create!(:name => 'System Administrator')
|
90
|
+
create!(:name => 'Software Managers')
|
91
|
+
create!(:name => 'Office Managers')
|
92
|
+
create!(:name => 'Accounting')
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
class Location < ActiveRecord::Base
|
97
|
+
has_and_belongs_to_many :users
|
98
|
+
def self.create_corpus!
|
99
|
+
create!(:name => 'Home')
|
100
|
+
create!(:name => 'Office')
|
101
|
+
create!(:name => 'Store')
|
102
|
+
create!(:name => 'Beach')
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
class Note < ActiveRecord::Base
|
107
|
+
belongs_to :user
|
108
|
+
def self.create_corpus!
|
109
|
+
wes = ScopedSearch::Test::Models::User.find_by_first_name('Wes')
|
110
|
+
john = ScopedSearch::Test::Models::User.find_by_first_name('John')
|
111
|
+
|
112
|
+
create!(:user_id => wes.id,
|
113
|
+
:title => 'Purchases',
|
114
|
+
:content => "1) Linksys Router. 2) Network Cable")
|
115
|
+
|
116
|
+
create!(:user_id => wes.id,
|
117
|
+
:title => 'Tasks',
|
118
|
+
:content => 'Clean my car, walk the dog and mow the yard buy milk')
|
119
|
+
|
120
|
+
create!(:user_id => wes.id,
|
121
|
+
:title => 'Grocery List',
|
122
|
+
:content => 'milk, gum, apples')
|
123
|
+
|
124
|
+
create!(:user_id => wes.id,
|
125
|
+
:title => 'Stocks to watch',
|
126
|
+
:content => 'MA, AAPL, V and SSO. Straddle MA at 200 with JAN 09 options')
|
127
|
+
|
128
|
+
create!(:user_id => john.id,
|
129
|
+
:title => 'Spec Tests',
|
130
|
+
:content => 'Spec Tests... Spec Tests... Spec Tests!!')
|
131
|
+
|
132
|
+
create!(:user_id => john.id,
|
133
|
+
:title => 'Things To Do',
|
134
|
+
:content => '1) Did I mention Spec Tests!!!, 2) Buy Linksys Router WRT160N')
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
class Address < ActiveRecord::Base
|
139
|
+
has_one :user
|
140
|
+
def self.create_corpus!
|
141
|
+
create!(:street => '800 Haskell St', :city => 'Reno', :state => 'NV', :postal_code => '89509')
|
142
|
+
create!(:street => '2499 Dorchester Rd', :city => 'Charleston', :state => 'SC', :postal_code => '29414')
|
143
|
+
create!(:street => '474 Mallard Way', :city => 'Fernley', :state => 'NV', :postal_code => '89408')
|
144
|
+
create!(:street => '1600 Montero Ct', :city => 'Sparks', :state => 'NV', :postal_code => '89434')
|
145
|
+
create!(:street => '200 4th St', :city => 'Sparks', :state => 'NV', :postal_code => '89434')
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|