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 203a4ea
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 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,13 @@ def active_record_get_autocomplete_items(parameters)
limit(limit).order(order)
items = items.where(where) unless where.blank?

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

scopes.each do |scope|
items = case scope
when String
Expand All @@ -42,16 +49,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 203a4ea

Please sign in to comment.