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

Commit

Permalink
Merge 46322b2 into 8c7b3a9
Browse files Browse the repository at this point in the history
  • Loading branch information
mezis committed Aug 30, 2013
2 parents 8c7b3a9 + 46322b2 commit 9073779
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gemfiles/rails30.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: /Users/mezis/Dropbox/Development/fuzzily
specs:
fuzzily (0.2.2)
fuzzily (0.2.3)
activerecord (>= 2.3.17)

GEM
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails31.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: /Users/mezis/Dropbox/Development/fuzzily
specs:
fuzzily (0.2.2)
fuzzily (0.2.3)
activerecord (>= 2.3.17)

GEM
Expand Down
1 change: 1 addition & 0 deletions lib/fuzzily/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def make_field_fuzzily_searchable(field, options={})

trigram_class.transaction do
batch.each { |record| record.send(trigram_association).delete_all }
break if inserts.empty?

if supports_bulk_inserts
trigram_class.connection.insert(insert_sql + inserts.join(", "))
Expand Down
2 changes: 1 addition & 1 deletion lib/fuzzily/trigram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Fuzzily
class String < SimpleDelegator

def trigrams
return [] if __getobj__.nil?
normalized = self.normalize
number_of_trigrams = normalized.length - 3
trigrams = (0..number_of_trigrams).map { |index| normalized[index,3] }.uniq
Expand All @@ -18,7 +19,6 @@ def scored_trigrams
# Remove accents, downcase, replace spaces and word start with '*',
# return list of normalized words
def normalize
# Iconv.iconv('ascii//translit//ignore', 'utf-8', self).first.
ActiveSupport::Multibyte::Chars.new(self).
mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/,'').downcase.to_s.
gsub(/[^a-z]/,' ').
Expand Down
18 changes: 17 additions & 1 deletion spec/fuzzily/searchable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,22 @@ def Stuff.name ; 'Stuff' ; end
end

describe '#update_fuzzy_<field>!' do
it 're-creates trigrams' do
before do
subject.fuzzily_searchable :name
end

it 're-creates trigrams' do
subject.create(:name => 'Paris')
old_ids = Trigram.all.map(&:id)
subject.last.update_fuzzy_name!
(old_ids & Trigram.all.map(&:id)).should be_empty
end

it 'ignores nil values' do
subject.create(:name => nil)
subject.last.update_fuzzy_name!
Trigram.all.should be_empty
end
end

describe '.bulk_update_fuzzy_<field>' do
Expand All @@ -80,6 +89,13 @@ def Stuff.name ; 'Stuff' ; end
subject.bulk_update_fuzzy_name
Trigram.all.should_not be_empty
end

it 'ignores nil values' do
subject.create(:name => nil)
Trigram.delete_all
subject.bulk_update_fuzzy_name
Trigram.all.should be_empty
end
end

context '(integrationg test)' do
Expand Down

0 comments on commit 9073779

Please sign in to comment.