Skip to content

Commit d563ed9

Browse files
committed
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) }]
1 parent 3438cdc commit d563ed9

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lib/rails-jquery-autocomplete/orm/active_record.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,20 @@ def active_record_get_autocomplete_items(parameters)
2424

2525
items = (::Rails::VERSION::MAJOR * 10 + ::Rails::VERSION::MINOR) >= 40 ? model.where(nil) : model.scoped
2626

27-
scopes.each { |scope| items = items.send(scope) } unless scopes.empty?
28-
2927
items = items.select(get_autocomplete_select_clause(model, method, options)) unless options[:full_model]
3028
items = items.where(get_autocomplete_where_clause(model, term, method, options)).
3129
limit(limit).order(order)
3230
items = items.where(where) unless where.blank?
3331

32+
scopes.each do |scope|
33+
items = case scope
34+
when String
35+
items.send(scope)
36+
when Proc
37+
items.instance_exec(&scope)
38+
end
39+
end
40+
3441
items
3542
end
3643

0 commit comments

Comments
 (0)