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

Search by several fields #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 22 additions & 4 deletions lib/rails-jquery-autocomplete/orm/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,24 @@ def active_record_get_autocomplete_items(parameters)
scopes = Array(options[:scopes])
where = options[:where]
limit = get_autocomplete_limit(options)
order = active_record_get_autocomplete_order(method, options, model)
order = Array.new

items = (::Rails::VERSION::MAJOR * 10 + ::Rails::VERSION::MINOR) >= 40 ? model.where(nil) : model.scoped

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

items = items.select(get_autocomplete_select_clause(model, method, options)) unless options[:full_model]
items = items.where(get_autocomplete_where_clause(model, term, method, options)).
limit(limit).order(order)

clauses = []
conditions = []
param_to_array(method).each do |method|
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 - method.

c = get_autocomplete_where_clause(model, term, method, options)
clauses << c[0]
conditions << c[1]
order << active_record_get_autocomplete_order(method, options, model)
end

items = items.where(clauses.join(' OR '), *conditions).limit(limit).order(order.join(', '))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Line is too long. [99/80]

items = items.where(where) unless where.blank?

items
Expand All @@ -43,7 +52,11 @@ def get_autocomplete_select_clause(model, method, options)
] + (options[:extra_data].blank? ? [] : options[:extra_data]))
else
table_name = model.table_name
(["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + (options[:extra_data].blank? ? [] : options[:extra_data]))
l = ["#{table_name}.#{model.primary_key}"]
param_to_array(method).each do |method|
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 - method.

l << "#{table_name}.#{method}"
end
(l + (options[:extra_data].blank? ? [] : options[:extra_data]))
end
end

Expand Down Expand Up @@ -78,6 +91,11 @@ def postgres?(model)
# Figure out if this particular model uses the PostgreSQL adapter
model.connection.class.to_s.match(/PostgreSQLAdapter/)
end

def param_to_array(s)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Inconsistent indentation detected.

return s if s.kind_of?(Array)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Prefer Object#is_a? over Object#kind_of?.

return [s]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Redundant return detected.

end
end
end
end