@@ -5,7 +5,11 @@ def active_record_get_autocomplete_order(method, options, model=nil)
55 order = options [ :order ]
66
77 table_prefix = model ? "#{ model . table_name } ." : ""
8- order || "LOWER(#{ table_prefix } #{ method } ) ASC"
8+ if sqlite?
9+ order || "LOWER(#{ method } ) ASC"
10+ else
11+ order || "LOWER(#{ table_prefix } #{ method } ) ASC"
12+ end
913 end
1014
1115 def active_record_get_autocomplete_items ( parameters )
@@ -18,7 +22,6 @@ def active_record_get_autocomplete_items(parameters)
1822 limit = get_autocomplete_limit ( options )
1923 order = active_record_get_autocomplete_order ( method , options , model )
2024
21-
2225 items = ( ::Rails ::VERSION ::MAJOR * 10 + ::Rails ::VERSION ::MINOR ) >= 40 ? model . where ( nil ) : model . scoped
2326
2427 scopes . each { |scope | items = items . send ( scope ) } unless scopes . empty?
@@ -32,8 +35,16 @@ def active_record_get_autocomplete_items(parameters)
3235 end
3336
3437 def get_autocomplete_select_clause ( model , method , options )
35- table_name = model . table_name
36- ( [ "#{ table_name } .#{ model . primary_key } " , "#{ table_name } .#{ method } " ] + ( options [ :extra_data ] . blank? ? [ ] : options [ :extra_data ] ) )
38+ if sqlite?
39+ table_name = model . quoted_table_name
40+ ( [
41+ "#{ table_name } .#{ model . connection . quote_column_name ( model . primary_key ) } as #{ model . primary_key } " ,
42+ "#{ table_name } .#{ model . connection . quote_column_name ( method ) } as #{ method } "
43+ ] + ( options [ :extra_data ] . blank? ? [ ] : options [ :extra_data ] ) )
44+ else
45+ table_name = model . table_name
46+ ( [ "#{ table_name } .#{ model . primary_key } " , "#{ table_name } .#{ method } " ] + ( options [ :extra_data ] . blank? ? [ ] : options [ :extra_data ] ) )
47+ end
3748 end
3849
3950 def get_autocomplete_where_clause ( model , term , method , options )
@@ -42,15 +53,28 @@ def get_autocomplete_where_clause(model, term, method, options)
4253 like_clause = ( postgres? ( model ) ? 'ILIKE' : 'LIKE' )
4354 if options [ :hstore ]
4455 [ "LOWER(#{ table_name } .#{ method } -> '#{ options [ :hstore ] [ :key ] } ') LIKE ?" , "#{ ( is_full_search ? '%' : '' ) } #{ term . downcase } %" ]
56+ elsif sqlite?
57+ [ "LOWER(#{ method } ) #{ like_clause } ?" , "#{ ( is_full_search ? '%' : '' ) } #{ term . downcase } %" ]
4558 else
4659 [ "LOWER(#{ table_name } .#{ method } ) #{ like_clause } ?" , "#{ ( is_full_search ? '%' : '' ) } #{ term . downcase } %" ]
4760 end
4861 end
4962
50- def postgres? ( model )
51- # Figure out if this particular model uses the PostgreSQL adapter
52- model . connection . class . to_s . match ( /PostgreSQLAdapter/ )
53- end
63+ protected
64+
65+ def sqlite?
66+ begin
67+ return ::ActiveRecord ::Base . connection . to_s . match ( /SQLite/ )
68+ rescue ::ActiveRecord ::ConnectionNotEstablished
69+ return false
70+ end
71+ return false
72+ end
73+
74+ def postgres? ( model )
75+ # Figure out if this particular model uses the PostgreSQL adapter
76+ model . connection . class . to_s . match ( /PostgreSQLAdapter/ )
77+ end
5478 end
5579 end
5680end
0 commit comments