Skip to content

Commit

Permalink
update readme with :match option
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed May 8, 2010
1 parent 1a8f535 commit 35ab1fe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
15 changes: 11 additions & 4 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ and sorts by the autocomplete field.
`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):

With :match option you can filter for different columns

# find and order with firstname/lastname
# return full_name of records for auto-completion
autocomplete_for :user, :full_name, :match => [:firstname, :lastname]

With a block you can generate any output you need (ERB allowed):

autocomplete_for :post, :title do |items|
items.map{|item| "#{item.title} -- #{item.id}"}.join("\n")
items.map{|item| "#{item.title} -- #{item.id} <%= Time.now %>"}.join("\n")
end

The items passed into the block is an ActiveRecord scope allowing further scopes to be chained:
Expand Down Expand Up @@ -91,8 +97,9 @@ Authors
Inspired by DHH`s 'obstrusive' autocomplete_plugin.

###Contributors (alphabetical)
- [Bryan Ash](http://bryan-ash.blogspot.com/)
- [Bryan Ash](http://bryan-ash.blogspot.com)
- [David Leal](http://github.com/david)
- [mlessard](http://github.com/mlessard)
- [Splendeo](http://www.splendeo.es)

[Michael Grosser](http://pragmatig.wordpress.com)
Expand Down
31 changes: 3 additions & 28 deletions lib/simple_autocomplete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,7 @@ module SimpleAutocomplete
VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
end

#Example:
#
# # Controller
# class BlogController < ApplicationController
# autocomplete_for :post, :title
# end
#
# # View
# <%= text_field :post, title, :class => 'autocomplete', 'data-autocomplete-url'=>autocomplete_for_post_title_posts_path %>
#
# #routes.rb
# map.resources :users, :collection => { :autocomplete_for_user_name => :get}
#
# # Options
# By default, autocomplete_for limits the results to 10 entries,
# and sorts by the given field.
#
# autocomplete_for takes a third parameter, an options hash to
# the find method used to search for the records:
#
# autocomplete_for :post, :title, :limit => 15, :order => 'created_at DESC'
#
# # Block
# 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
# see Readme for details
class ActionController::Base
def self.autocomplete_for(object, method, options = {}, &block)
options = options.dup
Expand Down Expand Up @@ -62,6 +36,7 @@ def self.autocomplete_for(object, method, options = {}, &block)
# -> the auto_user_name field will be resolved to a User, using User.find_by_autocomplete_name(value)
# -> Post has autocomplete_for('user','name')
# -> User has find_by_autocomplete('name')
# see Readme for more details
class ActiveRecord::Base
def self.autocomplete_for(model, attribute, options={})
name = options[:name] || model.to_s.underscore
Expand Down Expand Up @@ -92,4 +67,4 @@ def self.find_by_autocomplete(attr)
self.first(:conditions => [ "LOWER(#{attr}) = ?", value.to_s.downcase ])
end
end
end
end

0 comments on commit 35ab1fe

Please sign in to comment.