Skip to content

Allow use of pluck instead of select #47

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/rails-jquery-autocomplete/autocomplete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,20 @@ def get_object(model_sym)
# Hash also includes a key/value pair for each method in extra_data
#
def json_for_autocomplete(items, method, extra_data=[])
items = items.collect do |item|
hash = HashWithIndifferentAccess.new({"id" => item.id.to_s, "label" => item.send(method), "value" => item.send(method)})
extra_data.each do |datum|
hash[datum] = item.send(datum)
end if extra_data
# TODO: Come back to remove this if clause when test suite is better
hash
if items.first && items.first.is_a?(Hash)
# Query was a "pluck" and items are already hashes
items.each do |item|
item["label"] = item["value"] = item[method.to_s]
end
else
items = items.collect do |item|
Copy link
Collaborator

Choose a reason for hiding this comment

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

Prefer map over collect.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Prefer map over collect.

hash = HashWithIndifferentAccess.new({"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. [130/80]
Redundant curly braces around a hash parameter.
Space inside { missing.
Space inside } missing.

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. [130/80]
Redundant curly braces around a hash parameter.
Space inside { missing.
Space inside } missing.

extra_data.each do |datum|
hash[datum] = item.send(datum)
end if extra_data
# TODO: Come back to remove this if clause when test suite is better
hash
end
end
if block_given?
yield(items)
Expand Down
15 changes: 13 additions & 2 deletions lib/rails-jquery-autocomplete/orm/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,22 @@ def active_record_get_autocomplete_items(parameters)

scopes.each { |scope| items = items.send(scope) } unless scopes.empty?

items = items.select(get_autocomplete_select_clause(model, method, options)) unless options[:full_model]
select_method = options[:select_method] || :select

if select_method == :select
items = items.select(get_autocomplete_select_clause(model, method, options)) unless options[:full_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. [114/80]

Copy link
Author

Choose a reason for hiding this comment

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

it's not MY line

end
items = items.where(get_autocomplete_where_clause(model, term, method, options)).
limit(limit).order(order)
items = items.where(where) unless where.blank?

if select_method == :pluck
columns = get_autocomplete_select_clause(model, method, options).uniq
items = items.pluck(*columns)
columns = columns.map do |column|
column.sub(/^#{model.table_name}\./, "")
end
items = items.map { |item| Hash[columns.zip(item)] }
end
items
end

Expand Down
3 changes: 3 additions & 0 deletions test/lib/rails-jquery-autocomplete/autocomplete_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class AutocompleteTest < Test::Unit::TestCase
should 'parse items to JSON' do
item = mock(Object)
mock(item).send(:name).times(2) { 'Object Name' }
stub(item).is_a?(Hash) { false }
mock(item).id { 1 }
items = [item]
response = self.json_for_autocomplete(items, :name).first
Expand All @@ -65,6 +66,7 @@ class AutocompleteTest < Test::Unit::TestCase
should 'return an instance of HashWithIndifferentAccess' do
item = mock(Object)
mock(item).send(:name).times(2) { 'Object Name' }
stub(item).is_a?(Hash) { false }
mock(item).id { 1 }
items = [item]
response = self.json_for_autocomplete(items, :name).first
Expand All @@ -77,6 +79,7 @@ class AutocompleteTest < Test::Unit::TestCase
should 'add that extra data to result' do
item = mock(Object)
mock(item).send(:name).times(2) { 'Object Name' }
stub(item).is_a?(Hash) { false }
mock(item).id { 1 }
mock(item).send("extra") { 'Object Extra ' }

Expand Down