Sha256: 1cf72e88ad15ea9745ffc3b60bdc9178550616455b4d075f659f03abc5fdf1b2

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

module JqueryAutoComplete 
  
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def auto_complete_for(object, method, options = {})
      define_method("auto_complete_for_#{object}_#{method}") do
        object_constant = object.to_s.camelize.constantize
        
        find_options = { 
          :conditions => [ "LOWER(#{method}) LIKE ?", '%' + params[:term].downcase + '%' ], 
          :order => "#{method} ASC",
          :limit => 10 }.merge!(options)
        
        render :json => object_constant.to_auto_complete(:json, find_options)
      end
    end
  end  
end

module JqueryAutoCompleteJson
  include ActionView::Helpers::JavaScriptHelper
  include HandleAttributes

  def to_auto_complete(format, attributes)
    case format
      when :json
        json = %Q([)
        each do |elem|
          couples = elem.attributes.symbolize_keys
          json << %Q({ "id": "#{get_atr_value(elem, attributes[0], couples)}",)
          json << %Q( "label": "#{get_atr_value(elem, attributes[1], couples)}",)
          json << %Q( "value": "#{get_atr_value(elem, attributes[2], couples).gsub(/<\/?[^>]*>/, "")}" },)
        end
        json.chop! << " ]"
    end
  end
end

class Array
  include JqueryAutoCompleteJson
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
compass-jquery-plugin-0.3.2.3 lib/jquery/jquery_auto_complete.rb
compass-jquery-plugin-0.3.2.2 lib/jquery/jquery_auto_complete.rb