Module: ClientSideAutocomplete::Orm::ActiveRecord
- Defined in:
- lib/client_side_autocomplete/orm/active_record.rb
Instance Method Summary collapse
- #get_autocomplete_items(parameters) ⇒ Object
- #get_autocomplete_order(method, options, model = nil) ⇒ Object
- #get_autocomplete_select_clause(model, method, options) ⇒ Object
- #get_autocomplete_where_clause(model, term, method, options) ⇒ Object
- #postgres? ⇒ Boolean
Instance Method Details
#get_autocomplete_items(parameters) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/client_side_autocomplete/orm/active_record.rb', line 11 def get_autocomplete_items(parameters) model = parameters[:model] term = parameters[:term] method = parameters[:method] = parameters[:options] scopes = Array([:scopes]) limit = get_autocomplete_limit() order = get_autocomplete_order(method, , model) items = model.scoped scopes.each { |scope| items = items.send(scope) } unless scopes.empty? items = items.select(get_autocomplete_select_clause(model, method, )) unless [:full_model] items = items.where(get_autocomplete_where_clause(model, term, method, )). limit(limit).order(order) end |
#get_autocomplete_order(method, options, model = nil) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/client_side_autocomplete/orm/active_record.rb', line 4 def get_autocomplete_order(method, , model=nil) order = [:order] table_prefix = model ? "#{model.table_name}." : "" order || "#{table_prefix}#{method} ASC" end |
#get_autocomplete_select_clause(model, method, options) ⇒ Object
30 31 32 33 |
# File 'lib/client_side_autocomplete/orm/active_record.rb', line 30 def get_autocomplete_select_clause(model, method, ) table_name = model.table_name (["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + ([:extra_data].blank? ? [] : [:extra_data])) end |
#get_autocomplete_where_clause(model, term, method, options) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/client_side_autocomplete/orm/active_record.rb', line 35 def get_autocomplete_where_clause(model, term, method, ) table_name = model.table_name is_full_search = [:full] like_clause = (postgres? ? 'ILIKE' : 'LIKE') ["LOWER(#{table_name}.#{method}) #{like_clause} ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"] end |
#postgres? ⇒ Boolean
42 43 44 |
# File 'lib/client_side_autocomplete/orm/active_record.rb', line 42 def postgres? defined?(PGConn) end |