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

Work on scopes #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PATH
activerecord (>= 2.3.17)

GEM
remote: http://yarp.dev/
remote: https://rubygems.org/
specs:
activemodel (3.2.14)
activesupport (= 3.2.14)
Expand Down
4 changes: 2 additions & 2 deletions lib/fuzzily/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ def _find_by_fuzzy(_o, pattern, options={})
matches_for(pattern)
records = _load_for_ids(trigrams.map(&:owner_id))
# order records as per trigram query (no portable way to do this in SQL)
trigrams.map { |t| records[t.owner_id] }
trigrams.map { |t| records[t.owner_id] }.select { |r| r != nil }
end

def _load_for_ids(ids)
{}.tap do |result|
find(ids).each { |_r| result[_r.id] = _r }
where(:id => ids).each { |_r| result[_r.id] = _r }
end
end

Expand Down
23 changes: 21 additions & 2 deletions spec/fuzzily/searchable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
before(:each) { prepare_trigrams_table }
before(:each) { prepare_owners_table }

subject do
subject do
silence_warnings do
Stuff = Class.new(ActiveRecord::Base)
end
def Stuff.name ; 'Stuff' ; end
def Stuff.flag ; false ; end
Stuff
end

Expand Down Expand Up @@ -73,7 +74,7 @@ def Stuff.name ; 'Stuff' ; end
before do
subject.fuzzily_searchable :name
end

it 're-creates trigrams' do
subject.create!(:name => 'Paris')
old_ids = Trigram.all.map(&:id)
Expand Down Expand Up @@ -159,6 +160,24 @@ def Stuff.name ; 'Stuff' ; end
3.times { subject.create!(:name => 'Paris') }
subject.find_by_fuzzy_name('Paris', :offset => 2).length.should == 1
end

it 'doesnt die on scopes' do
subject.fuzzily_searchable :name
@new_york = subject.create!(:name => 'New York', :flag => true)
@yorkshire = subject.create!(:name => 'Yorkshire', :flag => false)

expect {
subject.where(:flag => true).find_by_fuzzy_name('York')
}.to_not raise_error
end

it 'doesnt return nils' do
subject.fuzzily_searchable :name
@new_yokr = subject.create!(:name => 'New York', :flag => true)

results = subject.where(:flag => false).find_by_fuzzy_name('York')
results.any?{ |r| r == nil }.should be_false
end
end
end

Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def self.up
create_table :stuffs do |t|
t.string :name
t.string :data
t.boolean :flag
t.timestamps
end
end
Expand Down Expand Up @@ -79,4 +80,4 @@ def prepare_owners_table
config.after(:each) do
DATABASE.delete if DATABASE.exist?
end
end
end