Module: AutocompleteRails::Controller::ClassMethods
- Defined in:
- lib/autocomplete_rails/controller.rb
Instance Method Summary collapse
-
#autocomplete(model_symbol, value_method, options = {}, &block) ⇒ Object
Generate an autocomplete controller action.
Instance Method Details
#autocomplete(model_symbol, value_method, options = {}, &block) ⇒ Object
Generate an autocomplete controller action.
The controller action is intended to interact with jquery UI’s autocomplete widget. The autocomplete controller provides suggestions while you type into a field that is provisioned with JQuery’s autocomplete widget.
The generated method is named “autocomplete_#model_symbol_#value_method”, for example:
class UsersController
autocomplete :user, :email
end
generates a method named ‘autocomplete_user_email`.
Parameters:
-
model_symbol - model class to autocomplete, e.g.
-
value_method - method on model to autocomplete. This supplies the ‘value’ field in results.
Also used as the label unless you supply options[:label_method].
-
options - hash of optional settings.
-
&block - an optional block to further modify the results set,
e.g. `{ |results| results.where(smth: @instance_variable) }`
Options accepts a hash of:
-
:label_method - call a separate method for the label, otherwise defaults to value_method. If your label
method is a method that is *not* a column in your DB, you may need options[:full_model].
-
:full_model - load full model instead of only selecting the specified values. Default is false.
-
:limit - default is 10.
-
:case_sensitive - if true, the search is case sensitive. Default is false.
-
:additional_data - collect additional data. Will be added to select unless full_model is invoked.
-
:full_search - search the entire value string for the term. Defaults to false, in which case the value
field being searched (see value_method above) must start with the search term.
-
:scopes - Build your autocomplete query from the specified ActiveRecord scope(s). Multiple scopes can be
used, pass them in as an array. Example: `scopes: [:scope1, :scope2]` If you need to pass additional arguments to your scope, define them as an array of arrays. Example: `scopes: [:scope1, [:scope2, 'argument 1', 'argument 2] ]`
-
:order - specify an order clause, defaults to ‘LOWER(#table.#value_method) ASC’
Be sure to add a route to reach the generated controller method. Example:
resources :users do
get :autocomplete_user_email, on: :collection
end
The following example searches for users by email, but displays their :full_name as the label. The full_model flag is also loaded, as full_name is a method that synthesizes multiple columns.
class UsersController
autocomplete :user, :email, label_method: full_name, full_model: true
end
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/autocomplete_rails/controller.rb', line 61 def autocomplete(model_symbol, value_method, = {}, &block) label_method = [:label_method] || value_method model = model_symbol.to_s.camelize.constantize autocomplete_method_name = "autocomplete_#{model_symbol}_#{value_method}" define_method(autocomplete_method_name) do results = autocomplete_results(model, value_method, label_method, , &block) render json: autocomplete_build_json(results, value_method, label_method, ), root: false end end |