couchrest_model_search 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +9 -8
- data/VERSION +1 -1
- data/couchrest_model_search.gemspec +2 -2
- data/lib/couchrest_model_search.rb +65 -37
- data/spec/couchrest_model_search_spec.rb +38 -8
- data/spec/spec_helper.rb +3 -0
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -13,13 +13,14 @@ This add search method calls to couchrest_model gem. Assuming you are using couc
|
|
13
13
|
# by default, couchrest_model only update view_by functions.
|
14
14
|
Article.update_search_doc
|
15
15
|
|
16
|
-
@articles = Article.search
|
16
|
+
@articles = Article.search "apple" # search by keyword apple
|
17
17
|
|
18
|
-
# you can also
|
18
|
+
# you can also customize the search function.
|
19
19
|
class CustomArticle < CouchRest::Model::Base
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
20
|
+
search_by :fulltext,
|
21
|
+
:index => %(
|
22
|
+
function(doc) {
|
23
|
+
// ....
|
24
|
+
}
|
25
|
+
)
|
26
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{couchrest_model_search}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Dorren Chen"]
|
12
|
-
s.date = %q{2010-10-
|
12
|
+
s.date = %q{2010-10-18}
|
13
13
|
s.description = %q{Add search function to CouchRest Model, assuming you're using couchdb-lucene}
|
14
14
|
s.email = %q{dorrenchen@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -4,46 +4,62 @@ module CouchRest
|
|
4
4
|
module Model
|
5
5
|
module DesignDoc
|
6
6
|
module ClassMethods
|
7
|
-
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
7
|
+
alias_method :orig_default_design_doc, :default_design_doc
|
8
|
+
def default_design_doc
|
9
|
+
orig_default_design_doc.update "fulltext" =>
|
10
|
+
{ "by_fulltext" => {
|
11
|
+
"index" => %(
|
12
|
+
function(doc) {
|
13
|
+
var ret = new Document();
|
14
|
+
function idx(obj) {
|
15
|
+
for (var key in obj){
|
16
|
+
switch (typeof obj[key]) {
|
17
|
+
case 'object':
|
18
|
+
idx(obj[key]);
|
19
|
+
break;
|
20
|
+
case 'function':
|
21
|
+
break;
|
22
|
+
default:
|
23
|
+
ret.add(obj[key]);
|
24
|
+
break;
|
25
|
+
}
|
24
26
|
}
|
25
|
-
}
|
26
|
-
};
|
27
|
+
};
|
27
28
|
|
28
|
-
|
29
|
-
|
29
|
+
if (doc['couchrest-type'] == '#{self.to_s}') {
|
30
|
+
idx(doc);
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
32
|
+
if (doc._attachments) {
|
33
|
+
for (var i in doc._attachments) {
|
34
|
+
ret.attachment('attachment', i);
|
35
|
+
}
|
34
36
|
}
|
35
37
|
}
|
38
|
+
return ret;
|
36
39
|
}
|
37
|
-
|
38
|
-
|
39
|
-
)
|
40
|
+
)
|
41
|
+
}
|
40
42
|
}
|
41
|
-
}
|
42
43
|
end
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
44
|
+
|
45
|
+
def update_search_doc
|
46
|
+
saved = stored_design_doc
|
47
|
+
if saved
|
48
|
+
saved["fulltext"] = design_doc["fulltext"]
|
49
|
+
saved.save
|
50
|
+
saved
|
51
|
+
else
|
52
|
+
design_doc.delete("_rev")
|
53
|
+
design_doc.database = database
|
54
|
+
design_doc.save
|
55
|
+
design_doc
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
alias_method :orig_save_design_doc, :save_design_doc
|
60
|
+
def save_design_doc(db = database, force = false)
|
61
|
+
orig_save_design_doc(db, force)
|
62
|
+
update_search_doc
|
47
63
|
end
|
48
64
|
end
|
49
65
|
end
|
@@ -61,16 +77,28 @@ class CouchRest::Database
|
|
61
77
|
end
|
62
78
|
|
63
79
|
class CouchRest::Model::Base
|
64
|
-
def self.search(
|
80
|
+
def self.search(query, view_fn="by_fulltext", options={})
|
65
81
|
options[:include_docs] = true
|
66
82
|
ret = self.database.search(self.to_s, view_fn, query, options)
|
67
83
|
ret['rows'].map {|r| self.new(r['doc'])}
|
68
84
|
end
|
69
85
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
86
|
+
# example search functions
|
87
|
+
# class Aritlce
|
88
|
+
# search_by :title,
|
89
|
+
# :index => %(
|
90
|
+
# function(doc) {
|
91
|
+
# // ....
|
92
|
+
# }
|
93
|
+
# )
|
94
|
+
#
|
95
|
+
# now you can make search like
|
96
|
+
# Article.search "apple" # by default it uses "by_fulltext" search function
|
97
|
+
# Article.search "apple", :by_title
|
98
|
+
def self.search_by(fn_name, fn)
|
99
|
+
design_doc["fulltext"] ||= {}
|
100
|
+
design_doc["fulltext"]["by_#{fn_name}"] = fn.stringify_keys!
|
101
|
+
req_design_doc_refresh
|
74
102
|
end
|
75
103
|
end
|
76
104
|
|
@@ -1,22 +1,52 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
class Article < CouchRest::Model::Base
|
4
|
+
use_database DB
|
5
|
+
end
|
6
|
+
|
7
|
+
describe "CouchrestModelSearch" do
|
8
|
+
before :each do
|
9
|
+
if doc = Article.stored_design_doc
|
10
|
+
doc.database = Article.database
|
11
|
+
doc.destroy
|
12
|
+
end
|
5
13
|
end
|
6
14
|
|
7
15
|
it "should overwrite default_design_doc" do
|
8
16
|
Article.design_doc["fulltext"].should_not be_nil
|
9
|
-
end
|
17
|
+
end
|
10
18
|
end
|
11
|
-
|
12
|
-
describe "
|
19
|
+
|
20
|
+
describe "overwrite design doc" do
|
13
21
|
class CustomArticle < CouchRest::Model::Base
|
14
|
-
|
15
|
-
|
22
|
+
use_database DB
|
23
|
+
search_by :fulltext,
|
24
|
+
{:index => %(function(doc) {})}
|
25
|
+
end
|
26
|
+
|
27
|
+
before :each do
|
28
|
+
if doc = CustomArticle.stored_design_doc
|
29
|
+
doc.database = CustomArticle.database
|
30
|
+
doc.destroy
|
16
31
|
end
|
17
32
|
end
|
18
33
|
|
19
34
|
it "should allow class to overwrite fulltext function" do
|
20
|
-
CustomArticle.
|
35
|
+
CustomArticle.update_search_doc
|
36
|
+
CustomArticle.stored_design_doc["fulltext"]["by_fulltext"]["index"].should == %(function(doc) {})
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should update search doc" do
|
40
|
+
CustomArticle.update_search_doc
|
41
|
+
CustomArticle.stored_design_doc["fulltext"]["by_fulltext"]["index"].should == %(function(doc) {})
|
42
|
+
|
43
|
+
class CustomArticle < CouchRest::Model::Base
|
44
|
+
use_database DB
|
45
|
+
search_by :fulltext,
|
46
|
+
{:index => %(function(doc) {// hello})}
|
47
|
+
end
|
48
|
+
|
49
|
+
CustomArticle.update_search_doc
|
50
|
+
CustomArticle.stored_design_doc["fulltext"]["by_fulltext"]["index"].should == %(function(doc) {// hello})
|
21
51
|
end
|
22
52
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,6 +4,9 @@ require 'couchrest_model_search'
|
|
4
4
|
require 'rspec'
|
5
5
|
require 'rspec/autorun'
|
6
6
|
|
7
|
+
COUCHDB_SERVER = CouchRest.new "http://admin:password@localhost:5984"
|
8
|
+
DB = COUCHDB_SERVER.database!('couchrest_model_search_test')
|
9
|
+
|
7
10
|
RSpec.configure do |config|
|
8
11
|
# == Mock Framework
|
9
12
|
#
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: couchrest_model_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dorren Chen
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-18 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|