Module: RailsJQueryAutocomplete::Autocomplete

Included in:
ActionController::Base
Defined in:
lib/rails-jquery-autocomplete/autocomplete.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(target) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/rails-jquery-autocomplete/autocomplete.rb', line 3

def self.included(target)
  target.extend RailsJQueryAutocomplete::Autocomplete::ClassMethods

  target.send :include, RailsJQueryAutocomplete::Orm::Mongoid if defined?(Mongoid::Document)
  target.send :include, RailsJQueryAutocomplete::Orm::MongoMapper if defined?(MongoMapper::Document)
  target.send :include, RailsJQueryAutocomplete::Orm::ActiveRecord

end

Instance Method Details

#get_autocomplete_limit(options) ⇒ Object

Returns a limit that will be used on the query



77
78
79
# File 'lib/rails-jquery-autocomplete/autocomplete.rb', line 77

def get_autocomplete_limit(options)
  options[:limit] ||= 10
end

#get_object(model_sym) ⇒ Object

Returns parameter model_sym as a constant

get_object(:actor)
# returns a Actor constant supposing it is already defined


86
87
88
# File 'lib/rails-jquery-autocomplete/autocomplete.rb', line 86

def get_object(model_sym)
  model_sym.to_s.camelize.constantize
end

#json_for_autocomplete(items, method, extra_data = []) ⇒ Object

Returns a hash with three keys actually used by the Autocomplete jQuery-ui Can be overriden to show whatever you like Hash also includes a key/value pair for each method in extra_data



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rails-jquery-autocomplete/autocomplete.rb', line 95

def json_for_autocomplete(items, method, extra_data=[])
  items = items.collect do |item|
    hash = HashWithIndifferentAccess.new({"id" => item.id.to_s, "label" => item.send(method), "value" => item.send(method)})
    extra_data.each do |datum|
      hash[datum] = item.send(datum)
    end if extra_data
    # TODO: Come back to remove this if clause when test suite is better
    hash
  end
  if block_given?
    yield(items)
  else
    items
  end
end