multi_column_search 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1 @@
1
+ v0.0.1. Initial release.
data/Manifest ADDED
@@ -0,0 +1,9 @@
1
+ CHANGELOG
2
+ Manifest
3
+ README.rdoc
4
+ Rakefile
5
+ init.rb
6
+ lib/multi_column_search.rb
7
+ spec/models.rb
8
+ spec/multi_column_search_spec.rb
9
+ spec/spec_helper.rb
data/README.rdoc ADDED
@@ -0,0 +1,36 @@
1
+ = Multi Column Search
2
+
3
+ Quick and dirty multi column LIKE searches. Need to search accross multiple columns, like company(name, address, city, state, zip)
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 multi_column_search with the fields you want to search.
10
+
11
+ class Company
12
+ multi_column_search :name, :address, :city, :state, :zip
13
+ end
14
+
15
+ Search for a single or multi value across all searched columns.
16
+
17
+ Company.search('Sam Comp') # => any company with name, address, city, state, zip containing with Sam Comp
18
+
19
+ == Install
20
+
21
+ Rails 3 Gem
22
+
23
+ # add to your gemfile in bundler
24
+ bundle install
25
+
26
+
27
+ == Docs
28
+
29
+ http://rdoc.info/projects/jackhq/multi_column_search
30
+
31
+ Homepage:: http://github.com/jackhq/multi_column_search
32
+ License:: Copyright (c) 2010 Jack Russell Software Company <mailto:team@jackrussellsoftware.com>, released under the MIT license.
33
+
34
+ == Credits
35
+
36
+ Completely inspired from "simple_column_search" by Elijah Miller : http://github.com/jqr/simple_column_search
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ #require 'spec/rake/spectask'
2
+
3
+ require 'echoe'
4
+ Echoe.new 'multi_column_search' do |p|
5
+ p.description = "Quick and dirty multi column LIKE searches."
6
+ p.url = "http://github.com/jackhq/multi_column_search"
7
+ p.author = "Jack Russell Software"
8
+ p.email = "team@jackrussellsoftware.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,3 @@
1
+ require 'multi_column_search'
2
+
3
+ ActiveRecord::Base.extend(MultiColumnSearch)
@@ -0,0 +1,53 @@
1
+ require 'active_record'
2
+
3
+ module MultiColumnSearch
4
+ # Adds a Model.search('term1 term2') method that searches across SEARCH_COLUMNS
5
+ # for ORed across columns.
6
+ #
7
+ # class Company
8
+ # multi_column_search :name, :address, :city, :state, :zip, :phone
9
+ # end
10
+ #
11
+ # Company.search('Jack Russ') # => any company, street, city that match Jack Russ
12
+ # Company.search('29464') # => any company with zip code 29464
13
+ # Company.search('Mount P')
14
+ # # => any company with 'Mount P' in the city
15
+ def multi_column_search(*args)
16
+ options = args.extract_options!
17
+ columns = args
18
+
19
+ options[:match] ||= :start
20
+ options[:name] ||= 'search'
21
+
22
+ # PostgreSQL LIKE is case-sensitive, use ILIKE for case-insensitive
23
+ like = connection.adapter_name == "PostgreSQL" ? "ILIKE" : "LIKE"
24
+ scope options[:name], lambda { |terms|
25
+ terms = terms.split.join('%')
26
+ conditions = terms.split.inject([]) do |acc, term|
27
+ pattern = get_multi_column_pattern options[:match], term
28
+ acc << columns.collect { |column| "#{table_name}.#{column} #{like} '#{pattern}'" }
29
+
30
+ end
31
+ conditions = conditions.map { |c| "(" + c.join(' OR ') + ")" }.join(' OR ')
32
+ puts conditions
33
+ where conditions
34
+ }
35
+ end
36
+
37
+ def get_multi_column_pattern(match, term)
38
+ case(match)
39
+ when :exact
40
+ term
41
+ when :start
42
+ term + '%'
43
+ when :middle
44
+ '%' + term + '%'
45
+ when :end
46
+ '%' + term
47
+ else
48
+ raise "Unexpected match type: #{options[:match]}"
49
+ end
50
+ end
51
+
52
+
53
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{multi_column_search}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Jack Russell Software"]
9
+ s.date = %q{2010-08-18}
10
+ s.description = %q{Quick and dirty multi column LIKE searches.}
11
+ s.email = %q{team@jackrussellsoftware.com}
12
+ s.extra_rdoc_files = ["CHANGELOG", "README.rdoc", "lib/multi_column_search.rb"]
13
+ s.files = ["CHANGELOG", "Manifest", "README.rdoc", "Rakefile", "init.rb", "lib/multi_column_search.rb", "spec/models.rb", "spec/multi_column_search_spec.rb", "spec/spec_helper.rb", "multi_column_search.gemspec"]
14
+ s.homepage = %q{http://github.com/jackhq/multi_column_search}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Multi_column_search", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{multi_column_search}
18
+ s.rubygems_version = %q{1.3.7}
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::VERSION) >= 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,4 @@
1
+ class Sponsor < ActiveRecord::Base
2
+ multi_column_search :name, :address, :city, :state, :zip, :phone, :fax
3
+
4
+ end
@@ -0,0 +1,30 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe MultiColumnSearch do
4
+ before(:each) do
5
+ Sponsor.delete_all
6
+
7
+ @jrs = Sponsor.create(:name => 'Jack Russell Software', :address => '1067 Cliffwood Drive', :city => 'Mount Pleasant', :state => 'SC', :zip => '29464', :phone => '843.628.0463', :fax => '843.388.9952')
8
+ @wal = Sponsor.create(:name => 'Walmart Company', :address => '333 Sam Sneed Drive', :city => 'Mount Pleasant', :state => 'SC', :zip => '29464', :phone => '843-628-4321', :fax => '843.388.9952')
9
+ @musc = Sponsor.create(:name => 'MUSC Health', :address => '1 Downtown Street', :city => 'Charleston', :state => 'SC', :zip => '29401', :phone => '843-555-0463', :fax => '843.388.9952')
10
+
11
+ @sponsors = [@jrs, @wal, @musc]
12
+ end
13
+
14
+ describe "multi column search default match" do
15
+ it "should find sponsors by zip" do
16
+ Sponsor.search('2946').count.should == 2
17
+ end
18
+
19
+ it "should find sponsors by address" do
20
+ Sponsor.search('333 Sam').count.should == 1
21
+ end
22
+
23
+ it "should find sponsors by city" do
24
+ Sponsor.search('Mount P').count.should == 2
25
+ end
26
+
27
+ end
28
+
29
+
30
+ end
@@ -0,0 +1,41 @@
1
+ require 'rspec'
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 :sponsors, :force => true do |t|
27
+ t.string :name
28
+ t.string :address
29
+ t.string :city
30
+ t.string :state
31
+ t.string :zip
32
+ t.string :phone
33
+ t.string :fax
34
+
35
+ t.timestamps
36
+ end
37
+ end
38
+
39
+ setup_database
40
+
41
+ require File.join(File.dirname(__FILE__), 'models')
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_column_search
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Jack Russell Software
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-18 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Quick and dirty multi column LIKE searches.
22
+ email: team@jackrussellsoftware.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - CHANGELOG
29
+ - README.rdoc
30
+ - lib/multi_column_search.rb
31
+ files:
32
+ - CHANGELOG
33
+ - Manifest
34
+ - README.rdoc
35
+ - Rakefile
36
+ - init.rb
37
+ - lib/multi_column_search.rb
38
+ - spec/models.rb
39
+ - spec/multi_column_search_spec.rb
40
+ - spec/spec_helper.rb
41
+ - multi_column_search.gemspec
42
+ has_rdoc: true
43
+ homepage: http://github.com/jackhq/multi_column_search
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --line-numbers
49
+ - --inline-source
50
+ - --title
51
+ - Multi_column_search
52
+ - --main
53
+ - README.rdoc
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 1
71
+ - 2
72
+ version: "1.2"
73
+ requirements: []
74
+
75
+ rubyforge_project: multi_column_search
76
+ rubygems_version: 1.3.7
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Quick and dirty multi column LIKE searches.
80
+ test_files: []
81
+