Skip to content

Commit

Permalink
OMG cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
David Padilla committed Jul 6, 2011
1 parent 82f51ff commit 4169716
Show file tree
Hide file tree
Showing 8 changed files with 3 additions and 371 deletions.
86 changes: 0 additions & 86 deletions lib/rails3-jquery-autocomplete/autocomplete.rb
Expand Up @@ -103,92 +103,6 @@ def get_implementation(object)
raise NotImplementedError
end
end

#DEPRECATED
def get_order(implementation, method, options)
warn 'Rails3JQueryAutocomplete#get_order is has been DEPRECATED, please use #get_autocomplete_order instead'
get_autocomplete_order(implementation, method, options)
end

# Returns the order parameter to be used in the query created by get_items
def get_autocomplete_order(implementation, method, options, model=nil)
order = options[:order]

case implementation
when :mongoid then
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
when :mongo_mapper then
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
when :activerecord then
table_prefix = model ? "#{model.table_name}." : ""
order || "#{table_prefix}#{method} ASC"
end
end

# DEPRECATED
def get_limit(options)
warn 'Rails3JQueryAutocomplete#get_limit is has been DEPRECATED, please use #get_autocomplete_limit instead'
get_autocomplete_limit(options)
end

# DEPRECATED
def get_items(parameters)
warn 'Rails3JQueryAutocomplete#get_items is has been DEPRECATED, you should use #get_autocomplete_items instead'
get_autocomplete_items(parameters)
end

#
# Can be overriden to return or filter however you like
# the objects to be shown by autocomplete
#
# items = get_autocomplete_items(:model => get_object(object), :options => options, :term => term, :method => method)
#
def get_autocomplete_items(parameters)
model = parameters[:model]
term = parameters[:term]
method = parameters[:method]
options = parameters[:options]

is_full_search = options[:full]
scopes = Array(options[:scopes])
limit = get_autocomplete_limit(options)
implementation = get_implementation(model)
order = get_autocomplete_order(implementation, method, options, model)

like_clause = (defined?(PGconn) ? 'ILIKE' : 'LIKE')

implementation == :mongo_mapper ? (items = model.query) : items = model.scoped

scopes.each { |scope| items = items.send(scope) } unless scopes.empty?

case implementation
when :mongoid
search = (is_full_search ? '.*' : '^') + term + '.*'
items = model.where(method.to_sym => /#{search}/i).limit(limit).order_by(order)
when :mongo_mapper
search = (is_full_search ? '.*' : '^') + term + '.*'
items = model.where(method.to_sym => /#{search}/i).limit(limit).sort(order)
when :activerecord
table_name = model.table_name
items = items.select(["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + (options[:extra_data].blank? ? [] : options[:extra_data])) unless options[:full_model]
items = items.where(["LOWER(#{table_name}.#{method}) #{like_clause} ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"]) \
.limit(limit).order(order)
end
end
end
end

41 changes: 0 additions & 41 deletions test/implementations_test.rb

This file was deleted.

2 changes: 2 additions & 0 deletions test/lib/rails3-jquery-autocomplete/orm/active_record_test.rb
Expand Up @@ -29,6 +29,8 @@ class ActiveRecordTest < Test::Unit::TestCase

context '#get_autocomplete_items' do
should 'retrieve the items from ActiveRecord' do
class Dog ; end

model = Dog
scoped = []
whered = []
Expand Down
44 changes: 0 additions & 44 deletions test/support/active_record.rb

This file was deleted.

39 changes: 0 additions & 39 deletions test/support/mongo_mapper.rb

This file was deleted.

43 changes: 0 additions & 43 deletions test/support/mongoid.rb

This file was deleted.

117 changes: 0 additions & 117 deletions test/test_controller.rb

This file was deleted.

2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -40,4 +40,4 @@ class Test::Unit::TestCase
# end
# end

class Dog < ActiveRecord::Base ; end
# class Dog < ActiveRecord::Base ; end

0 comments on commit 4169716

Please sign in to comment.