Skip to content

Commit

Permalink
Merge remote-tracking branch 'github/master'
Browse files Browse the repository at this point in the history
* github/master:
  Bump version to 1.0.6
  Removed rcov since it's not even compatible with ruby 1.9.x
  The Postgres query differences are now treated at model level. This commits just adds the tests.
  Make sure travis is testing for Ruby 1.9.3
  Make sure that you're checking whether the specific model in question uses the PostgreSQL adapter, not only whether PGConn is defined for the entire app.  Fixes problems where multiple adapters are used in the same application, such as Issue crowdint#118.
  Option where for active record realized.
  Option :update_elements for SimpleForm implemented.
  • Loading branch information
hidde-jan committed Feb 18, 2012
2 parents bcd4700 + 229ac8e commit 7a944b0
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 22 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
rvm:
- 1.9.2
- 1.9.3
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Changelog

* 1.0.6 Postgres or non-postgres queries are now determined at model level
* 1.0.3 Fixed Formtastic 2.0 + Ruby 1.8.7 compat issue
* 1.0.2 Fixed issue #93, #94
* 1.0.1 Formtastic 2.0 compatibility fix
Expand Down
7 changes: 0 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ require 'bundler'
Bundler::GemHelper.install_tasks

require 'rake/testtask'
require 'rcov/rcovtask'

task :default => [:uglify, :test]

Expand All @@ -20,9 +19,3 @@ task :uglify do
end
end

Rcov::RcovTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/**/*_test.rb']
t.rcov_opts = %w{--exclude \/gems\/}
t.verbose = true
end
11 changes: 8 additions & 3 deletions lib/rails3-jquery-autocomplete/orm/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def get_autocomplete_items(parameters)
method = parameters[:method]
options = parameters[:options]
scopes = Array(options[:scopes])
where = options[:where]
limit = get_autocomplete_limit(options)
order = get_autocomplete_order(method, options, model)

Expand All @@ -25,6 +26,9 @@ def get_autocomplete_items(parameters)
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?

items
end

def get_autocomplete_select_clause(model, method, options)
Expand All @@ -35,12 +39,13 @@ def get_autocomplete_select_clause(model, method, options)
def get_autocomplete_where_clause(model, term, method, options)
table_name = model.table_name
is_full_search = options[:full]
like_clause = (postgres? ? 'ILIKE' : 'LIKE')
like_clause = (postgres?(model) ? 'ILIKE' : 'LIKE')
["LOWER(#{table_name}.#{method}) #{like_clause} ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"]
end

def postgres?
defined?(PGconn)
def postgres?(model)
# Figure out if this particular model uses the PostgreSQL adapter
model.connection.class.to_s.match(/PostgreSQLAdapter/)
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/rails3-jquery-autocomplete/simple_form_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module SimpleForm
module Inputs
class AutocompleteInput < Base
def input
@builder.autocomplete_field(attribute_name, options[:url], input_html_options)
@builder.autocomplete_field(attribute_name, options[:url], input_html_options.merge(update_elements(options[:update_elements])))
end

protected
Expand All @@ -14,6 +14,10 @@ def limit
def has_placeholder?
placeholder_present?
end

def update_elements(elements)
{'data-update-elements' => elements.to_json}
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rails3-jquery-autocomplete/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Rails3JQueryAutocomplete
VERSION = '1.0.5'
VERSION = '1.0.6'
end
1 change: 0 additions & 1 deletion rails3-jquery-autocomplete.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Gem::Specification.new do |s|
s.add_development_dependency('shoulda', '~>2.11.1')
s.add_development_dependency('uglifier')
s.add_development_dependency('rr')
s.add_development_dependency('rcov')

s.files = Dir['lib/**/*'] + %w{CHANGELOG.md LICENSE README.md Rakefile}
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down
33 changes: 24 additions & 9 deletions test/lib/rails3-jquery-autocomplete/orm/active_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Dog ; end
context '#get_autocomplete_where_clause' do
setup do
@model = Object.new
mock(@model).table_name { 'table_name' }
mock(@model).table_name { 'table_name' }

@term = 'query'
@options = {}
Expand All @@ -93,36 +93,51 @@ class Dog ; end

context 'Not Postgres' do
should 'return options for where' do
mock(self).postgres? { false }
mock(self).postgres?(@model) { false }
assert_equal ["LOWER(table_name.method) LIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
end
end

context 'Postgres' do
should 'return options for where with ILIKE' do
mock(self).postgres? { true }
mock(self).postgres?(@model) { true }
assert_equal ["LOWER(table_name.method) ILIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
end
end

context 'full search' do
should 'return options for where with the term sourrounded by %%' do
mock(self).postgres? { false }
mock(self).postgres?(@model) { false }
@options[:full] = true
assert_equal ["LOWER(table_name.method) LIKE ?", "%query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
end
end
end

context '#postgres?' do
should 'return nil if PGconn is not defined' do
assert_nil self.postgres?
setup do
@model = stub
end

should 'return true if PGconn is defined' do
class ::PGconn ; end
context 'the connection class is not postgres' do
setup do
mock(@model).connection { stub }
end

assert self.postgres?
should 'return nil if the connection class matches PostgreSQLAdapter' do
assert_nil self.postgres?(@model)
end
end

context 'the connection class matches PostgreSQLAdapter' do
setup do
class PostgreSQLAdapter; end
mock(@model).connection { PostgreSQLAdapter.new }
end

should 'return true' do
assert self.postgres?(@model)
end
end
end
end
Expand Down

0 comments on commit 7a944b0

Please sign in to comment.