-
Notifications
You must be signed in to change notification settings - Fork 80
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -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| | ||
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. Prefer |
||
hash = HashWithIndifferentAccess.new({"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. [130/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. [130/80] |
||
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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
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. [114/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. 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 | ||
|
||
|
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.
Prefer
map
overcollect
.