-
Notifications
You must be signed in to change notification settings - Fork 80
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
base: master
Are you sure you want to change the base?
Multiple models #42
Changes from 18 commits
d076658
41f5a44
2b5a44f
79fdd56
e238eb8
2f5aa5e
9408d66
da5c46e
78acac4
6a0a337
de031c9
743777c
38f59e5
71abe4c
4697867
9794d25
bc153a3
b5e7803
065247f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -49,30 +49,41 @@ 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| | ||
method("#{get_prefix(get_object(object))}_get_autocomplete_order").call(method, options, model) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the new Ruby 1.9 hash syntax. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
:options => options, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the new Ruby 1.9 hash syntax. |
||
:term => term, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the new Ruby 1.9 hash syntax. |
||
:method => method | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the new Ruby 1.9 hash syntax. |
||
) | ||
json += json_for_autocomplete(items, \ | ||
options[:display_value] ||= method, options[:extra_data], &block) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [81/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [81/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [81/80] |
||
end | ||
end | ||
|
||
render :json => json_for_autocomplete(items, options[:display_value] ||= method, options[:extra_data], &block), root: false | ||
render :json => json, root: false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the new Ruby 1.9 hash syntax. |
||
end | ||
end | ||
end | ||
|
@@ -98,7 +109,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) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [97/80] |
||
|
||
class ::Movie ; end | ||
|
||
|
@@ -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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the new Ruby 1.9 hash syntax. |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space after #. |
||
get :autocomplete_action_name | ||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
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.