Skip to content

Commit a7e96d9

Browse files
committed
Fixes for SQLITE schema (dot prefix named) tables
1 parent 80b824e commit a7e96d9

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Changelog
22

3+
* 1.0.3
4+
* Fixes issue with SQLITE to support schemas
35
* 1.0.2
46
* Fixes compatibility with newer versions of Simple Form gem
57
* Fixes issue of not propagating events and issue of selected value not a string
@@ -9,7 +11,7 @@
911
* Option to enable/disable no matches found labeling
1012
* 1.0.0
1113
* Adds support for Rails 4
12-
14+
1315
___
1416

1517
Changes from here on are for the rails3-jquery-autocomplete gem release versions

lib/rails-jquery-autocomplete/orm/active_record.rb

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
5680
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module RailsJQueryAutocomplete
2-
VERSION = '1.0.2'
2+
VERSION = '1.0.3'
33
end

rails-jquery-autocomplete.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
1818
s.add_development_dependency 'sqlite3-ruby'
1919
s.add_development_dependency 'mongoid', '>= 2.0.0'
2020
s.add_development_dependency 'mongo_mapper', '>= 0.9'
21+
#s.add_development_dependency 'mongo', '~> 1.6.2'
2122
s.add_development_dependency 'bson_ext', '~> 1.6.2'
2223
s.add_development_dependency 'guard'
2324
s.add_development_dependency 'guard-test'
@@ -26,6 +27,7 @@ Gem::Specification.new do |s|
2627
s.add_development_dependency 'uglifier'
2728
s.add_development_dependency 'rr'
2829
s.add_development_dependency 'simple_form', '~>1.5'
30+
s.add_development_dependency 'debugger'
2931

3032
s.files = Dir['lib/**/*'] + %w{CHANGELOG.md LICENSE README.md Rakefile}
3133
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")

0 commit comments

Comments
 (0)