Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple models #42

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
36 changes: 21 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,36 @@ 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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing space after #.

#both object and method can be specified by object or id
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing space after #.

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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Align the elements of a hash literal if they span more than one line.
Use the new Ruby 1.9 hash syntax.
Tab detected.

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. [117/80]
Unnecessary spacing detected.
Operator += should be surrounded with a single space.

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 +104,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