rails-jquery-autocomplete 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ module RailsJQueryAutocomplete
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
@@ -0,0 +1,8 @@
1
+ module RailsJQueryAutocomplete
2
+ module Orm
3
+ autoload :ActiveRecord , 'rails-jquery-autocomplete/orm/active_record'
4
+ autoload :Mongoid , 'rails-jquery-autocomplete/orm/mongoid'
5
+ autoload :MongoMapper , 'rails-jquery-autocomplete/orm/mongo_mapper'
6
+ end
7
+ end
8
+
@@ -0,0 +1,56 @@
1
+ module RailsJQueryAutocomplete
2
+ module Orm
3
+ module ActiveRecord
4
+ def active_record_get_autocomplete_order(method, options, model=nil)
5
+ order = options[:order]
6
+
7
+ table_prefix = model ? "#{model.table_name}." : ""
8
+ order || "LOWER(#{table_prefix}#{method}) ASC"
9
+ end
10
+
11
+ def active_record_get_autocomplete_items(parameters)
12
+ model = parameters[:model]
13
+ term = parameters[:term]
14
+ options = parameters[:options]
15
+ method = options[:hstore] ? options[:hstore][:method] : parameters[:method]
16
+ scopes = Array(options[:scopes])
17
+ where = options[:where]
18
+ limit = get_autocomplete_limit(options)
19
+ order = active_record_get_autocomplete_order(method, options, model)
20
+
21
+
22
+ items = (::Rails::VERSION::MAJOR * 10 + ::Rails::VERSION::MINOR) >= 40 ? model.where(nil) : model.scoped
23
+
24
+ scopes.each { |scope| items = items.send(scope) } unless scopes.empty?
25
+
26
+ items = items.select(get_autocomplete_select_clause(model, method, options)) unless options[:full_model]
27
+ items = items.where(get_autocomplete_where_clause(model, term, method, options)).
28
+ limit(limit).order(order)
29
+ items = items.where(where) unless where.blank?
30
+
31
+ items
32
+ end
33
+
34
+ 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]))
37
+ end
38
+
39
+ def get_autocomplete_where_clause(model, term, method, options)
40
+ table_name = model.table_name
41
+ is_full_search = options[:full]
42
+ like_clause = (postgres?(model) ? 'ILIKE' : 'LIKE')
43
+ if options[:hstore]
44
+ ["LOWER(#{table_name}.#{method} -> '#{options[:hstore][:key]}') LIKE ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"]
45
+ else
46
+ ["LOWER(#{table_name}.#{method}) #{like_clause} ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"]
47
+ end
48
+ end
49
+
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
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,30 @@
1
+ module RailsJQueryAutocomplete
2
+ module Orm
3
+ module MongoMapper
4
+ def mongo_mapper_get_autocomplete_order(method, options, model=nil)
5
+ order = options[:order]
6
+ if order
7
+ order.split(',').collect do |fields|
8
+ sfields = fields.split
9
+ [sfields[0].downcase.to_sym, sfields[1].downcase.to_sym]
10
+ end
11
+ else
12
+ [[method.to_sym, :asc]]
13
+ end
14
+ end
15
+
16
+ def mongo_mapper_get_autocomplete_items(parameters)
17
+ model = parameters[:model]
18
+ method = parameters[:method]
19
+ options = parameters[:options]
20
+ is_full_search = options[:full]
21
+ term = parameters[:term]
22
+ limit = get_autocomplete_limit(options)
23
+ order = mongo_mapper_get_autocomplete_order(method, options)
24
+
25
+ search = (is_full_search ? '.*' : '^') + term + '.*'
26
+ items = model.where(method.to_sym => /#{search}/i).limit(limit).sort(order)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ module RailsJQueryAutocomplete
2
+ module Orm
3
+ module Mongoid
4
+ def mongoid_get_autocomplete_order(method, options, model=nil)
5
+ order = options[:order]
6
+ if order
7
+ order.split(',').collect do |fields|
8
+ sfields = fields.split
9
+ [sfields[0].downcase.to_sym, sfields[1].downcase.to_sym]
10
+ end
11
+ else
12
+ [[method.to_sym, :asc]]
13
+ end
14
+ end
15
+
16
+ def mongoid_get_autocomplete_items(parameters)
17
+ model = parameters[:model]
18
+ method = parameters[:method]
19
+ options = parameters[:options]
20
+ is_full_search = options[:full]
21
+ term = parameters[:term]
22
+ limit = get_autocomplete_limit(options)
23
+ order = mongoid_get_autocomplete_order(method, options)
24
+
25
+ if is_full_search
26
+ search = '.*' + Regexp.escape(term) + '.*'
27
+ else
28
+ search = '^' + Regexp.escape(term)
29
+ end
30
+ items = model.where(method.to_sym => /#{search}/i).limit(limit).order_by(order)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ module RailsJQueryAutocomplete
2
+ module Rails
3
+ class Engine < ::Rails::Engine ; end
4
+ end
5
+ end
@@ -0,0 +1,85 @@
1
+ module SimpleForm
2
+ module Inputs
3
+ module Autocomplete
4
+ #
5
+ # Method used to rename the autocomplete key to a more standard
6
+ # data-autocomplete key
7
+ #
8
+ def rewrite_autocomplete_option
9
+ new_options = {}
10
+ new_options["data-autocomplete-fields"] = JSON.generate(options.delete :fields) if options[:fields]
11
+ new_options["data-update-elements"] = JSON.generate(options.delete :update_elements) if options[:update_elements]
12
+ new_options["data-id-element"] = options.delete :id_element if options[:id_element]
13
+ input_html_options.merge new_options
14
+ end
15
+ end
16
+
17
+ class AutocompleteInput < Base
18
+ include Autocomplete
19
+
20
+ def input
21
+ @builder.autocomplete_field(
22
+ attribute_name,
23
+ options[:url],
24
+ rewrite_autocomplete_option
25
+ )
26
+ end
27
+
28
+ protected
29
+ def limit
30
+ column && column.limit
31
+ end
32
+
33
+ def has_placeholder?
34
+ placeholder_present?
35
+ end
36
+ end
37
+
38
+ class AutocompleteCollectionInput < CollectionInput
39
+ include Autocomplete
40
+
41
+ def input
42
+ # http://www.codeofficer.com/blog/entry/form_builders_in_rails_discovering_field_names_and_ids_for_javascript/
43
+ hidden_id = "#{object_name}_#{attribute_name}_hidden".gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
44
+ id_element = options[:id_element]
45
+ if id_element
46
+ id_element << ", #" << hidden_id
47
+ else
48
+ id_element = "#" + hidden_id
49
+ end
50
+ options[:id_element] = id_element
51
+ autocomplete_options = rewrite_autocomplete_option
52
+ #
53
+ label_method, value_method = detect_collection_methods
54
+ association = object.send(reflection.name)
55
+ if association && association.respond_to?(label_method)
56
+ autocomplete_options[:value] = association.send(label_method)
57
+ end
58
+ out = @builder.autocomplete_field(
59
+ attribute_name,
60
+ options[:url],
61
+ autocomplete_options
62
+ )
63
+ hidden_options = if association && association.respond_to?(value_method)
64
+ new_options = {}
65
+ new_options[:value] = association.send(value_method)
66
+ input_html_options.merge new_options
67
+ else
68
+ input_html_options
69
+ end
70
+ hidden_options[:id] = hidden_id
71
+ out << @builder.hidden_field(
72
+ attribute_name,
73
+ hidden_options
74
+ )
75
+ out.html_safe
76
+ end
77
+ end
78
+ end
79
+
80
+ class FormBuilder
81
+ map_type :autocomplete, :to => SimpleForm::Inputs::AutocompleteInput
82
+ map_type :autocomplete_collection, :to => SimpleForm::Inputs::AutocompleteCollectionInput
83
+ end
84
+
85
+ end
@@ -0,0 +1,3 @@
1
+ module RailsJQueryAutocomplete
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,12 @@
1
+ module Steak
2
+ module Autocomplete
3
+ def choose_autocomplete_result(text, input_id="input[data-autocomplete]")
4
+ page.execute_script %Q{ $('#{input_id}').trigger("focus") }
5
+ page.execute_script %Q{ $('#{input_id}').trigger("keydown") }
6
+ sleep 1
7
+ page.execute_script %Q{ $('.ui-menu-item a:contains("#{text}")').trigger("mouseenter").trigger("click"); }
8
+ end
9
+ end
10
+ end
11
+
12
+ RSpec.configuration.include Steak::Autocomplete, :type => :acceptance
metadata ADDED
@@ -0,0 +1,250 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-jquery-autocomplete
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Padilla
8
+ - Joiey Seeley
9
+ - Sundus Yousuf
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-01-20 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '4.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '4.0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: sqlite3-ruby
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: mongoid
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: 2.0.0
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: 2.0.0
57
+ - !ruby/object:Gem::Dependency
58
+ name: mongo_mapper
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0.9'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0.9'
71
+ - !ruby/object:Gem::Dependency
72
+ name: mongo
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.6.2
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ~>
83
+ - !ruby/object:Gem::Version
84
+ version: 1.6.2
85
+ - !ruby/object:Gem::Dependency
86
+ name: bson_ext
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ version: 1.6.2
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ version: 1.6.2
99
+ - !ruby/object:Gem::Dependency
100
+ name: guard
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ - !ruby/object:Gem::Dependency
114
+ name: guard-test
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ type: :development
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ - !ruby/object:Gem::Dependency
128
+ name: test-unit
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 2.2.0
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ~>
139
+ - !ruby/object:Gem::Version
140
+ version: 2.2.0
141
+ - !ruby/object:Gem::Dependency
142
+ name: shoulda
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ~>
146
+ - !ruby/object:Gem::Version
147
+ version: 3.0.1
148
+ type: :development
149
+ prerelease: false
150
+ version_requirements: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ~>
153
+ - !ruby/object:Gem::Version
154
+ version: 3.0.1
155
+ - !ruby/object:Gem::Dependency
156
+ name: uglifier
157
+ requirement: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ! '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ type: :development
163
+ prerelease: false
164
+ version_requirements: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ! '>='
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ - !ruby/object:Gem::Dependency
170
+ name: rr
171
+ requirement: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ! '>='
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ type: :development
177
+ prerelease: false
178
+ version_requirements: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ! '>='
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ - !ruby/object:Gem::Dependency
184
+ name: simple_form
185
+ requirement: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ~>
188
+ - !ruby/object:Gem::Version
189
+ version: '1.5'
190
+ type: :development
191
+ prerelease: false
192
+ version_requirements: !ruby/object:Gem::Requirement
193
+ requirements:
194
+ - - ~>
195
+ - !ruby/object:Gem::Version
196
+ version: '1.5'
197
+ description: Use jQuery's autocomplete plugin with Rails 4+.
198
+ email: david.padilla@crowdint.com joiey.seeley@gmail.com sundusahmedyousuf@gmail.com
199
+ executables: []
200
+ extensions: []
201
+ extra_rdoc_files: []
202
+ files:
203
+ - lib/assets/javascripts/autocomplete-rails-uncompressed.js
204
+ - lib/assets/javascripts/autocomplete-rails.js
205
+ - lib/cucumber/autocomplete.rb
206
+ - lib/generators/autocomplete/install_generator.rb
207
+ - lib/generators/autocomplete/uncompressed_generator.rb
208
+ - lib/rails-jquery-autocomplete/autocomplete.rb
209
+ - lib/rails-jquery-autocomplete/form_helper.rb
210
+ - lib/rails-jquery-autocomplete/formtastic.rb
211
+ - lib/rails-jquery-autocomplete/formtastic_plugin.rb
212
+ - lib/rails-jquery-autocomplete/orm/active_record.rb
213
+ - lib/rails-jquery-autocomplete/orm/mongo_mapper.rb
214
+ - lib/rails-jquery-autocomplete/orm/mongoid.rb
215
+ - lib/rails-jquery-autocomplete/orm.rb
216
+ - lib/rails-jquery-autocomplete/rails/engine.rb
217
+ - lib/rails-jquery-autocomplete/simple_form_plugin.rb
218
+ - lib/rails-jquery-autocomplete/version.rb
219
+ - lib/rails-jquery-autocomplete.rb
220
+ - lib/steak/autocomplete.rb
221
+ - CHANGELOG.md
222
+ - LICENSE
223
+ - README.md
224
+ - Rakefile
225
+ homepage: http://github.com/crowdint/rails-jquery-autocomplete
226
+ licenses:
227
+ - MIT
228
+ metadata: {}
229
+ post_install_message:
230
+ rdoc_options: []
231
+ require_paths:
232
+ - lib
233
+ required_ruby_version: !ruby/object:Gem::Requirement
234
+ requirements:
235
+ - - ! '>='
236
+ - !ruby/object:Gem::Version
237
+ version: '0'
238
+ required_rubygems_version: !ruby/object:Gem::Requirement
239
+ requirements:
240
+ - - ! '>='
241
+ - !ruby/object:Gem::Version
242
+ version: '0'
243
+ requirements: []
244
+ rubyforge_project:
245
+ rubygems_version: 2.1.11
246
+ signing_key:
247
+ specification_version: 4
248
+ summary: Use jQuery's autocomplete plugin with Rails 4+.
249
+ test_files: []
250
+ has_rdoc: