texticle 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +10 -0
- data/Manifest.txt +1 -0
- data/README.rdoc +16 -7
- data/lib/texticle.rb +10 -7
- data/lib/texticle/railtie.rb +14 -0
- data/lib/texticle/tasks.rb +8 -4
- data/test/test_texticle.rb +13 -0
- metadata +45 -10
data/CHANGELOG.rdoc
CHANGED
data/Manifest.txt
CHANGED
data/README.rdoc
CHANGED
@@ -14,10 +14,25 @@ named_scope methods making searching easy and fun!
|
|
14
14
|
|
15
15
|
== SYNOPSIS:
|
16
16
|
|
17
|
+
=== Rails 3 Configuration
|
18
|
+
|
19
|
+
In the project's Gemfile add
|
20
|
+
|
21
|
+
gem 'texticle', '1.0.2', :git => 'git://github.com/tenderlove/texticle'
|
22
|
+
|
23
|
+
=== Rails 2 Configuration
|
24
|
+
|
17
25
|
In environment.rb:
|
18
26
|
|
19
27
|
config.gem 'texticle'
|
20
28
|
|
29
|
+
In your Rakefile:
|
30
|
+
|
31
|
+
require 'rubygems'
|
32
|
+
require 'texticle/tasks'
|
33
|
+
|
34
|
+
=== Usage
|
35
|
+
|
21
36
|
Declare your index in your model:
|
22
37
|
|
23
38
|
class Product < ActiveRecord::Base
|
@@ -31,13 +46,7 @@ Use the search method
|
|
31
46
|
|
32
47
|
Product.search('hello world')
|
33
48
|
|
34
|
-
Full text searches can be sped up by creating indexes.
|
35
|
-
indexes, add these lines to your Rakefile:
|
36
|
-
|
37
|
-
require 'rubygems'
|
38
|
-
require 'texticle/tasks'
|
39
|
-
|
40
|
-
Then run:
|
49
|
+
Full text searches can be sped up by creating indexes. To create them, run:
|
41
50
|
|
42
51
|
$ rake texticle:create_indexes
|
43
52
|
|
data/lib/texticle.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'texticle/full_text_index'
|
2
|
+
require 'texticle/railtie' if defined?(Rails) and Rails::VERSION::MAJOR > 2
|
2
3
|
|
3
4
|
####
|
4
5
|
# Texticle exposes full text search capabilities from PostgreSQL, and allows
|
@@ -48,7 +49,7 @@ require 'texticle/full_text_index'
|
|
48
49
|
# end
|
49
50
|
module Texticle
|
50
51
|
# The version of Texticle you are using.
|
51
|
-
VERSION = '1.0.
|
52
|
+
VERSION = '1.0.3'
|
52
53
|
|
53
54
|
# A list of full text indexes
|
54
55
|
attr_accessor :full_text_indexes
|
@@ -58,17 +59,19 @@ module Texticle
|
|
58
59
|
def index name = nil, dictionary = 'english', &block
|
59
60
|
search_name = ['search', name].compact.join('_')
|
60
61
|
|
61
|
-
class_eval
|
62
|
-
named_scope
|
62
|
+
class_eval do
|
63
|
+
named_scope search_name.to_sym, lambda { |term|
|
64
|
+
# Let's extract the individual terms to allow for quoted terms.
|
65
|
+
term = term.scan(/"([^"]+)"|(\S+)/).flatten.compact.map {|lex| "'#{lex}'"}.join(' & ')
|
63
66
|
{
|
64
|
-
:select => "
|
65
|
-
|
67
|
+
:select => "#{table_name}.*, ts_rank_cd((#{full_text_indexes.first.to_s}),
|
68
|
+
to_tsquery(#{connection.quote(term)})) as rank",
|
66
69
|
:conditions =>
|
67
|
-
["
|
70
|
+
["#{full_text_indexes.first.to_s} @@ to_tsquery(?)", term],
|
68
71
|
:order => 'rank DESC'
|
69
72
|
}
|
70
73
|
}
|
71
|
-
|
74
|
+
end
|
72
75
|
index_name = [table_name, name, 'fts_idx'].compact.join('_')
|
73
76
|
(self.full_text_indexes ||= []) <<
|
74
77
|
FullTextIndex.new(index_name, dictionary, self, &block)
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# Module used to conform to Rails 3 plugin API
|
2
|
+
require File.expand_path( File.dirname(__FILE__) + '/../texticle')
|
3
|
+
|
4
|
+
module Texticle
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
rake_tasks do
|
7
|
+
load File.dirname(__FILE__) + '/tasks.rb'
|
8
|
+
end
|
9
|
+
|
10
|
+
initializer "texticle.configure_rails_initialization" do
|
11
|
+
ActiveRecord::Base.extend(Texticle)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/texticle/tasks.rb
CHANGED
@@ -10,7 +10,7 @@ namespace :texticle do
|
|
10
10
|
fh.puts "class FullTextSearch#{now.to_i} < ActiveRecord::Migration"
|
11
11
|
fh.puts " def self.up"
|
12
12
|
Dir[File.join(RAILS_ROOT, 'app', 'models', '*.rb')].each do |f|
|
13
|
-
klass = File.basename(f, '.rb').classify.constantize
|
13
|
+
klass = File.basename(f, '.rb').pluralize.classify.constantize
|
14
14
|
if klass.respond_to?(:full_text_indexes)
|
15
15
|
(klass.full_text_indexes || []).each do |fti|
|
16
16
|
fh.puts <<-eostmt
|
@@ -32,10 +32,14 @@ namespace :texticle do
|
|
32
32
|
desc "Create full text indexes"
|
33
33
|
task :create_indexes => ['texticle:destroy_indexes'] do
|
34
34
|
Dir[File.join(RAILS_ROOT, 'app', 'models', '*.rb')].each do |f|
|
35
|
-
klass = File.basename(f, '.rb').classify.constantize
|
35
|
+
klass = File.basename(f, '.rb').pluralize.classify.constantize
|
36
36
|
if klass.respond_to?(:full_text_indexes)
|
37
37
|
(klass.full_text_indexes || []).each do |fti|
|
38
|
-
|
38
|
+
begin
|
39
|
+
fti.create
|
40
|
+
rescue ActiveRecord::StatementInvalid => e
|
41
|
+
warn "WARNING: Couldn't create index for #{klass.to_s}, skipping..."
|
42
|
+
end
|
39
43
|
end
|
40
44
|
end
|
41
45
|
end
|
@@ -44,7 +48,7 @@ namespace :texticle do
|
|
44
48
|
desc "Destroy full text indexes"
|
45
49
|
task :destroy_indexes => [:environment] do
|
46
50
|
Dir[File.join(RAILS_ROOT, 'app', 'models', '*.rb')].each do |f|
|
47
|
-
klass = File.basename(f, '.rb').classify.constantize
|
51
|
+
klass = File.basename(f, '.rb').pluralize.classify.constantize
|
48
52
|
if klass.respond_to?(:full_text_indexes)
|
49
53
|
(klass.full_text_indexes || []).each do |fti|
|
50
54
|
fti.destroy
|
data/test/test_texticle.rb
CHANGED
@@ -44,4 +44,17 @@ class TestTexticle < TexticleTestCase
|
|
44
44
|
ns = x.named_scopes.first[1].call('foo')
|
45
45
|
assert_match(/^#{x.table_name}\.\*/, ns[:select])
|
46
46
|
end
|
47
|
+
|
48
|
+
def test_double_quoted_queries
|
49
|
+
x = fake_model
|
50
|
+
x.class_eval do
|
51
|
+
extend Texticle
|
52
|
+
index('awesome') do
|
53
|
+
name
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
ns = x.named_scopes.first[1].call('foo bar "foo bar"')
|
58
|
+
assert_match(/'foo' & 'bar' & 'foo bar'/, ns[:select])
|
59
|
+
end
|
47
60
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: texticle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 17
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 3
|
10
|
+
version: 1.0.3
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Aaron Patterson
|
@@ -9,19 +15,41 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2010-07-08 00:00:00 -07:00
|
13
19
|
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
22
|
+
name: rubyforge
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 0
|
33
|
+
- 4
|
34
|
+
version: 2.0.4
|
17
35
|
type: :development
|
18
|
-
|
19
|
-
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: hoe
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
20
42
|
requirements:
|
21
43
|
- - ">="
|
22
44
|
- !ruby/object:Gem::Version
|
23
|
-
|
24
|
-
|
45
|
+
hash: 23
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 6
|
49
|
+
- 0
|
50
|
+
version: 2.6.0
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
25
53
|
description: |-
|
26
54
|
Texticle exposes full text search capabilities from PostgreSQL, and allows
|
27
55
|
you to declare full text indexes. Texticle will extend ActiveRecord with
|
@@ -44,6 +72,7 @@ files:
|
|
44
72
|
- Rakefile
|
45
73
|
- lib/texticle.rb
|
46
74
|
- lib/texticle/full_text_index.rb
|
75
|
+
- lib/texticle/railtie.rb
|
47
76
|
- lib/texticle/tasks.rb
|
48
77
|
- rails/init.rb
|
49
78
|
- test/helper.rb
|
@@ -60,21 +89,27 @@ rdoc_options:
|
|
60
89
|
require_paths:
|
61
90
|
- lib
|
62
91
|
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
63
93
|
requirements:
|
64
94
|
- - ">="
|
65
95
|
- !ruby/object:Gem::Version
|
96
|
+
hash: 3
|
97
|
+
segments:
|
98
|
+
- 0
|
66
99
|
version: "0"
|
67
|
-
version:
|
68
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
69
102
|
requirements:
|
70
103
|
- - ">="
|
71
104
|
- !ruby/object:Gem::Version
|
105
|
+
hash: 3
|
106
|
+
segments:
|
107
|
+
- 0
|
72
108
|
version: "0"
|
73
|
-
version:
|
74
109
|
requirements: []
|
75
110
|
|
76
111
|
rubyforge_project: texticle
|
77
|
-
rubygems_version: 1.3.
|
112
|
+
rubygems_version: 1.3.7
|
78
113
|
signing_key:
|
79
114
|
specification_version: 3
|
80
115
|
summary: Texticle exposes full text search capabilities from PostgreSQL, and allows you to declare full text indexes
|