dusen 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +3 -0
- data/README.md +24 -3
- data/Rakefile +21 -0
- data/lib/dusen/active_record/base_ext.rb +13 -9
- data/lib/dusen/active_record/search_text.rb +1 -1
- data/lib/dusen/version.rb +1 -1
- data/spec/shared/spec/dusen/active_record/base_ext_spec.rb +13 -1
- metadata +4 -4
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -187,8 +187,6 @@ We now need to your model which text to index. We do this using the `search_text
|
|
187
187
|
[name, street, city, email]
|
188
188
|
end
|
189
189
|
|
190
|
-
end
|
191
|
-
|
192
190
|
end
|
193
191
|
|
194
192
|
You can return any object or array of objects. Dusen will stringify the return value and index those words. Note that indexed words do not need to be fields of your model:
|
@@ -210,10 +208,11 @@ Also note that if you migrated an existing table to FULLTEXT search, you need to
|
|
210
208
|
|
211
209
|
If you migrated an existing table to FULLTEXT search, you must build the index for all existing records:
|
212
210
|
|
213
|
-
Model.all.each(
|
211
|
+
Model.all.each(&:index_search_text)
|
214
212
|
|
215
213
|
You only need to do this once. Dusen will automatically index all further changes to your records.
|
216
214
|
|
215
|
+
|
217
216
|
### Indexing changes in associated records
|
218
217
|
|
219
218
|
Dusen lets you index words from associated models. When you do this you need to reindex the indexed model whenever an associated record changes, or else the indexed text will be out of date.
|
@@ -247,6 +246,28 @@ To make sure contacts will reindex when the organization changes its name, use t
|
|
247
246
|
All records returned by `part_of_search_text_for` will be reindexed when the organization is changed or destroyed.
|
248
247
|
|
249
248
|
|
249
|
+
### Obtaining the currently indexed words
|
250
|
+
|
251
|
+
To access a string of words that is indexed for a record, call `#search_text`:
|
252
|
+
|
253
|
+
contact = Contact.create!(:email => 'foo@bar.com', :city => 'Foohausen')
|
254
|
+
context.search_text # => "foo@bar.com Foohausen"
|
255
|
+
|
256
|
+
This can be practical if you want to index a record under the same words as its association:
|
257
|
+
|
258
|
+
class Contact < ActiveRecord::Base
|
259
|
+
|
260
|
+
belongs_to :organization
|
261
|
+
|
262
|
+
search_syntax
|
263
|
+
|
264
|
+
search_text do
|
265
|
+
[name, email, organization.search_text]
|
266
|
+
end
|
267
|
+
|
268
|
+
end
|
269
|
+
|
270
|
+
|
250
271
|
Programmatic access without DSL
|
251
272
|
-------------------------------
|
252
273
|
|
data/Rakefile
CHANGED
@@ -4,6 +4,27 @@ require 'bundler/gem_tasks'
|
|
4
4
|
desc 'Default: Run all specs.'
|
5
5
|
task :default => 'all:spec'
|
6
6
|
|
7
|
+
namespace :travis_ci do
|
8
|
+
|
9
|
+
desc 'Things to do before Travis CI begins'
|
10
|
+
task :prepare do
|
11
|
+
Rake::Task['travis_ci:create_database'].invoke &&
|
12
|
+
Rake::Task['travis_ci:create_database_yml'].invoke
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Creates a test database'
|
16
|
+
task :create_database do
|
17
|
+
system("mysql -e 'create database dusen_test;'")
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'Creates a database.yml'
|
21
|
+
task :create_database_yml do
|
22
|
+
config_dir = "spec/shared/app_root/config"
|
23
|
+
system("cp #{config_dir}/database.sample.yml #{config_dir}/database.yml")
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
7
28
|
namespace :all do
|
8
29
|
|
9
30
|
desc "Run specs on all spec apps"
|
@@ -28,30 +28,34 @@ module Dusen
|
|
28
28
|
|
29
29
|
@has_search_text = true
|
30
30
|
|
31
|
-
has_one :
|
31
|
+
has_one :search_text_record, :as => :source, :dependent => :destroy, :class_name => '::Dusen::ActiveRecord::SearchText', :inverse_of => :source
|
32
32
|
|
33
|
-
after_create :
|
33
|
+
after_create :create_initial_search_text_record
|
34
34
|
|
35
35
|
after_update :invalidate_search_text
|
36
36
|
|
37
|
-
define_method :
|
37
|
+
define_method :search_text do
|
38
38
|
new_text = instance_eval(&text)
|
39
39
|
new_text = Array.wrap(new_text).flatten.collect(&:to_s).join(' ').gsub(/\s+/, ' ').strip
|
40
|
-
|
41
|
-
|
40
|
+
new_text
|
41
|
+
end
|
42
|
+
|
43
|
+
define_method :index_search_text do
|
44
|
+
search_text_record.present? or build_search_text_record
|
45
|
+
search_text_record.update_words!(search_text)
|
42
46
|
true
|
43
47
|
end
|
44
48
|
|
45
49
|
define_method :invalidate_search_text do
|
46
|
-
|
50
|
+
search_text_record.invalidate!
|
47
51
|
true
|
48
52
|
end
|
49
53
|
|
50
54
|
private
|
51
55
|
|
52
|
-
define_method :
|
53
|
-
|
54
|
-
|
56
|
+
define_method :create_initial_search_text_record do
|
57
|
+
build_search_text_record(:stale => true)
|
58
|
+
search_text_record.save!
|
55
59
|
end
|
56
60
|
|
57
61
|
search_syntax do
|
@@ -4,7 +4,7 @@ module Dusen
|
|
4
4
|
|
5
5
|
self.table_name = 'search_texts'
|
6
6
|
|
7
|
-
belongs_to :source, :polymorphic => true, :inverse_of => :
|
7
|
+
belongs_to :source, :polymorphic => true, :inverse_of => :search_text_record
|
8
8
|
|
9
9
|
def update_words!(words)
|
10
10
|
update_attributes!(:words => words, :stale => false)
|
data/lib/dusen/version.rb
CHANGED
@@ -94,7 +94,7 @@ describe ActiveRecord::Base do
|
|
94
94
|
Dusen::ActiveRecord::SearchText.count.should be_zero
|
95
95
|
end
|
96
96
|
|
97
|
-
describe 'indexing fields from associated records'
|
97
|
+
describe 'indexing fields from associated records' do
|
98
98
|
|
99
99
|
it 'should allow to index fields from an associated record' do
|
100
100
|
category = Recipe::Category.create!(:name => 'Rice')
|
@@ -133,6 +133,18 @@ describe ActiveRecord::Base do
|
|
133
133
|
|
134
134
|
end
|
135
135
|
|
136
|
+
end
|
137
|
+
|
138
|
+
describe '#search_text' do
|
139
|
+
|
140
|
+
it 'should return the currently indexable words for this record' do
|
141
|
+
category = Recipe::Category.create!(:name => 'Rice')
|
142
|
+
recipe = Recipe.create!(:name => 'Martini Chicken', :category => category)
|
143
|
+
recipe.search_text.should == "Martini Chicken Rice"
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
136
148
|
end
|
137
149
|
|
138
150
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dusen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 4
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Henning Koch
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-12-
|
18
|
+
date: 2012-12-17 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|