lib/autocomplete_rails/controller.rb in autocomplete_rails-0.1.0 vs lib/autocomplete_rails/controller.rb in autocomplete_rails-0.2.0
- old
+ new
@@ -35,12 +35,13 @@
# * :limit - default is 10.
# * :case_sensitive - if true, the search is case sensitive. Default is false.
# * :additional_data - collect additional data. Will be added to select unless full_model is invoked.
# * :full_search - search the entire value string for the term. Defaults to false, in which case the value
# field being searched (see value_method above) must start with the search term.
- # * :scopes - limit query to these ActiveRecord scopes, passed in as an array,
- # for example: `scopes: [:scope1, :scope2]`
+ # * :scopes - Build your autocomplete query from the specified ActiveRecord scope(s). Multiple scopes can be
+ # used, pass them in as an array. Example: `scopes: [:scope1, :scope2]`
+ # * :order - specify an order clause, defaults to 'LOWER(#{table}.#{value_method}) ASC'
#
# Be sure to add a route to reach the generated controller method. Example:
#
# resources :users do
# get :autocomplete_user_email, on: :collection
@@ -96,10 +97,11 @@
term = term.gsub(/[_%]/) { |x| "\\#{x}" } # escape any _'s or %'s in the search term
term = "#{term}%"
term = "%#{term}" if options[:full_search]
table_name = model.table_name
lower = options[:case_sensitive] ? '' : 'LOWER'
- ["#{lower}(#{table_name}.#{value_method}) LIKE #{lower}(?) ESCAPE \"\\\"", term]
+ ["#{lower}(#{table_name}.#{value_method}) LIKE #{lower}(?)", term] # escape default: \ on postgres, mysql, sqlite
+ # ["#{lower}(#{table_name}.#{value_method}) LIKE #{lower}(?) ESCAPE \"\\\"", term] # use single-quotes, not double
end
def autocomplete_limit_clause(options)
options[:limit] ||= 10
end