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
40 changes: 26 additions & 14 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,42 @@ 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)
method("#{get_prefix(parameters[:model])}_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(
: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.

Use the new Ruby 1.9 hash syntax.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Indent the first parameter one step more than the start of the previous line.
Use the new Ruby 1.9 hash syntax.

:options => options,
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.

:term => term,
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.

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

Use the new Ruby 1.9 hash syntax.

)
new_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.

json += new_json unless new_json.nil?
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 +110,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
16 changes: 8 additions & 8 deletions test/lib/rails-jquery-autocomplete_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module RailsJQueryAutocomplete
class RailsJQueryAutocompleteTest < ActionController::TestCase
ActorsController = Class.new(ActionController::Base)
ActorsController.autocomplete(:movie, :name)
ActorsController.autocomplete(:action_name, { :movie => :name }, { :display_value => :name })
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. [97/80]
Redundant curly braces around a hash parameter.
Use the new Ruby 1.9 hash syntax.


class ::Movie ; end

Expand All @@ -15,22 +15,22 @@ class ::Movie ; end
end

should 'respond to the action' do
assert_respond_to @controller, :autocomplete_movie_name
assert_respond_to @controller, :autocomplete_action_name
end

should 'render the JSON items' do
mock(@controller).get_autocomplete_items({
:model => Movie, :method => :name, :options => @options, :term => "query"
}) { @items }
mock(@controller).get_autocomplete_items(
{ :model => Movie, :options => @options, :term => "query", :method => :name }
) { @items }

mock(@controller).json_for_autocomplete(@items, :name, nil)
get :autocomplete_movie_name, :term => 'query'
get :autocomplete_action_name, :term => 'query'
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.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end

context 'no term is specified' do
should "render an empty hash" do
mock(@controller).json_for_autocomplete({}, :name, nil)
get :autocomplete_movie_name
# mock(@controller).json_for_autocomplete(nil, {}, :name, nil)
get :autocomplete_action_name
end
end
end
Expand Down