Skip to content
37 changes: 22 additions & 15 deletions lib/rails-jquery-autocomplete/autocomplete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def self.included(target)
# end
#
module ClassMethods
def autocomplete(object, method, options = {}, &block)
def autocomplete(name, object_method_hash, options = {}, &block)

define_method("get_prefix") do |model|
if defined?(Mongoid::Document) && model.include?(Mongoid::Document)
Expand All @@ -49,30 +49,37 @@ def autocomplete(object, method, options = {}, &block)
'active_record'
end
end
define_method("get_autocomplete_order") do |method, options, model=nil|
method("#{get_prefix(get_object(options[:class_name] || object))}_get_autocomplete_order").call(method, options, model)
define_method("get_autocomplete_order") do |object, method, options, model=nil|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shadowing outer local variable - options.
Line is too long. [87/80]
Surrounding space missing in default value assignment.

method("#{get_prefix(get_object(object))}_get_autocomplete_order").call(method, options, model)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [105/80]

end

define_method("get_autocomplete_items") do |parameters|
method("#{get_prefix(get_object(options[:class_name] || object))}_get_autocomplete_items").call(parameters)
define_method("get_autocomplete_items") do |object, parameters|
method("#{get_prefix(get_object(object))}_get_autocomplete_items").call(parameters)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [93/80]

end

define_method("autocomplete_#{object}_#{method}") do
# var = {:object => :method, "class_name" => "column_id", \
# :object => "column_id", "class_name" => :method}
# #Pass parameters as autocomplete :name, var
# :name creates the name of the action
define_method("autocomplete_#{name}") do

method = options[:column_name] if options.has_key?(:column_name)
json = Array.new

term = params[:term]

if term && !term.blank?
#allow specifying fully qualified class name for model object
class_name = options[:class_name] || object
items = get_autocomplete_items(:model => get_object(class_name), \
:options => options, :term => term, :method => method)
else
items = {}

object_method_hash.each do |object, method|
# allow specifying fully qualified class name for model object
# both object and method can be specified by object or id
items = get_autocomplete_items(object, :model => get_object(object), \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [84/80]
Use the new Ruby 1.9 hash syntax.

:options => options, :term => term, :method => method)
json += json_for_autocomplete(items, \
options[:display_value] ||= method, options[:extra_data], &block)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [81/80]
Align the parameters of a method call if they span more than one line.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [81/80]
Align the parameters of a method call if they span more than one line.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [81/80]
Align the parameters of a method call if they span more than one line.

end
end

render :json => json_for_autocomplete(items, options[:display_value] ||= method, options[:extra_data], &block), root: false
render :json => json, root: false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the new Ruby 1.9 hash syntax.

end
end
end
Expand All @@ -98,7 +105,7 @@ def get_object(model_sym)
#
def json_for_autocomplete(items, method, extra_data=[])
items = items.collect do |item|
hash = {"id" => item.id.to_s, "label" => item.send(method), "value" => item.send(method)}
hash = { "id" => item.id.to_s, "label" => item.send(method), "value" => item.send(method) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [99/80]

extra_data.each do |datum|
hash[datum] = item.send(datum)
end if extra_data
Expand Down