From d563ed9bd0e4cb672dc6e20645a88dcc29ac7f35 Mon Sep 17 00:00:00 2001 From: lingceng Date: Fri, 5 Feb 2016 12:24:37 +0800 Subject: [PATCH 1/2] Fix #45 Make scopes option can accepts Proc Scopes can also override the select and where. So you can use code like following to query unique result. autocomplete :item, :brand, full: true, scopes: [-> { unscope(:select).select('MIN(id) as id, brand').group(:brand) }] --- lib/rails-jquery-autocomplete/orm/active_record.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/rails-jquery-autocomplete/orm/active_record.rb b/lib/rails-jquery-autocomplete/orm/active_record.rb index 3754334..9348606 100644 --- a/lib/rails-jquery-autocomplete/orm/active_record.rb +++ b/lib/rails-jquery-autocomplete/orm/active_record.rb @@ -24,13 +24,20 @@ def active_record_get_autocomplete_items(parameters) items = (::Rails::VERSION::MAJOR * 10 + ::Rails::VERSION::MINOR) >= 40 ? model.where(nil) : model.scoped - scopes.each { |scope| items = items.send(scope) } unless scopes.empty? - items = items.select(get_autocomplete_select_clause(model, method, options)) unless options[:full_model] items = items.where(get_autocomplete_where_clause(model, term, method, options)). limit(limit).order(order) items = items.where(where) unless where.blank? + scopes.each do |scope| + items = case scope + when String + items.send(scope) + when Proc + items.instance_exec(&scope) + end + end + items end From 2a613e72ccd1e6cfc723444a57373409027c0f25 Mon Sep 17 00:00:00 2001 From: lingceng Date: Fri, 5 Feb 2016 15:21:19 +0800 Subject: [PATCH 2/2] Add unique option Use unique option to remove dulidate result: autocomplete :item, :brand, full: true, unique: true --- .../orm/active_record.rb | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/rails-jquery-autocomplete/orm/active_record.rb b/lib/rails-jquery-autocomplete/orm/active_record.rb index 9348606..c161100 100644 --- a/lib/rails-jquery-autocomplete/orm/active_record.rb +++ b/lib/rails-jquery-autocomplete/orm/active_record.rb @@ -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 << lambda do + select = "MIN(#{model.primary_key}) as #{model.primary_key}, #{method}" + unscope(:select).select(select).group(method) + end + end + scopes.each do |scope| items = case scope when String @@ -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)