Skip to content

Commit

Permalink
Fix #45 Make scopes option can accepts Proc
Browse files Browse the repository at this point in the history
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) }]
  • Loading branch information
lingceng committed Feb 5, 2016
1 parent 3438cdc commit d563ed9
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/rails-jquery-autocomplete/orm/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d563ed9

Please sign in to comment.