simple_column_search 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +13 -0
- data/Manifest +8 -0
- data/README.rdoc +40 -0
- data/Rakefile +18 -0
- data/init.rb +3 -0
- data/lib/simple_column_search.rb +49 -0
- data/simple_column_search.gemspec +30 -0
- data/spec/models.rb +10 -0
- data/spec/spec_helper.rb +36 -0
- metadata +70 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
v1.0.0. PostgreSQL support, Rit Li [rit]
|
2
|
+
|
3
|
+
v0.1.2. Fixed initialization when used as a plugin.
|
4
|
+
|
5
|
+
v0.1.1. Version bump to trigger a GitHub rebuild.
|
6
|
+
|
7
|
+
v0.1.0. Support for specifying match type and specifying name.
|
8
|
+
|
9
|
+
v0.0.3. Plenty of doc cleanups.
|
10
|
+
|
11
|
+
v0.0.2. Added a simpler interface to enable the plugin.
|
12
|
+
|
13
|
+
v0.0.1. Initial release.
|
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
= Simple Column Search
|
2
|
+
|
3
|
+
Quick and dirty multi column LIKE searches. Great for prototyping auto-completers as it handles partial matches and adding search terms refines the results.
|
4
|
+
|
5
|
+
Running lots of queries or have a large data set? You should probably upgrade to a real search back-end already!
|
6
|
+
|
7
|
+
== Examples
|
8
|
+
|
9
|
+
Add a search method to your model by calling simple_column_search with the fields you want to search.
|
10
|
+
|
11
|
+
class User
|
12
|
+
simple_column_search :first_name, :last_name
|
13
|
+
end
|
14
|
+
|
15
|
+
Search for a single value across all searched columns.
|
16
|
+
|
17
|
+
User.search('eli') # => anyone with first or last name starting with eli
|
18
|
+
User.search('miller') # => anyone with first or last name starting with miller
|
19
|
+
|
20
|
+
Refine the search by adding another search term.
|
21
|
+
|
22
|
+
User.search('eli miller')
|
23
|
+
# => anyone with first or last name starting with eli AND
|
24
|
+
# anyone with first or last name starting with miller
|
25
|
+
|
26
|
+
== Install
|
27
|
+
|
28
|
+
As a Rails plugin.
|
29
|
+
|
30
|
+
./script/plugin install git://github.com/jqr/simple_column_search.git
|
31
|
+
|
32
|
+
Prefer gems? Add this to your environment.rb and run the following command.
|
33
|
+
|
34
|
+
config.gem 'simple_column_search', :source => 'http://gemcutter.org'
|
35
|
+
|
36
|
+
$ rake gems:install
|
37
|
+
|
38
|
+
|
39
|
+
Homepage:: http://github.com/jqr/simple_column_search/tree/master
|
40
|
+
License:: Copyright (c) 2008 Elijah Miller <mailto:elijah.miller@gmail.com>, released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec/rake/spectask'
|
2
|
+
|
3
|
+
require 'echoe'
|
4
|
+
Echoe.new 'simple_column_search' do |p|
|
5
|
+
p.description = "Quick and dirty multi column LIKE searches."
|
6
|
+
p.url = "http://github.com/jqr/simple_column_search"
|
7
|
+
p.author = "Elijah Miller"
|
8
|
+
p.email = "elijah.miller@gmail.com"
|
9
|
+
p.retain_gemspec = true
|
10
|
+
p.need_tar_gz = false
|
11
|
+
p.extra_deps = [
|
12
|
+
]
|
13
|
+
p.ignore_pattern = ['spec/test.sqlite3']
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Default: run specs'
|
17
|
+
task :default => :spec
|
18
|
+
task :test => :spec
|
data/init.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'activerecord'
|
3
|
+
|
4
|
+
module SimpleColumnSearch
|
5
|
+
# Adds a Model.search('term1 term2') method that searches across SEARCH_COLUMNS
|
6
|
+
# for ANDed TERMS ORed across columns.
|
7
|
+
#
|
8
|
+
# class User
|
9
|
+
# simple_column_search :first_name, :last_name
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# User.search('elijah') # => anyone with first or last name elijah
|
13
|
+
# User.search('miller') # => anyone with first or last name miller
|
14
|
+
# User.search('elijah miller')
|
15
|
+
# # => anyone with first or last name elijah AND
|
16
|
+
# # anyone with first or last name miller
|
17
|
+
def simple_column_search(*args)
|
18
|
+
options = args.extract_options!
|
19
|
+
columns = args
|
20
|
+
|
21
|
+
options[:match] ||= :start
|
22
|
+
options[:name] ||= 'search'
|
23
|
+
|
24
|
+
# PostgreSQL LIKE is case-sensitive, use ILIKE for case-insensitive
|
25
|
+
like = connection.adapter_name == "PostgreSQL" ? "ILIKE" : "LIKE"
|
26
|
+
|
27
|
+
named_scope options[:name], lambda { |terms|
|
28
|
+
conditions = terms.split.inject(nil) do |acc, term|
|
29
|
+
pattern =
|
30
|
+
case(options[:match])
|
31
|
+
when :exact
|
32
|
+
term
|
33
|
+
when :start
|
34
|
+
term + '%'
|
35
|
+
when :middle
|
36
|
+
'%' + term + '%'
|
37
|
+
when :end
|
38
|
+
'%' + term
|
39
|
+
else
|
40
|
+
raise "Unexpected match type: #{options[:match]}"
|
41
|
+
end
|
42
|
+
merge_conditions acc, [columns.collect { |column| "#{table_name}.#{column} #{like} :pattern" }.join(' OR '), { :pattern => pattern }]
|
43
|
+
end
|
44
|
+
|
45
|
+
{ :conditions => conditions }
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{simple_column_search}
|
5
|
+
s.version = "1.0.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Elijah Miller"]
|
9
|
+
s.date = %q{2009-12-03}
|
10
|
+
s.description = %q{Quick and dirty multi column LIKE searches.}
|
11
|
+
s.email = %q{elijah.miller@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["CHANGELOG", "lib/simple_column_search.rb", "README.rdoc"]
|
13
|
+
s.files = ["CHANGELOG", "init.rb", "lib/simple_column_search.rb", "Manifest", "Rakefile", "README.rdoc", "spec/models.rb", "spec/spec_helper.rb", "simple_column_search.gemspec"]
|
14
|
+
s.homepage = %q{http://github.com/jqr/simple_column_search}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Simple_column_search", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{simple_column_search}
|
18
|
+
s.rubygems_version = %q{1.3.5}
|
19
|
+
s.summary = %q{Quick and dirty multi column LIKE searches.}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
data/spec/models.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
class Person < ActiveRecord::Base
|
2
|
+
simple_column_search :first_name, :last_name, :alias
|
3
|
+
|
4
|
+
simple_column_search :first_name, :name => :search_first_name_default_match
|
5
|
+
|
6
|
+
simple_column_search :first_name, :name => :search_first_name_exact, :match => :exact
|
7
|
+
simple_column_search :first_name, :name => :search_first_name_start, :match => :start
|
8
|
+
simple_column_search :first_name, :name => :search_first_name_middle, :match => :middle
|
9
|
+
simple_column_search :first_name, :name => :search_first_name_end, :match => :end
|
10
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
|
5
|
+
$: << File.join(File.dirname(__FILE__), '..', 'lib')
|
6
|
+
|
7
|
+
require File.join(File.dirname(__FILE__), '..', 'init')
|
8
|
+
|
9
|
+
TEST_DATABASE_FILE = File.join(File.dirname(__FILE__), 'test.sqlite3')
|
10
|
+
|
11
|
+
Spec::Runner.configure do |config|
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
def setup_database
|
16
|
+
File.unlink(TEST_DATABASE_FILE) if File.exist?(TEST_DATABASE_FILE)
|
17
|
+
ActiveRecord::Base.establish_connection(
|
18
|
+
"adapter" => "sqlite3", "timeout" => 5000, "database" => TEST_DATABASE_FILE
|
19
|
+
)
|
20
|
+
create_tables
|
21
|
+
end
|
22
|
+
|
23
|
+
def create_tables
|
24
|
+
c = ActiveRecord::Base.connection
|
25
|
+
|
26
|
+
c.create_table :people, :force => true do |t|
|
27
|
+
t.string :first_name
|
28
|
+
t.string :last_name
|
29
|
+
t.string :alias
|
30
|
+
t.timestamps
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
setup_database
|
35
|
+
|
36
|
+
require File.join(File.dirname(__FILE__), 'models')
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_column_search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Elijah Miller
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-03 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Quick and dirty multi column LIKE searches.
|
17
|
+
email: elijah.miller@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- CHANGELOG
|
24
|
+
- lib/simple_column_search.rb
|
25
|
+
- README.rdoc
|
26
|
+
files:
|
27
|
+
- CHANGELOG
|
28
|
+
- init.rb
|
29
|
+
- lib/simple_column_search.rb
|
30
|
+
- Manifest
|
31
|
+
- Rakefile
|
32
|
+
- README.rdoc
|
33
|
+
- spec/models.rb
|
34
|
+
- spec/spec_helper.rb
|
35
|
+
- simple_column_search.gemspec
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/jqr/simple_column_search
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --line-numbers
|
43
|
+
- --inline-source
|
44
|
+
- --title
|
45
|
+
- Simple_column_search
|
46
|
+
- --main
|
47
|
+
- README.rdoc
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "1.2"
|
61
|
+
version:
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project: simple_column_search
|
65
|
+
rubygems_version: 1.3.5
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Quick and dirty multi column LIKE searches.
|
69
|
+
test_files: []
|
70
|
+
|