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
Open

Conversation

bluefalcon26
Copy link

Refactored to allow search of multiple model/method combinations. Simplifies optional parameters.


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
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), \
: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.

:model => get_object(object),
: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.

Use the new Ruby 1.9 hash syntax.

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

}) { @items }
mock(@controller).get_autocomplete_items(
Movie,
{ :method => :name, :options => @options, :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.

Redundant curly braces around a hash parameter.
Use the new Ruby 1.9 hash syntax.


mock(@controller).json_for_autocomplete(@items, :name, nil)
get :autocomplete_movie_name, :term => 'query'
get :autocomplete_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.

:model => Movie, :method => :name, :options => @options, :term => "query"
}) { @items }
mock(@controller).get_autocomplete_items(
{ :model => Movie, :method => :name, :options => @options, :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.

Line is too long. [89/80]
Redundant curly braces around a hash parameter.
Indent the first parameter one step more than the start of the previous line.
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)
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 #.

:method => method
)
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.

@bigtunacan
Copy link
Collaborator

@bluefalcon26 Looks like you put some time into this one. Can you provide some additional details on the use case here? Thanks.

@bluefalcon26
Copy link
Author

@bigtunacan We have one "search" box at the top of our site that we want to autocomplete matching on two models, employers and schools. Somewhat like Facebook has the search box that searches people, photos, pages, etc. Except this isn't a true search, it's just meant to allow autocomplete and navigation to those models' pages. Does that make sense?

@bluefalcon26
Copy link
Author

@bigtunacan We now have this in our search controller:

# GET :autocomplete_institutions
institutions = { employer: :name, organization: :name }
autocomplete :institutions, institutions

You could also do

# GET :autocomplete_institutions
institutions = { employer: :name, employer: :about, employer: :location } # etc
autocomplete :institutions, institutions

@bigtunacan
Copy link
Collaborator

@bluefalcon26 each individuals result set for the autocomplete can get pretty complicated, reaching across multiple models, possibly needing to aggregate, de-dupe data, etc...

It sounds like is true of the situation you had as well. Typically users add the get_autocomplete_items method to their controller and then override this to bring back the data needed for such a customized search.

Since I don't know your particular use case though; I may be missing something here.

@bluefalcon26
Copy link
Author

The only problem I can see with the way I've done it would be possible duplicates if someone searched the same model with multiple methods. Should I implement something to automatically cull duplicate results?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants