Skip to content

Commit

Permalink
added named scope items filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Michele Franzin committed May 7, 2010
1 parent 0196a02 commit 20a8464
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
12 changes: 7 additions & 5 deletions CHANGELOG
@@ -1,15 +1,17 @@
HEAD [2010-05-07]
====================

Features
Features
* named scope items filtering

Enhancements
Enhancements

Changes
Changes

Bugfixes

Bugfixes

0.1.0 [2010-02-03]
====================

First release.
First release.
44 changes: 41 additions & 3 deletions README.textile
@@ -1,8 +1,10 @@
h1. JrailsAutoComplete
h1. jRails autocomplete with unobtrusive javascript

This plugin adds autocomplete support to jrails[1], using jquery-ui[2];
it works similarly to the original rails auto complete, but generating unobtrusive
javascript.
javascript. Additional features:

* named scope items filtering, based on a customized list of autocomplete options.

*WARNING NOTE*: This is an active work in progress - currently only used on
few live projects right now. Use at your own discretion.
Expand All @@ -24,9 +26,43 @@ In your views:
<%= text_field_with_auto_complete :fruit, :name %>
</code></pre>

h2. 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.

h3. Example using anonymous scope:

<pre><code>auto_complete_for :fruit, :shape do |items, params|
items.scoped( { :conditions => [ "colour = ?", params['fruit']['color'] ] })
end
</code></pre>

h3. Example using named scope:

Having the model:

<pre><code>class Fruit < ActiveRecord::Base
belongs_to :owner
named_scope :by_owner,
lambda { |owner_name| {
:include => :owner,
:conditions => [ "owner.name = ?", owner_name ]
} }
end
</code></pre>

In the controller you can use the macro function as such

<pre><code>auto_complete_for :fruit, :name do | items, params |
items.by_owner(params['owner'])
end
</code></pre>

h2. Todo

* named scope filtering support
* made a gem
* test, test... test!

Expand All @@ -39,6 +75,8 @@ Please direct all queries to the issue tracker:
h2. Credits

Thanks to Paul Smith for the "original idea":http://github.com/elandesign/formtastic_autocomplete.git
and Pat Shaughnessy for "inspirating":http://patshaughnessy.net/repeated_auto_complete me
the named scope part.

h2. References

Expand Down
14 changes: 8 additions & 6 deletions lib/jrails_auto_complete.rb
Expand Up @@ -26,15 +26,17 @@ def self.included(base)
module ClassMethods
def auto_complete_for(object_name, method_name, options = {})
self.send(:define_method, "auto_complete_for_#{object_name}_#{method_name}") do
find_options = {
:conditions => [ "LOWER(#{method_name}) LIKE ?", '%' + params['term'].downcase + '%' ],
:order => "#{method_name} ASC",
model = object_name.to_s.camelize.constantize
full_method_name = "#{model.quoted_table_name}.#{method_name}"
find_options= {
:conditions => ["LOWER(#{full_method_name}) LIKE ?", '%' + params['term'].downcase + '%'],
:order => "#{full_method_name} ASC",
:limit => 10
}.merge!(options)

@items = object_name.to_s.camelize.constantize.find(:all, find_options)

@items = yield(@items) if block_given?
@items = model.scoped(find_options)
@items = yield(@items, params) if block_given?

render :inline => "<%= auto_complete_result @items, '#{method_name}', '#{params['term']}' %>"
end
end
Expand Down

0 comments on commit 20a8464

Please sign in to comment.