auto_complete plugin refactored to handle complex forms and named scopes
Ruby
Pull request Compare This branch is 3 commits ahead of patshaughnessy:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
rails
test
.gitignore
README.rdoc
Rakefile
VERSION
repeated_auto_complete.gemspec

README.rdoc

Repeated Auto Complete

Fork of the standard auto_complete plugin to support:

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

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

See: patshaughnessy.net/repeated_auto_complete for details.

adamico's fork

Working on rails3 compatibility

Install as a gem

gem sources -a http://gemcutter.org
sudo gem install repeated_auto_complete

and in config/environment.rb:

Rails::Initializer.run do |config|

  config.gem "repeated_auto_complete"

end

Install as a plugin

script/plugin install git://github.com/patshaughnessy/auto_complete.git

Repeated autocomplete text fields:

A “text_field_with_auto_complete” method is made available to the form builder yielded by form_for and fields_for that:

  • Insures unique id's for <input> and <div> tags by inserting unique integers into their id attributes

  • 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

  • Supports nested attributes in Rails 2.3

form.text_field_with_auto_complete works the same as the original text_field_with_auto_complete macro, except it does not take the object as a parameter.

Example with nested attributes using Rails 2.3 or later:

class Project < ActiveRecord::Base
  has_many :tasks
  accepts_nested_attributes_for :tasks, :allow_destroy => true
end

<% form_for @project do |project_form| %>
  <p>
    <%= project_form.label :name, "Project:" %>
    <%= project_form.text_field_with_auto_complete :name, {}, {:method => :get } %>
  </p>
  <% project_form.fields_for :tasks do |task_form| %>
    <p>
      <%= task_form.label :name, "Task:" %>
      <%= task_form.text_field_with_auto_complete :name, {}, { :method => :get, :skip_style => true } %>
    </p>
  <% end %>
<% end %>

Rails 2.2 and earlier example:

<% for person in @group.people %>
  <% fields_for "group[person_attributes][]", person do |person_form| %>
    <p>
      Person <%= person_form.label :name %><br/>
      <%= person_form.text_field_with_auto_complete :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

Autocomplete Scaffolding:

The “View Mapper” gem will generate scaffolding code that illustrates how to use the standard Rails auto_complete plugin in a simple form, or the repeated_auto_complete plugin/gem in a complex form.

See: patshaughnessy.net/2009/10/1/auto_complete-scaffolding

Or: patshaughnessy.net/2009/11/25/scaffolding-for-auto-complete-on-a-complex-nested-form

Copyright © 2009 [Pat Shaughnessy], released under the MIT license