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

Fix #45 Make scopes option can accepts Proc #48

Open
wants to merge 2 commits 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
31 changes: 23 additions & 8 deletions lib/rails-jquery-autocomplete/orm/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,42 @@ def active_record_get_autocomplete_items(parameters)

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)
items = items.where(where) unless where.blank?

if options[:unique]
scopes << lambda do
select = "MIN(#{model.primary_key}) as #{model.primary_key}, #{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. [83/80]

unscope(:select).select(select).group(method)
end
end

scopes.each do |scope|
items = case scope
when String
items.send(scope)
when Proc
items.instance_exec(&scope)
end
end

items
end

def get_autocomplete_select_clause(model, method, options)
if sqlite?
base = if sqlite?
table_name = model.quoted_table_name
([
"#{table_name}.#{model.connection.quote_column_name(model.primary_key)} as #{model.primary_key}",
"#{table_name}.#{model.connection.quote_column_name(method)} as #{method}"
] + (options[:extra_data].blank? ? [] : options[:extra_data]))
[
"#{table_name}.#{model.connection.quote_column_name(model.primary_key)} as #{model.primary_key}",
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. [109/80]

"#{table_name}.#{model.connection.quote_column_name(method)} as #{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. [86/80]

]
else
table_name = model.table_name
(["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + (options[:extra_data].blank? ? [] : options[:extra_data]))
["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"]
end
base + (options[:extra_data] || [])
end

def get_autocomplete_where_clause(model, term, method, options)
Expand Down