Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom_search + documentation #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ Passing an array of attributes/column names to this option will fetch and return
autocomplete :brand, :name, :extra_data => [:slogan]
end

#### :full_model

By default, your search will only return the required columns from the database needed to populate your form, namely id and the column you are searching (name, in the above example).

Passing true to this option will return all column from the table.

class ProductsController < Admin::BaseController
autocomplete :brand, :name, :full_model => true
end

#### :display_value

If you want to display a different version of what you're looking for, you can use the :display_value option.
Expand Down Expand Up @@ -166,6 +176,21 @@ Only the object's id and the column you are searching on will be returned in JSO
By default autocomplete uses method name as column name. Now it can be specified using column_name options
`:column_name => 'name'`

#### :where
If you want to add extra conditions to search add the conditions as a parameter.

class ProductsController < Admin::BaseController
autocomplete :brand, :name, :where => {:id => 3}
end

#### :custom_search
If you want to execute a custom search with arel or Squeel, just pass it as string.

class ProductsController < Admin::BaseController
autocomplete :brand, :name, :custom_search => 'where{(name =~ "%#{term}%") | (code =~"%#{term}%")}'
end


#### json encoder
Autocomplete uses Yajl as JSON encoder/decoder, but you can specify your own

Expand Down
7 changes: 5 additions & 2 deletions lib/rails3-jquery-autocomplete/orm/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ def get_autocomplete_items(parameters)
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 = if options[:custom_search]
eval("items.#{options[:custom_search]}")
else
items.where(get_autocomplete_where_clause(model, term, method, options))
end.limit(limit).order(order)
items = items.where(where) unless where.blank?

items
Expand Down