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

Add :offset option to fuzzy find method. #25

Merged
merged 6 commits into from
Dec 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,5 @@ Copyright (c) 2013 HouseTrip Ltd.
5. Create a new Pull Request


Thanks to @bclennox, @fdegiuli, @nickbender, @Shanison for pointing out
Thanks to @bclennox, @fdegiuli, @nickbender, @Shanison, @rickbutton for pointing out
and/or helping on various issues.
31 changes: 29 additions & 2 deletions lib/fuzzily/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ def fuzzily_searchable(*fields)

def _find_by_fuzzy(_o, pattern, options={})
options[:limit] ||= 10
options[:offset] ||= 0

trigrams = _o.trigram_class_name.constantize.
limit(options[:limit]).
offset(options[:offset]).
for_model(self.name).
for_field(_o.field.to_s).
matches_for(pattern)
Expand Down Expand Up @@ -135,6 +137,31 @@ def make_field_fuzzily_searchable(field, options={})
module Rails2ClassMethods
include ClassMethods

def self.extended(base)
base.class_eval do
named_scope :offset, lambda { |*args| { :offset => args.first } }
end
end

private

def _add_trigram_association(_o)
has_many _o.trigram_association,
:class_name => _o.trigram_class_name,
:as => :owner,
:conditions => { :fuzzy_field => _o.field.to_s },
:dependent => :destroy,
:autosave => true
end

def _with_included_trigrams(_o)
self.scoped(:include => _o.trigram_association)
end
end

module Rails3ClassMethods
include ClassMethods

private

def _add_trigram_association(_o)
Expand All @@ -151,7 +178,7 @@ def _with_included_trigrams(_o)
end
end

Rails3ClassMethods = Rails2ClassMethods


module Rails4ClassMethods
include ClassMethods
Expand All @@ -173,4 +200,4 @@ def _with_included_trigrams(_o)
end

end
end
end
6 changes: 6 additions & 0 deletions spec/fuzzily/searchable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ def Stuff.name ; 'Stuff' ; end
3.times { subject.create!(:name => 'Paris') }
subject.find_by_fuzzy_name('Paris', :limit => 2).length.should == 2
end

it 'honours offset option' do
subject.fuzzily_searchable :name
3.times { subject.create!(:name => 'Paris') }
subject.find_by_fuzzy_name('Paris', :offset => 2).length.should == 1
end
end
end

Expand Down