Permalink
Browse files

allow to access a record's currently indexed words using record#searc…

…h_text
  • Loading branch information...
triskweline committed Dec 17, 2012
1 parent 46d1dff commit f4f48ae56c06fbaf904ee72f77bb7b3d0ead9792
View
@@ -212,6 +212,7 @@ If you migrated an existing table to FULLTEXT search, you must build the index f
You only need to do this once. Dusen will automatically index all further changes to your records.
+
### Indexing changes in associated records
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.
@@ -245,6 +246,28 @@ To make sure contacts will reindex when the organization changes its name, use t
All records returned by `part_of_search_text_for` will be reindexed when the organization is changed or destroyed.
+### Obtaining the currently indexed words
+
+To access a string of words that is indexed for a record, call `#search_text`:
+
+ contact = Contact.create!(:email => 'foo@bar.com', :city => 'Foohausen')
+ context.search_text # => "foo@bar.com Foohausen"
+
+This can be practical if you want to index a record under the same words as its association:
+
+ class Contact < ActiveRecord::Base
+
+ belongs_to :organization
+
+ search_syntax
+
+ search_text do
+ [name, email, organization.search_text]
+ end
+
+ end
+
+
Programmatic access without DSL
-------------------------------
@@ -28,30 +28,34 @@ def search_text(&text)
@has_search_text = true
- has_one :search_text, :as => :source, :dependent => :destroy, :class_name => '::Dusen::ActiveRecord::SearchText', :inverse_of => :source
+ has_one :search_text_record, :as => :source, :dependent => :destroy, :class_name => '::Dusen::ActiveRecord::SearchText', :inverse_of => :source
- after_create :create_search_text
+ after_create :create_initial_search_text_record
after_update :invalidate_search_text
- define_method :index_search_text do
+ define_method :search_text do
new_text = instance_eval(&text)
new_text = Array.wrap(new_text).flatten.collect(&:to_s).join(' ').gsub(/\s+/, ' ').strip
- search_text || build_search_text
- search_text.update_words!(new_text)
+ new_text
+ end
+
+ define_method :index_search_text do
+ search_text_record.present? or build_search_text_record
+ search_text_record.update_words!(search_text)
true
end
define_method :invalidate_search_text do
- search_text.invalidate!
+ search_text_record.invalidate!
true
end
private
- define_method :create_search_text do
- build_search_text(:stale => true)
- search_text.save!
+ define_method :create_initial_search_text_record do
+ build_search_text_record(:stale => true)
+ search_text_record.save!
end
search_syntax do
@@ -4,7 +4,7 @@ class SearchText < ::ActiveRecord::Base
self.table_name = 'search_texts'
- belongs_to :source, :polymorphic => true, :inverse_of => :search_text
+ belongs_to :source, :polymorphic => true, :inverse_of => :search_text_record
def update_words!(words)
update_attributes!(:words => words, :stale => false)
View
@@ -1,5 +1,5 @@
# encoding: utf-8
module Dusen
- VERSION = '0.3.0'
+ VERSION = '0.4.0'
end
@@ -94,7 +94,7 @@
Dusen::ActiveRecord::SearchText.count.should be_zero
end
- describe 'indexing fields from associated records'
+ describe 'indexing fields from associated records' do
it 'should allow to index fields from an associated record' do
category = Recipe::Category.create!(:name => 'Rice')
@@ -133,6 +133,18 @@
end
+ end
+
+ describe '#search_text' do
+
+ it 'should return the currently indexable words for this record' do
+ category = Recipe::Category.create!(:name => 'Rice')
+ recipe = Recipe.create!(:name => 'Martini Chicken', :category => category)
+ recipe.search_text.should == "Martini Chicken Rice"
+ end
+
+ end
+
end
end

0 comments on commit f4f48ae

Please sign in to comment.