Skip to content

Commit

Permalink
Add unique option
Browse files Browse the repository at this point in the history
Use unique option to remove dulidate result:

    autocomplete :item, :brand, full: true, unique: true
  • Loading branch information
lingceng committed Feb 5, 2016
1 parent d563ed9 commit a33eb3e
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/rails-jquery-autocomplete/orm/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def active_record_get_autocomplete_items(parameters)
limit(limit).order(order)
items = items.where(where) unless where.blank?

if options[:unique]
scopes << lambda { unscope(:select).select("MIN(#{table_name}.#{model.primary_key}) as #{model.primary_key}, #{table_name}.#{method}").group(method) }
end

scopes.each do |scope|
items = case scope
when String
Expand All @@ -42,16 +46,17 @@ def active_record_get_autocomplete_items(parameters)
end

def get_autocomplete_select_clause(model, method, options)
if sqlite?
base = if sqlite?
table_name = model.quoted_table_name
([
"#{table_name}.#{model.connection.quote_column_name(model.primary_key)} as #{model.primary_key}",
"#{table_name}.#{model.connection.quote_column_name(method)} as #{method}"
] + (options[:extra_data].blank? ? [] : options[:extra_data]))
[
"#{table_name}.#{model.connection.quote_column_name(model.primary_key)} as #{model.primary_key}",
"#{table_name}.#{model.connection.quote_column_name(method)} as #{method}"
]
else
table_name = model.table_name
(["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + (options[:extra_data].blank? ? [] : options[:extra_data]))
["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"]
end
base + ( options[:extra_data] || [] )
end

def get_autocomplete_where_clause(model, term, method, options)
Expand Down

0 comments on commit a33eb3e

Please sign in to comment.