antunderwood / auto_complete

auto_complete plugin refactored to handle complex forms and named scopes

Home Page:http://patshaughnessy.net/repeated_auto_complete

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fork of the standard auto_complete plugin to support:

1. Creating auto complete text fields that can be repeated more than once a single form.

2. Using auto_complete_for with named scopes to display a customized list
of autocomplete options.

See: http://patshaughnessy.net/repeated_auto_complete for details.


Repeated autocomplete text fields:
==================================

A "text_field_with_auto_complete" method is made available to form_for and fields_for that:
- Insures unique id's for <input> and <div> tags
- Uses the object name from the surrounding call to form_for or fields_for so attribute
mass assignment will work as usual
- Works with the same server side controller method, auto_complete_for, as usual

Example:

<% for person in @group.people %>
  <% auto_complete_fields_for "group[person_attributes][]", person do |person_form| %>
    <p>
      Person <%= person_form.label :name %><br/>
      <%= person_form.text_field_with_auto_complete :person, :name, {}, {:method => :get }  %>
    </p>
  <% end %>
<% end %>


Named scopes with auto_complete_for:
====================================

auto_complete_for now optionally accepts a block that is called with the item list and HTTP parameters
when the auto complete AJAX request is received. This block can be used to specify that a named scope
be used to generate a customized list of autocomplete options.

Example using anonymous scope:

auto_complete_for :some_model, :some_other_field do |items, params|
  items.scoped( { :conditions => [ "a_third_field = ?", params['some_model']['a_third_field'] ] })
end



Example using named scope:

class Task < ActiveRecord::Base
  belongs_to :project
  named_scope :by_project,
    lambda { |project_name| {
      :include => :project,
      :conditions => [ "projects.name = ?", project_name ]
    } }
end

auto_complete_for :task, :name do | items, params |
  items.by_project(params['project'])
end


Copyright (c) 2009 [Pat Shaughnessy], released under the MIT license

About

auto_complete plugin refactored to handle complex forms and named scopes

http://patshaughnessy.net/repeated_auto_complete