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

grosser / simple_auto_complete forked from david/simple_auto_complete

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

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (1)
    • v0.3.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.

Rails: Simple, customizable, unobstrusive - Autocomplete — Read more

  cancel

http://pragmatig.wordpress.com/2008/04/25/unobtrusive-autocomplete-rails-plugin/

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

This URL has Read+Write access

fix readme layout 
grosser (author)
Wed Dec 23 08:25:52 -0800 2009
commit  686e7ef4973955231999be54ede3eb7d91322023
tree    e266a8eff1acf9c1279bb194dceac2065a5e59f6
parent  42742e07f51eeaf1a3c97686e5aad55d511504fd
simple_auto_complete /
name age
history
message
file CHANGELOG Wed Dec 23 06:56:22 -0800 2009 add to changelog [grosser]
file README.markdown Wed Dec 23 08:25:52 -0800 2009 fix readme layout [grosser]
file Rakefile Wed Dec 23 08:23:43 -0800 2009 fix typo [grosser]
file VERSION Wed Dec 23 08:19:23 -0800 2009 initial version / gemspec [grosser]
directory example_js/ Sat Aug 15 07:12:46 -0700 2009 reformatting for viewing pleasure [grosser]
file init.rb Wed Dec 23 06:50:21 -0800 2009 refactor code injection to be simpler [grosser]
directory lib/ Wed Dec 23 08:22:03 -0800 2009 add SimpleAutocomplete::VERSION, remove unncece... [grosser]
file simple_autocomplete.gemspec Wed Dec 23 08:23:50 -0800 2009 Regenerated gemspec for version 0.3.0 [grosser]
directory spec/ Wed Dec 23 08:22:03 -0800 2009 add SimpleAutocomplete::VERSION, remove unncece... [grosser]
README.markdown
  • simple unobstrusive autocomplete
  • JS-library-independent
  • Controller and Model helpers

Install

As Rails plugin:

script/plugin install git://github.com/grosser/simple_auto_complete.git

Or As Gem:

sudo gem install simple_autocomplete

Then copy javascripts/css from example folder OR use your own

Examples

Controller

By default, autocomplete_for limits the results to 10 entries, and sorts by the autocomplete field.

class UsersController < ApplicationController
  autocomplete_for :user, :name
end

autocomplete_for takes a third parameter, an options hash which is used in the find:

autocomplete_for :user, :name, :limit => 15, :order => 'created_at DESC'

With a block you can generate any output you need (passed into render :inline):

autocomplete_for :post, :title do |items|
  items.map{|item| "#{item.title} -- #{item.id}"}.join("\n")
end

The items passed into the block is an ActiveRecord scope allowing further scopes to be chained:

autocomplete_for :post, :title do |items|
  items.for_user(current_user).map(&:title).join("\n")
end

View

<%= f.text_field :auto_user_name, :class => 'autocomplete', 'autocomplete_url'=>autocomplete_for_user_name_users_path %>

Routes

map.resources :users, :collection => { :autocomplete_for_user_name => :get}

JS

use any library you like (includes examples for jquery jquery.js + jquery.autocomplete.js + jquery.autocomplete.css)

jQuery(function($){//on document ready
  //autocomplete
  $('input.autocomplete').each(function(){
    var $input = $(this);
    $input.autocomplete($input.attr('autocomplete_url'));
  });
});

Records (Optional)

  • converts an auto_complete form field to an association on assignment
  • Tries to find the record by using find_by_autocomplete_xxx on the records model
  • unfound record -> nil
  • blank string -> nil
  • Controller find works independent of this find

Example for a post with autocompleted user name:

class User
  find_by_autocomplete :name # User.find_by_autocomplete_name('Michael')
end

class Post
  has_one :user
  autocomplete_for(:user, :name) #--> f.text_field :auto_user_name
  # OR
  autocomplete_for(:user, :name, :name=>:creator) #--> f.text_field :auto_creator_name (creator must a an User)
end

Authors

Inspired by DHH`s 'obstrusive' autocomplete_plugin.

Contributors (alphabetical)

  • Bryan Ash
  • David Leal

Michael Grosser
grosser.michael@gmail.com
Hereby placed under public domain, do what you want, just do not hold me accountable...

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