rails3-jquery-autocomplete 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +2 -0
- data/lib/rails3-jquery-autocomplete.rb +1 -0
- data/lib/rails3-jquery-autocomplete/formtastic.rb +25 -8
- data/lib/rails3-jquery-autocomplete/formtastic_plugin.rb +44 -0
- data/lib/rails3-jquery-autocomplete/orm/active_record.rb +1 -1
- data/lib/rails3-jquery-autocomplete/version.rb +1 -1
- data/test/lib/rails3-jquery-autocomplete/orm/active_record_test.rb +126 -126
- metadata +21 -20
data/CHANGELOG.md
CHANGED
@@ -3,6 +3,7 @@ require 'rails3-jquery-autocomplete/autocomplete'
|
|
3
3
|
|
4
4
|
module Rails3JQueryAutocomplete
|
5
5
|
autoload :Orm , 'rails3-jquery-autocomplete/orm'
|
6
|
+
autoload :FormtasticPlugin , 'rails3-jquery-autocomplete/formtastic_plugin'
|
6
7
|
|
7
8
|
unless ::Rails.version < "3.1"
|
8
9
|
require 'rails3-jquery-autocomplete/rails/engine'
|
@@ -2,20 +2,37 @@
|
|
2
2
|
# Load the formtastic plugin if using Formtastic
|
3
3
|
begin
|
4
4
|
require 'formtastic'
|
5
|
-
module Formtastic
|
6
|
-
module Inputs
|
7
|
-
class AutocompleteInput
|
8
|
-
include Base
|
9
|
-
include Base::Stringish
|
10
5
|
|
11
|
-
|
12
|
-
|
13
|
-
|
6
|
+
if Formtastic.constants.include?(:VERSION)
|
7
|
+
|
8
|
+
#
|
9
|
+
# Formtastic 2.0
|
10
|
+
#
|
11
|
+
|
12
|
+
|
13
|
+
module Formtastic
|
14
|
+
module Inputs
|
15
|
+
class AutocompleteInput
|
16
|
+
include Base
|
17
|
+
include Base::Stringish
|
18
|
+
|
19
|
+
def to_html
|
20
|
+
input_wrapping do
|
21
|
+
label_html <<
|
14
22
|
builder.autocomplete_field(method, options.delete(:url), input_html_options)
|
23
|
+
end
|
15
24
|
end
|
16
25
|
end
|
17
26
|
end
|
18
27
|
end
|
28
|
+
else
|
29
|
+
|
30
|
+
#
|
31
|
+
# Formtastic 1.x
|
32
|
+
#
|
33
|
+
class Formtastic::SemanticFormBuilder < ActionView::Helpers::FormBuilder
|
34
|
+
include Rails3JQueryAutocomplete::FormtasticPlugin
|
35
|
+
end
|
19
36
|
end
|
20
37
|
rescue LoadError
|
21
38
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Rails3JQueryAutocomplete
|
2
|
+
module FormtasticPlugin
|
3
|
+
def autocomplete_input(method, options = {})
|
4
|
+
if options.key?(:selected) || options.key?(:checked) || options.key?(:default)
|
5
|
+
::ActiveSupport::Deprecation.warn(
|
6
|
+
"The :selected, :checked (and :default) options are deprecated in Formtastic and will be removed from 1.0. " <<
|
7
|
+
"Please set default values in your models (using an after_initialize callback) or in your controller set-up. " <<
|
8
|
+
"See http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html for more information.", caller)
|
9
|
+
end
|
10
|
+
|
11
|
+
options[:required] = method_required?(method) unless options.key?(:required)
|
12
|
+
options[:as] ||= "autocompleted_string"
|
13
|
+
|
14
|
+
html_class = [ options[:as], (options[:required] ? :required : :optional) ]
|
15
|
+
html_class << 'error' if @object && @object.respond_to?(:errors) && !@object.errors[method.to_sym].blank?
|
16
|
+
|
17
|
+
wrapper_html = options.delete(:wrapper_html) || {}
|
18
|
+
wrapper_html[:id] ||= generate_html_id(method)
|
19
|
+
wrapper_html[:class] = (html_class << wrapper_html[:class]).flatten.compact.join(' ')
|
20
|
+
|
21
|
+
if options[:input_html] && options[:input_html][:id]
|
22
|
+
options[:label_html] ||= {}
|
23
|
+
options[:label_html][:for] ||= options[:input_html][:id]
|
24
|
+
end
|
25
|
+
|
26
|
+
input_parts = self.class.inline_order.dup
|
27
|
+
input_parts = input_parts - [:errors, :hints] if options[:as] == :hidden
|
28
|
+
|
29
|
+
list_item_content = input_parts.map do |type|
|
30
|
+
send(:"inline_#{type}_for", method, options)
|
31
|
+
end.compact.join("\n")
|
32
|
+
|
33
|
+
return template.content_tag(:li, Formtastic::Util.html_safe(list_item_content), wrapper_html)
|
34
|
+
end
|
35
|
+
|
36
|
+
alias_method :autocompleted_input, :autocomplete_input
|
37
|
+
|
38
|
+
|
39
|
+
protected
|
40
|
+
def autocompleted_string_input(method, options)
|
41
|
+
self.label(method, options_for_label(options)) << autocomplete_field(method, options.delete(:url), options)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -1,130 +1,130 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
module Rails3JQueryAutocomplete
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
4
|
+
module Orm
|
5
|
+
class ActiveRecordTest < Test::Unit::TestCase
|
6
|
+
include Rails3JQueryAutocomplete::Orm::ActiveRecord
|
7
|
+
|
8
|
+
context "#get_autocomplete_order" do
|
9
|
+
context 'order is specified' do
|
10
|
+
should 'returns that order option' do
|
11
|
+
assert_equal "field ASC", get_autocomplete_order(:field, {:order => 'field ASC'})
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'no order is specified' do
|
16
|
+
should 'return the order clause by the field ASC' do
|
17
|
+
assert_equal "field ASC", get_autocomplete_order(:field, {})
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'a different model is specified' do
|
21
|
+
should 'return the order clause by the table_name.field ASC' do
|
22
|
+
model = Object.new
|
23
|
+
mock(model).table_name { 'table_name' }
|
24
|
+
assert_equal "table_name.field ASC", get_autocomplete_order(:field, {}, model)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context '#get_autocomplete_items' do
|
31
|
+
should 'retrieve the items from ActiveRecord' do
|
32
|
+
class Dog ; end
|
33
|
+
|
34
|
+
model = Dog
|
35
|
+
scoped = []
|
36
|
+
whered = []
|
37
|
+
term = 'query'
|
38
|
+
method = :field
|
39
|
+
|
40
|
+
options = {
|
41
|
+
:model => model,
|
42
|
+
:term => term,
|
43
|
+
:method => method,
|
44
|
+
:options => {}
|
45
|
+
}
|
46
|
+
|
47
|
+
mock(self).get_autocomplete_limit(anything) { 10 }
|
48
|
+
mock(self).get_autocomplete_order(anything, anything, anything) { "order ASC" }
|
49
|
+
mock(self).get_autocomplete_select_clause(model, method, {}) { ["field"] }
|
50
|
+
mock(self).get_autocomplete_where_clause(model, term, method, {}) { ["WHERE something"] }
|
51
|
+
mock(model).table_name.times(any_times) { 'model_table_name' }
|
52
|
+
|
53
|
+
mock(model).scoped { model }
|
54
|
+
mock(model).select(["field"]) { model }
|
55
|
+
mock(model).where(["WHERE something"]).mock!.limit(10).mock!.
|
56
|
+
order("order ASC") { 1 }
|
57
|
+
|
58
|
+
assert_equal 1, get_autocomplete_items(options)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context '#get_autocomplete_select_clause' do
|
63
|
+
setup do
|
64
|
+
@model = Object.new
|
65
|
+
mock(@model).table_name { 'table_name' }
|
66
|
+
mock(@model).primary_key { 'id' }
|
67
|
+
end
|
68
|
+
|
69
|
+
should 'create a select clause' do
|
70
|
+
assert_equal ["table_name.id", "table_name.method"],
|
71
|
+
get_autocomplete_select_clause(@model, :method, {})
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'with extra options' do
|
75
|
+
should 'return those extra fields on the clause' do
|
76
|
+
options = {:extra_data => ['table_name.created_at']}
|
77
|
+
|
78
|
+
assert_equal ["table_name.id", "table_name.method", "table_name.created_at"],
|
79
|
+
get_autocomplete_select_clause(@model, :method, options)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context '#get_autocomplete_where_clause' do
|
85
|
+
setup do
|
86
|
+
@model = Object.new
|
87
|
+
mock(@model).table_name { 'table_name' }
|
88
|
+
|
89
|
+
@term = 'query'
|
90
|
+
@options = {}
|
91
|
+
@method = :method
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'Not Postgres' do
|
95
|
+
should 'return options for where' do
|
96
|
+
mock(self).postgres? { false }
|
97
|
+
assert_equal ["LOWER(table_name.method) LIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'Postgres' do
|
102
|
+
should 'return options for where with ILIKE' do
|
103
|
+
mock(self).postgres? { true }
|
104
|
+
assert_equal ["LOWER(table_name.method) ILIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'full search' do
|
109
|
+
should 'return options for where with the term sourrounded by %%' do
|
110
|
+
mock(self).postgres? { false }
|
111
|
+
@options[:full] = true
|
112
|
+
assert_equal ["LOWER(table_name.method) LIKE ?", "%query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
context '#postgres?' do
|
118
|
+
should 'return nil if PGconn is not defined' do
|
119
|
+
assert_nil self.postgres?
|
120
|
+
end
|
121
|
+
|
122
|
+
should 'return true if PGconn is defined' do
|
123
|
+
class ::PGconn ; end
|
124
|
+
|
125
|
+
assert self.postgres?
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
130
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails3-jquery-autocomplete
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-22 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70313670337240 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70313670337240
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: sqlite3-ruby
|
27
|
-
requirement: &
|
27
|
+
requirement: &70313670336720 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70313670336720
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: mongoid
|
38
|
-
requirement: &
|
38
|
+
requirement: &70313670336120 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 2.0.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70313670336120
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: mongo_mapper
|
49
|
-
requirement: &
|
49
|
+
requirement: &70313670335580 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0.9'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70313670335580
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: bson_ext
|
60
|
-
requirement: &
|
60
|
+
requirement: &70313670335000 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 1.3.0
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70313670335000
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: shoulda
|
71
|
-
requirement: &
|
71
|
+
requirement: &70313670334440 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 2.11.1
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70313670334440
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: uglifier
|
82
|
-
requirement: &
|
82
|
+
requirement: &70313670333940 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70313670333940
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: rr
|
93
|
-
requirement: &
|
93
|
+
requirement: &70313670333420 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *70313670333420
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: rcov
|
104
|
-
requirement: &
|
104
|
+
requirement: &70313670333000 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ! '>='
|
@@ -109,7 +109,7 @@ dependencies:
|
|
109
109
|
version: '0'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *70313670333000
|
113
113
|
description: Use jQuery's autocomplete plugin with Rails 3.
|
114
114
|
email: david.padilla@crowdint.com
|
115
115
|
executables: []
|
@@ -124,6 +124,7 @@ files:
|
|
124
124
|
- lib/rails3-jquery-autocomplete/autocomplete.rb
|
125
125
|
- lib/rails3-jquery-autocomplete/form_helper.rb
|
126
126
|
- lib/rails3-jquery-autocomplete/formtastic.rb
|
127
|
+
- lib/rails3-jquery-autocomplete/formtastic_plugin.rb
|
127
128
|
- lib/rails3-jquery-autocomplete/orm/active_record.rb
|
128
129
|
- lib/rails3-jquery-autocomplete/orm/mongo_mapper.rb
|
129
130
|
- lib/rails3-jquery-autocomplete/orm/mongoid.rb
|