phat_pgsearch 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+ gem "rails", "~> 3.0.0"
3
+
4
+ group :development do
5
+ gem "autotest"
6
+ gem "bundler"
7
+ gem "jeweler"
8
+ gem "rspec"
9
+ gem "yard"
10
+ gem "pg"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,96 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ZenTest (4.5.0)
5
+ abstract (1.0.0)
6
+ actionmailer (3.0.4)
7
+ actionpack (= 3.0.4)
8
+ mail (~> 2.2.15)
9
+ actionpack (3.0.4)
10
+ activemodel (= 3.0.4)
11
+ activesupport (= 3.0.4)
12
+ builder (~> 2.1.2)
13
+ erubis (~> 2.6.6)
14
+ i18n (~> 0.4)
15
+ rack (~> 1.2.1)
16
+ rack-mount (~> 0.6.13)
17
+ rack-test (~> 0.5.7)
18
+ tzinfo (~> 0.3.23)
19
+ activemodel (3.0.4)
20
+ activesupport (= 3.0.4)
21
+ builder (~> 2.1.2)
22
+ i18n (~> 0.4)
23
+ activerecord (3.0.4)
24
+ activemodel (= 3.0.4)
25
+ activesupport (= 3.0.4)
26
+ arel (~> 2.0.2)
27
+ tzinfo (~> 0.3.23)
28
+ activeresource (3.0.4)
29
+ activemodel (= 3.0.4)
30
+ activesupport (= 3.0.4)
31
+ activesupport (3.0.4)
32
+ arel (2.0.8)
33
+ autotest (4.4.6)
34
+ ZenTest (>= 4.4.1)
35
+ builder (2.1.2)
36
+ diff-lcs (1.1.2)
37
+ erubis (2.6.6)
38
+ abstract (>= 1.0.0)
39
+ git (1.2.5)
40
+ i18n (0.5.0)
41
+ jeweler (1.5.2)
42
+ bundler (~> 1.0.0)
43
+ git (>= 1.2.5)
44
+ rake
45
+ mail (2.2.15)
46
+ activesupport (>= 2.3.6)
47
+ i18n (>= 0.4.0)
48
+ mime-types (~> 1.16)
49
+ treetop (~> 1.4.8)
50
+ mime-types (1.16)
51
+ pg (0.10.1)
52
+ polyglot (0.3.1)
53
+ rack (1.2.1)
54
+ rack-mount (0.6.13)
55
+ rack (>= 1.0.0)
56
+ rack-test (0.5.7)
57
+ rack (>= 1.0)
58
+ rails (3.0.4)
59
+ actionmailer (= 3.0.4)
60
+ actionpack (= 3.0.4)
61
+ activerecord (= 3.0.4)
62
+ activeresource (= 3.0.4)
63
+ activesupport (= 3.0.4)
64
+ bundler (~> 1.0)
65
+ railties (= 3.0.4)
66
+ railties (3.0.4)
67
+ actionpack (= 3.0.4)
68
+ activesupport (= 3.0.4)
69
+ rake (>= 0.8.7)
70
+ thor (~> 0.14.4)
71
+ rake (0.8.7)
72
+ rspec (2.5.0)
73
+ rspec-core (~> 2.5.0)
74
+ rspec-expectations (~> 2.5.0)
75
+ rspec-mocks (~> 2.5.0)
76
+ rspec-core (2.5.1)
77
+ rspec-expectations (2.5.0)
78
+ diff-lcs (~> 1.1.2)
79
+ rspec-mocks (2.5.0)
80
+ thor (0.14.6)
81
+ treetop (1.4.9)
82
+ polyglot (>= 0.3.1)
83
+ tzinfo (0.3.24)
84
+ yard (0.6.4)
85
+
86
+ PLATFORMS
87
+ ruby
88
+
89
+ DEPENDENCIES
90
+ autotest
91
+ bundler
92
+ jeweler
93
+ pg
94
+ rails (~> 3.0.0)
95
+ rspec
96
+ yard
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Marco Scholl
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,120 @@
1
+ = phat_pgsearch
2
+
3
+ a plugin for postgresql tssearch support
4
+
5
+ == Installation
6
+
7
+ add to Gemfile
8
+ gem "phat_pgsearch"
9
+
10
+ == Configuration
11
+
12
+ PhatPgsearch.setup do |config|
13
+ # defaut catalog
14
+ # config.catalog = :english
15
+ end
16
+
17
+ == Using
18
+
19
+ === create table columns
20
+ create_table :sample_headers, :force => true do |t|
21
+ t.string :title
22
+ t.tsvector :tsv
23
+ end
24
+ create_table :sample_items, :force => true do |t|
25
+ t.integer :sample_header_id
26
+ t.text :content
27
+ t.tsvector :tsv
28
+ t.tsvector :tsv_full
29
+ end
30
+ create_table :sample_comments, :force => true do |t|
31
+ t.integer :sample_item_id
32
+ t.text :comment
33
+ t.tsvector :tsv
34
+ end
35
+
36
+ add_gin_index :sample_headers, :tsv, :gin => true
37
+ add_gin_index :sample_items, :tsv, :gin => true
38
+ add_gist_index :sample_comments, :tsv, :gist => true
39
+
40
+ === define index
41
+
42
+ class SampleHeader < ActiveRecord::Base
43
+ has_many :sample_items
44
+
45
+ pgsearch_index :tsv do
46
+ field :title, :weight => :a
47
+ end
48
+
49
+ end
50
+ class SampleItem < ActiveRecord::Base
51
+ has_many :sample_comments
52
+ belongs_to :sample_header
53
+
54
+ pgsearch_index :tsv, :catalog => :english do
55
+ field :content
56
+ end
57
+
58
+ pgsearch_index :tsv_full, :catalog => :german do
59
+ field :title, :weight => :a
60
+ field :content, :weight => :b
61
+ field :comment_texts, :weight => :d
62
+ end
63
+
64
+ def title
65
+ sample_header.title
66
+ end
67
+
68
+ def comment_texts
69
+ sample_comments.collect{ |comment| comment.comment }.join('')
70
+ end
71
+
72
+ end
73
+
74
+ class SampleComment < ActiveRecord::Base
75
+ belongs_to :sample_item
76
+ end
77
+
78
+ === search
79
+
80
+ # search on field :tsv_full
81
+ SampleItem.pgsearch(:tsv_full, 'test test2')
82
+
83
+ # search on field :tsv with custom catalog
84
+ SampleItem.pgsearch(:tsv, 'test test2', :catalog => :english)
85
+
86
+ # disable auto sorting and use own select and sorting
87
+ SampleItem.pgsearch(:tsv_full, 'test test2', :rank => false).
88
+ select("sample_items.*, #{SampleItem.pgsearch_query(:tsv_full, 'test test2')} AS rank").
89
+ order(:rank)
90
+
91
+ == Requirements
92
+
93
+ * Rails 3 (http://github.com/rails/rails)
94
+ * PostgreSQL
95
+
96
+ == Test environments
97
+
98
+ * ruby-1.9.2 (work)
99
+ * ruby-1.8.7 (segfault in pg)
100
+ * ree-1.8.7 (work)
101
+
102
+ == Maintainers
103
+
104
+ * Team Phatworx (http://github.com/phatworx)
105
+ * Marco Scholl (http://github.com/traxanos)
106
+
107
+ == Contributing to phat_pgsearch
108
+
109
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
110
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
111
+ * Fork the project
112
+ * Start a feature/bugfix branch
113
+ * Commit and push until you are happy with your contribution
114
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
115
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
116
+
117
+ == Copyright
118
+
119
+ Copyright (c) 2011 Marco Scholl. See LICENSE.txt for further details.
120
+
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "phat_pgsearch"
16
+ gem.homepage = "http://github.com/traxanos/phat_pgsearch"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{a postgresql based search plugin}
19
+ gem.description = %Q{a plugin for tssearch support from postgresql}
20
+ gem.email = "develop@marco-scholl.de"
21
+ gem.authors = ["Marco Scholl"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'yard'
43
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,67 @@
1
+ module PhatPgsearch
2
+ module ActiveRecord
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods #:nodoc:
9
+
10
+ def pgsearch_index(*args, &block)
11
+ unless @pgsearch_definitions.is_a? Hash
12
+ include InstanceMethods
13
+ extend SingletonMethods
14
+ @pgsearch_definitions = {}
15
+ before_save :build_pgsearch_index
16
+ end
17
+ @pgsearch_definitions[args.first.to_sym] = IndexDefinition.new(*args, &block)
18
+ end
19
+
20
+ end
21
+
22
+ module SingletonMethods #:nodoc:
23
+
24
+ def pgsearch_definitions
25
+ @pgsearch_definitions
26
+ end
27
+
28
+ def pgsearch(*args)
29
+ options = args.extract_options!
30
+ normalization = options.delete(:normalization) || 32
31
+ rank = options.delete(:rank)
32
+
33
+ scope = self
34
+
35
+ search_query = pgsearch_query(args.first, args.second, options)
36
+
37
+ if rank.nil? or rank == true
38
+ scope = scope.select("#{self.connection.quote_table_name(self.table_name)}.*, ts_rank_cd(#{self.connection.quote_column_name(args.first)}, #{search_query}, #{normalization.to_i}) AS rank")
39
+ end
40
+
41
+ scope.where("#{self.connection.quote_table_name(self.table_name)}.#{self.connection.quote_column_name(args.first)} @@ #{search_query}")
42
+ end
43
+
44
+ def pgsearch_query(*args)
45
+ options = args.extract_options!
46
+ raise ArgumentError, "invalid field given" unless pgsearch_definitions.include? args.first.to_sym
47
+ raise ArgumentError, "invalid query" if not args.second or not args.second.is_a? String
48
+
49
+ definition = pgsearch_definitions[args.first.to_sym]
50
+ catalog = options[:catalog] || definition.catalog
51
+
52
+ "plainto_tsquery(#{self.sanitize(catalog)}, #{self.sanitize(args.second)})"
53
+ end
54
+
55
+ end
56
+
57
+ module InstanceMethods #:nodoc:
58
+
59
+ def build_pgsearch_index
60
+ self.class.pgsearch_definitions.each_pair do |index_field, index_definition|
61
+ IndexBuilder.new(self, index_definition)
62
+ end
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ module PhatPgsearch
3
+ class IndexBuilder
4
+ attr_reader :base, :definition, :build, :connection
5
+ def initialize(base, definition)
6
+ @base = base
7
+ @definition = definition
8
+ @connection = base.class.connection
9
+ build_index
10
+ end
11
+
12
+ protected
13
+
14
+ def build_index
15
+ partials = []
16
+ definition.fields.each do |field_definition|
17
+ field = field_definition.first
18
+ field_options = field_definition.extract_options!
19
+ field_content = base.respond_to?(field) ? base.send(field.to_s) : ''
20
+ if not field_options[:weight].nil? and [:a, :b, :c, :d].include? field_options[:weight].to_sym
21
+ partial = "setweight(to_tsvector(#{base.class.sanitize(definition.catalog)}, #{base.class.sanitize(field_content)}), '#{field_options[:weight].to_s.upcase}')"
22
+ else
23
+ partial = "to_tsvector(#{base.class.sanitize(definition.catalog)}, #{base.class.sanitize(field_content)})"
24
+ end
25
+ partials << partial
26
+ end
27
+ base.send("#{definition.index_field}=", base.class.connection.select_value("SELECT #{partials.join(' || ')}"))
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ module PhatPgsearch
2
+ class IndexDefinition
3
+ attr_reader :options, :fields, :index_field
4
+ def initialize(*args, &block)
5
+ @options = args.extract_options!
6
+ @index_field = args.first
7
+ @fields = []
8
+ instance_eval(&block)
9
+ end
10
+
11
+ def field(*args)
12
+ @fields << args
13
+ end
14
+
15
+ def catalog
16
+ options[:catalog] || PhatPgsearch.catalog
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,43 @@
1
+ module PhatPgsearch
2
+ module PostgreSQL
3
+
4
+ module SchemaStatements
5
+
6
+ def add_gin_index(table_name, column_name, options = {})
7
+ index_name = index_name(table_name, :column => column_name)
8
+ index_name = options[:name].to_s if options.key?(:name)
9
+ execute "CREATE INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} USING gin(#{quote_column_name(column_name)})"
10
+ end
11
+
12
+ def add_gist_index(table_name, column_name, options = {})
13
+ index_name = index_name(table_name, :column => column_name)
14
+ index_name = options[:name].to_s if options.key?(:name)
15
+ execute "CREATE INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} USING gist(#{quote_column_name(column_name)})"
16
+ end
17
+ end
18
+
19
+ module PostgreSQLColumn
20
+ def simplified_type(field_type)
21
+ if field_type == 'tsvector'
22
+ :string
23
+ else
24
+ super(field_type)
25
+ end
26
+ end
27
+ end
28
+
29
+ module TableDefinition
30
+ def self.included(base)
31
+ # add data type
32
+ ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:tsvector] = {:name => "tsvector"}
33
+ end
34
+
35
+ # add tsvector column
36
+ def tsvector(*args)
37
+ options = args.extract_options!
38
+ column_names = args
39
+ column_names.each { |name| column(name, :tsvector, options) }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,30 @@
1
+ require 'active_support/all'
2
+ require 'active_record'
3
+ require 'active_record/connection_adapters/postgresql_adapter'
4
+ module PhatPgsearch
5
+ autoload :IndexDefinition, 'phat_pgsearch/index_definition'
6
+ autoload :IndexBuilder, 'phat_pgsearch/index_builder'
7
+ autoload :ActiveRecord, 'phat_pgsearch/active_record'
8
+ autoload :PostgreSQL, 'phat_pgsearch/postgresql'
9
+
10
+ # default catalog
11
+ mattr_accessor :catalog
12
+ @@catalog = :english
13
+
14
+ class << self
15
+ # default
16
+ def setup
17
+ yield self
18
+ end
19
+
20
+ def init
21
+ ::ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, PostgreSQL::TableDefinition)
22
+ ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:include, PostgreSQL::SchemaStatements)
23
+ ::ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send(:include, PostgreSQL::PostgreSQLColumn)
24
+ ::ActiveRecord::Base.send(:include, ActiveRecord)
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ PhatPgsearch.init
@@ -0,0 +1,81 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{phat_pgsearch}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Marco Scholl"]
12
+ s.date = %q{2011-02-22}
13
+ s.description = %q{a plugin for tssearch support from postgresql}
14
+ s.email = %q{develop@marco-scholl.de}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/phat_pgsearch.rb",
29
+ "lib/phat_pgsearch/active_record.rb",
30
+ "lib/phat_pgsearch/index_builder.rb",
31
+ "lib/phat_pgsearch/index_definition.rb",
32
+ "lib/phat_pgsearch/postgresql.rb",
33
+ "phat_pgsearch.gemspec",
34
+ "spec/active_record_spec.rb",
35
+ "spec/phat_pgsearch_spec.rb",
36
+ "spec/spec_helper.rb",
37
+ "spec/support/active_record.rb"
38
+ ]
39
+ s.homepage = %q{http://github.com/traxanos/phat_pgsearch}
40
+ s.licenses = ["MIT"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = %q{1.5.2}
43
+ s.summary = %q{a postgresql based search plugin}
44
+ s.test_files = [
45
+ "spec/active_record_spec.rb",
46
+ "spec/phat_pgsearch_spec.rb",
47
+ "spec/spec_helper.rb",
48
+ "spec/support/active_record.rb"
49
+ ]
50
+
51
+ if s.respond_to? :specification_version then
52
+ s.specification_version = 3
53
+
54
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
55
+ s.add_runtime_dependency(%q<rails>, ["~> 3.0.0"])
56
+ s.add_development_dependency(%q<autotest>, [">= 0"])
57
+ s.add_development_dependency(%q<bundler>, [">= 0"])
58
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
59
+ s.add_development_dependency(%q<rspec>, [">= 0"])
60
+ s.add_development_dependency(%q<yard>, [">= 0"])
61
+ s.add_development_dependency(%q<pg>, [">= 0"])
62
+ else
63
+ s.add_dependency(%q<rails>, ["~> 3.0.0"])
64
+ s.add_dependency(%q<autotest>, [">= 0"])
65
+ s.add_dependency(%q<bundler>, [">= 0"])
66
+ s.add_dependency(%q<jeweler>, [">= 0"])
67
+ s.add_dependency(%q<rspec>, [">= 0"])
68
+ s.add_dependency(%q<yard>, [">= 0"])
69
+ s.add_dependency(%q<pg>, [">= 0"])
70
+ end
71
+ else
72
+ s.add_dependency(%q<rails>, ["~> 3.0.0"])
73
+ s.add_dependency(%q<autotest>, [">= 0"])
74
+ s.add_dependency(%q<bundler>, [">= 0"])
75
+ s.add_dependency(%q<jeweler>, [">= 0"])
76
+ s.add_dependency(%q<rspec>, [">= 0"])
77
+ s.add_dependency(%q<yard>, [">= 0"])
78
+ s.add_dependency(%q<pg>, [">= 0"])
79
+ end
80
+ end
81
+
@@ -0,0 +1,85 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ describe PhatPgsearch::ActiveRecord do
5
+ describe SampleHeader do
6
+ describe "#save" do
7
+ before do
8
+ @sample_header = SampleHeader.new(:title => "Some test title")
9
+ end
10
+
11
+ subject { @sample_header }
12
+
13
+ it "should have build index after save" do
14
+ subject.save
15
+ subject.tsv.should_not be_nil
16
+ end
17
+ end
18
+ end
19
+
20
+ describe SampleItem do
21
+ describe "#save" do
22
+ before do
23
+ @sample_header = SampleHeader.new(:title => "Some test title")
24
+ @sample_item = @sample_header.sample_items.build(:content => 'noch so ein süßer test mit sonder zeichen')
25
+ @sample_item.sample_comments.build(:comment => 'das ist ein kommentar')
26
+ @sample_item.sample_comments.build(:comment => 'das ist ein weiterer kommentar')
27
+ @sample_header.save
28
+ end
29
+
30
+ subject { @sample_item }
31
+
32
+ it "should have build index after save" do
33
+ subject.tsv.should_not be_nil
34
+ subject.tsv_full.should_not be_nil
35
+ end
36
+ end
37
+ end
38
+
39
+ context "sample archive" do
40
+ before :all do
41
+ @sample_header = SampleHeader.new(:title => "Some test title")
42
+ @sample_item = @sample_header.sample_items.build(:content => 'noch so ein süßer test mit sonder zeichen')
43
+ @sample_item.sample_comments.build(:comment => 'das ist ein kommentar')
44
+ @sample_item.sample_comments.build(:comment => 'das ist ein weiterer kommentar')
45
+ @sample_header.save
46
+ end
47
+
48
+ describe SampleItem do
49
+ describe "#pgsearch :tsv, 'kommentar'" do
50
+ before { @pgsearch = SampleItem.pgsearch(:tsv, 'kommentar') }
51
+ subject { @pgsearch }
52
+ it "should do something" do
53
+ subject.to_a
54
+ end
55
+
56
+ end
57
+ describe "#pgsearch :tsv_full, 'kommentar'" do
58
+ before { @pgsearch = SampleItem.pgsearch(:tsv_full, 'kommentar') }
59
+ subject { @pgsearch }
60
+ it "should do something" do
61
+ subject.to_a
62
+ end
63
+
64
+ end
65
+ describe "#pgsearch :tsv_full, 'kommentar', :catalog => :german" do
66
+ before { @pgsearch = SampleItem.pgsearch(:tsv_full, 'kommentar', :catalog => :german) }
67
+ subject { @pgsearch }
68
+ it "should do something" do
69
+ subject.to_a
70
+ end
71
+
72
+ end
73
+ describe "#pgsearch :tsv_full, 'kommentar', :rank => false" do
74
+ before { @pgsearch = SampleItem.pgsearch(:tsv_full, 'kommentar', :rank => false).select("sample_items.*, #{SampleItem.pgsearch_query(:tsv_full, 'kommentar')} AS rank").order(:rank) }
75
+ subject { @pgsearch }
76
+ it "should do something" do
77
+ subject.to_a
78
+ end
79
+
80
+ end
81
+ end
82
+
83
+ end
84
+
85
+ end
@@ -0,0 +1,5 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe PhatPgsearch do
4
+
5
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'active_record'
5
+ require 'phat_pgsearch'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+
13
+ end
@@ -0,0 +1,69 @@
1
+ #ActiveRecord::Base.logger = Logger.new(STDOUT)
2
+ ActiveRecord::Base.establish_connection(
3
+ {
4
+ :adapter => 'postgresql',
5
+ :database => 'phat_pgsearch',
6
+ :encoding => 'utf-8',
7
+ :password => 'devel',
8
+ :host => 'localhost',
9
+ :username => 'pgadmin',
10
+ :min_messages => 'WARNING'
11
+ }
12
+ )
13
+
14
+ ActiveRecord::Schema.define(:version => 0) do
15
+ create_table :sample_headers, :force => true do |t|
16
+ t.string :title
17
+ t.tsvector :tsv
18
+ end
19
+ create_table :sample_items, :force => true do |t|
20
+ t.integer :sample_header_id
21
+ t.text :content
22
+ t.tsvector :tsv
23
+ t.tsvector :tsv_full
24
+ end
25
+ create_table :sample_comments, :force => true do |t|
26
+ t.integer :sample_item_id
27
+ t.text :comment
28
+ t.tsvector :tsv
29
+ end
30
+
31
+ add_gin_index :sample_headers, :tsv, :gin => true
32
+ add_gin_index :sample_items, :tsv, :gin => true
33
+ add_gist_index :sample_comments, :tsv, :gist => true
34
+ end
35
+
36
+ class SampleHeader < ActiveRecord::Base
37
+ has_many :sample_items
38
+
39
+ pgsearch_index :tsv do
40
+ field :title, :weight => :a
41
+ end
42
+
43
+ end
44
+ class SampleItem < ActiveRecord::Base
45
+ has_many :sample_comments
46
+ belongs_to :sample_header
47
+
48
+ pgsearch_index :tsv, :catalog => :english do
49
+ field :content
50
+ end
51
+
52
+ pgsearch_index :tsv_full, :catalog => :german do
53
+ field :title, :weight => :a
54
+ field :content, :weight => :b
55
+ field :comment_texts, :weight => :d
56
+ end
57
+
58
+ def title
59
+ sample_header.title
60
+ end
61
+
62
+ def comment_texts
63
+ sample_comments.collect{ |comment| comment.comment }.join('')
64
+ end
65
+
66
+ end
67
+ class SampleComment < ActiveRecord::Base
68
+ belongs_to :sample_item
69
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phat_pgsearch
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Marco Scholl
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-22 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 3.0.0
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: autotest
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: bundler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: jeweler
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: rspec
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: yard
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: pg
84
+ requirement: &id007 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: *id007
93
+ description: a plugin for tssearch support from postgresql
94
+ email: develop@marco-scholl.de
95
+ executables: []
96
+
97
+ extensions: []
98
+
99
+ extra_rdoc_files:
100
+ - LICENSE.txt
101
+ - README.rdoc
102
+ files:
103
+ - .document
104
+ - .rspec
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE.txt
108
+ - README.rdoc
109
+ - Rakefile
110
+ - VERSION
111
+ - lib/phat_pgsearch.rb
112
+ - lib/phat_pgsearch/active_record.rb
113
+ - lib/phat_pgsearch/index_builder.rb
114
+ - lib/phat_pgsearch/index_definition.rb
115
+ - lib/phat_pgsearch/postgresql.rb
116
+ - phat_pgsearch.gemspec
117
+ - spec/active_record_spec.rb
118
+ - spec/phat_pgsearch_spec.rb
119
+ - spec/spec_helper.rb
120
+ - spec/support/active_record.rb
121
+ has_rdoc: true
122
+ homepage: http://github.com/traxanos/phat_pgsearch
123
+ licenses:
124
+ - MIT
125
+ post_install_message:
126
+ rdoc_options: []
127
+
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ hash: -4541903171305181018
136
+ segments:
137
+ - 0
138
+ version: "0"
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: "0"
145
+ requirements: []
146
+
147
+ rubyforge_project:
148
+ rubygems_version: 1.5.2
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: a postgresql based search plugin
152
+ test_files:
153
+ - spec/active_record_spec.rb
154
+ - spec/phat_pgsearch_spec.rb
155
+ - spec/spec_helper.rb
156
+ - spec/support/active_record.rb