dusen 0.4.0 → 0.4.1

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.
@@ -21,7 +21,7 @@ module Dusen
21
21
  end
22
22
 
23
23
  def index_search_texts
24
- Dusen::ActiveRecord::SearchText.rewrite_all_invalid(self)
24
+ Dusen::ActiveRecord::SearchText.synchronize_model(self)
25
25
  end
26
26
 
27
27
  def search_text(&text)
@@ -1,3 +1,5 @@
1
+ require 'set'
2
+
1
3
  module Dusen
2
4
  module ActiveRecord
3
5
  class SearchText < ::ActiveRecord::Base
@@ -22,14 +24,25 @@ module Dusen
22
24
  scoped(:conditions => { :stale => true })
23
25
  end
24
26
 
25
- def self.rewrite_all_invalid(model)
27
+ def self.synchronize_model(model)
26
28
  invalid_index_records = for_model(model).invalid
27
- ids = Util.collect_column(invalid_index_records, :source_id)
28
- Util.append_scope_conditions(model, :id => ids).each(&:index_search_text)
29
+ source_ids = Util.collect_column(invalid_index_records, :source_id)
30
+ pending_source_ids = Set.new(source_ids)
31
+ source_records = Util.append_scope_conditions(model, :id => source_ids)
32
+ source_records.find_in_batches do |batch|
33
+ batch.each do |source_record|
34
+ source_record.index_search_text
35
+ pending_source_ids.delete(source_record.id)
36
+ end
37
+ end
38
+ if pending_source_ids.present?
39
+ invalid_index_records.delete_all(:source_id => pending_source_ids.to_a)
40
+ end
41
+ true
29
42
  end
30
43
 
31
44
  def self.match(model, words)
32
- rewrite_all_invalid(model) if model.search_text?
45
+ synchronize_model(model) if model.search_text?
33
46
  Dusen::Util.append_scope_conditions(
34
47
  model,
35
48
  :id => matching_source_ids(model, words)
data/lib/dusen/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Dusen
4
- VERSION = '0.4.0'
4
+ VERSION = '0.4.1'
5
5
  end
@@ -12,6 +12,14 @@ shared_examples_for 'model with search syntax' do
12
12
  subject.search('Abraham').to_a.should == [match]
13
13
  end
14
14
 
15
+ it 'should not find stale text after fields were updated (bugfix)' do
16
+ match = subject.create!(:name => 'Abraham')
17
+ no_match = subject.create!(:name => 'Elizabath')
18
+ match.update_attributes!(:name => 'Johnny')
19
+ subject.search('Abraham').to_a.should be_empty
20
+ subject.search('Johnny').to_a.should == [match]
21
+ end
22
+
15
23
  it 'should AND multiple words' do
16
24
  match = subject.create!(:name => 'Abraham Lincoln')
17
25
  no_match = subject.create!(:name => 'Abraham')
@@ -84,14 +92,31 @@ describe ActiveRecord::Base do
84
92
  it 'should be shadowed by a Dusen::ActiveRecord::SearchText, which is created, updated and destroyed with the record' do
85
93
  user = User::WithFulltext.create!(:name => 'name', :email => 'email', :city => 'city')
86
94
  User::WithFulltext.index_search_texts
87
- Dusen::ActiveRecord::SearchText.all.collect(&:words).should == ['name email city']
95
+
96
+ search_texts = Dusen::ActiveRecord::SearchText.all
97
+ search_texts.size.should == 1
98
+ search_texts[0].words.should == 'name email city'
99
+ search_texts[0].should_not be_stale
100
+
88
101
  user.reload
89
102
  user.update_attributes!(:email => 'changed_email')
103
+
104
+ search_texts = Dusen::ActiveRecord::SearchText.all
105
+ search_texts.size.should == 1
106
+ search_texts[0].words.should == 'name email city'
107
+ search_texts[0].should be_stale
108
+
90
109
  User::WithFulltext.index_search_texts
91
- Dusen::ActiveRecord::SearchText.all.collect(&:words).should == ['name changed_email city']
110
+
111
+ search_texts = Dusen::ActiveRecord::SearchText.all
112
+ search_texts.size.should == 1
113
+ search_texts[0].words.should == 'name changed_email city'
114
+ search_texts[0].should_not be_stale
115
+
92
116
  user.destroy
93
- User::WithFulltext.index_search_texts
94
- Dusen::ActiveRecord::SearchText.count.should be_zero
117
+
118
+ search_texts = Dusen::ActiveRecord::SearchText.all
119
+ search_texts.size.should be_zero
95
120
  end
96
121
 
97
122
  describe 'indexing fields from associated records' do
@@ -20,4 +20,24 @@ describe Dusen::ActiveRecord::SearchText do
20
20
 
21
21
  end
22
22
 
23
+ describe '.synchronize_model' do
24
+
25
+ it 'should refresh stale index records' do
26
+ user = User::WithFulltext.create!(:name => 'Abraham')
27
+ user.search_text_record.should be_stale
28
+ Dusen::ActiveRecord::SearchText.synchronize_model(User::WithFulltext)
29
+ user.search_text_record(true).should_not be_stale
30
+ end
31
+
32
+ it 'should remove index records that no longer map to a model record' do
33
+ user = User::WithFulltext.create!
34
+ Dusen::ActiveRecord::SearchText.count.should == 1
35
+ User::WithFulltext.delete_all
36
+ Dusen::ActiveRecord::SearchText.count.should == 1
37
+ Dusen::ActiveRecord::SearchText.synchronize_model(User::WithFulltext)
38
+ Dusen::ActiveRecord::SearchText.count.should be_zero
39
+ end
40
+
41
+ end
42
+
23
43
  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: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 0
10
- version: 0.4.0
9
+ - 1
10
+ version: 0.4.1
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-17 00:00:00 +01:00
18
+ date: 2013-01-03 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency