forked from crowdint/rails3-jquery-autocomplete
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Padilla
committed
Jul 6, 2011
1 parent
17026c8
commit 82f51ff
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Rails3JQueryAutocomplete | ||
module Orm | ||
module MongoMapper | ||
def get_autocomplete_order(method, options, model=nil) | ||
order = options[:order] | ||
if order | ||
order.split(',').collect do |fields| | ||
sfields = fields.split | ||
[sfields[0].downcase.to_sym, sfields[1].downcase.to_sym] | ||
end | ||
else | ||
[[method.to_sym, :asc]] | ||
end | ||
end | ||
|
||
def get_autocomplete_items(parameters) | ||
model = parameters[:model] | ||
method = parameters[:method] | ||
options = parameters[:options] | ||
is_full_search = options[:full] | ||
term = parameters[:term] | ||
limit = get_autocomplete_limit(options) | ||
order = get_autocomplete_order(method, options) | ||
|
||
search = (is_full_search ? '.*' : '^') + term + '.*' | ||
items = model.where(method.to_sym => /#{search}/i).limit(limit).sort(order) | ||
end | ||
end | ||
end | ||
end |
60 changes: 60 additions & 0 deletions
60
test/lib/rails3-jquery-autocomplete/orm/mongo_mapper_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'test_helper' | ||
|
||
module Rails3JQueryAutocomplete | ||
module Orm | ||
class MongoMapperTest < Test::Unit::TestCase | ||
include Rails3JQueryAutocomplete::Orm::MongoMapper | ||
|
||
context "#get_autocomplete_order" do | ||
context "order is specified" do | ||
should 'returns the parametrized order for Mongoid' do | ||
assert_equal [[:field, :asc], [:state, :desc]], | ||
get_autocomplete_order(:method, :order => 'field ASC, state DESC') | ||
end | ||
end | ||
|
||
context 'order is not specified' do | ||
should 'return the method ordered ASC by default' do | ||
assert_equal [[:method, :asc]], | ||
get_autocomplete_order(:method, {}) | ||
end | ||
end | ||
end | ||
|
||
context "#get_autocomplete_items" do | ||
setup do | ||
@model = mock(Object) | ||
|
||
@parameters = { | ||
:model => @model, | ||
:method => :field, | ||
:term => 'query', | ||
:options => {:full => false} | ||
} | ||
mock(self).get_autocomplete_limit(anything) { 10 } | ||
mock(self).get_autocomplete_order(anything, anything) { [[:order, :asc]] } | ||
end | ||
|
||
context 'not a full search' do | ||
should 'do stuff' do | ||
mock(@model).where({:field=>/^query.*/i}).mock!.limit(10). | ||
mock!.sort([[:order, :asc]]) | ||
|
||
get_autocomplete_items(@parameters) | ||
end | ||
end | ||
|
||
context 'full search' do | ||
should 'return a full search query' do | ||
@parameters[:options] = {:full => true} | ||
|
||
mock(@model).where({:field => /.*query.*/i}).mock!.limit(10). | ||
mock!.sort([[:order, :asc]]) | ||
|
||
get_autocomplete_items(@parameters) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |