Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Commit

Permalink
Merge a212873 into 50cf95b
Browse files Browse the repository at this point in the history
  • Loading branch information
Nguyen The Thang committed Jun 24, 2014
2 parents 50cf95b + a212873 commit 2157dbd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,16 @@ class Employee < ActiveRecord::Base
end
```

## Update Trigram index using `sidekiq-delay`

For larger text, it takes time to build the index. Thus it can be moved into delay task using `sidekiq` + `sidekiq-delay` or `delayed_job` gem, both of them provide the method `delay` to move the execution to background thread by adding option `async`:

```ruby
class Employee < ActiveRecord::Base
fuzzily_searchable :name, async: true

end
```

## License

Expand Down
10 changes: 8 additions & 2 deletions lib/fuzzily/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def make_field_fuzzily_searchable(field, options={})
:field => field,
:trigram_class_name => options.fetch(:class_name, 'Trigram'),
:trigram_association => "trigrams_for_#{field}".to_sym,
:update_trigrams_method => "update_fuzzy_#{field}!".to_sym
:update_trigrams_method => "update_fuzzy_#{field}!".to_sym,
:async => options.fetch(:async, false)
)

_add_trigram_association(_o)
Expand All @@ -126,11 +127,16 @@ def make_field_fuzzily_searchable(field, options={})
end

define_method _o.update_trigrams_method do
_update_fuzzy!(_o)
if _o.async && self.respond_to?(:delay)
self.delay._update_fuzzy!(_o)
else
_update_fuzzy!(_o)
end
end

after_save do |record|
next unless record.send("#{field}_changed?".to_sym)

record.send(_o.update_trigrams_method)
end

Expand Down

0 comments on commit 2157dbd

Please sign in to comment.