github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

patshaughnessy / auto_complete forked from rails/auto_complete

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 31
    • 42
  • Source
  • Commits
  • Network (42)
  • Issues (1)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

auto_complete plugin refactored to handle complex forms and named scopes — Read more

  cancel

http://patshaughnessy.net/repeated_auto_complete

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

use rails/init.rb for tests 
patshaughnessy (author)
Mon Dec 14 20:24:03 -0800 2009
commit  1e92a46a438d2a7a293b6052f07276a2c44ff1f5
tree    29ac98262e74a76d0d3abdeeb41a438aee2f41ed
parent  529f29d699ae197b7505133e2b0a23d348990363
auto_complete /
name age
history
message
file .gitignore Wed Nov 25 10:10:17 -0800 2009 repeated_auto_complete gem and view_mapper module [patshaughnessy]
file README.rdoc Mon Dec 14 20:22:21 -0800 2009 rdoc for README [patshaughnessy]
file Rakefile Wed Nov 25 10:10:17 -0800 2009 repeated_auto_complete gem and view_mapper module [patshaughnessy]
file VERSION Wed Nov 25 10:10:17 -0800 2009 repeated_auto_complete gem and view_mapper module [patshaughnessy]
directory lib/ Wed Nov 25 10:10:17 -0800 2009 repeated_auto_complete gem and view_mapper module [patshaughnessy]
directory rails/ Wed Nov 25 10:10:17 -0800 2009 repeated_auto_complete gem and view_mapper module [patshaughnessy]
file repeated_auto_complete.gemspec Wed Nov 25 10:10:17 -0800 2009 repeated_auto_complete gem and view_mapper module [patshaughnessy]
directory test/ Mon Dec 14 20:24:03 -0800 2009 use rails/init.rb for tests [patshaughnessy]
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.

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

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server