rails-autocomplete 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +51 -0
  3. data/LICENSE +20 -0
  4. data/README.md +453 -0
  5. data/Rakefile +21 -0
  6. data/lib/assets/javascripts/autocomplete-rails-uncompressed.js +197 -0
  7. data/lib/assets/javascripts/autocomplete-rails.js +1 -0
  8. data/lib/cucumber/autocomplete.rb +6 -0
  9. data/lib/generators/autocomplete/install_generator.rb +14 -0
  10. data/lib/generators/autocomplete/uncompressed_generator.rb +14 -0
  11. data/lib/rails-jquery-autocomplete.rb +23 -0
  12. data/lib/rails-jquery-autocomplete/autocomplete.rb +112 -0
  13. data/lib/rails-jquery-autocomplete/form_helper.rb +49 -0
  14. data/lib/rails-jquery-autocomplete/formtastic.rb +41 -0
  15. data/lib/rails-jquery-autocomplete/formtastic_plugin.rb +44 -0
  16. data/lib/rails-jquery-autocomplete/orm.rb +6 -0
  17. data/lib/rails-jquery-autocomplete/orm/active_record.rb +83 -0
  18. data/lib/rails-jquery-autocomplete/rails/engine.rb +5 -0
  19. data/lib/rails-jquery-autocomplete/simple_form_plugin.rb +100 -0
  20. data/lib/rails-jquery-autocomplete/version.rb +3 -0
  21. data/lib/steak/autocomplete.rb +12 -0
  22. data/test/form_helper_test.rb +25 -0
  23. data/test/generators/autocomplete/install_generator_test.rb +25 -0
  24. data/test/generators/autocomplete/uncompressed_generator_test.rb +25 -0
  25. data/test/lib/rails-jquery-autocomplete/autocomplete_test.rb +93 -0
  26. data/test/lib/rails-jquery-autocomplete/orm/active_record_test.rb +190 -0
  27. data/test/lib/rails-jquery-autocomplete/simple_form_plugin_test.rb +33 -0
  28. data/test/lib/rails-jquery-autocomplete_test.rb +38 -0
  29. data/test/test_helper.rb +35 -0
  30. data/test/view_test_helper.rb +108 -0
  31. metadata +196 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5b73e5f1fa989395f289f2ce373bba760046f705
4
+ data.tar.gz: 32a45fa485d1430b5f0fbbcb11b168b52c477e4b
5
+ SHA512:
6
+ metadata.gz: fadde783a8ac0a5abb2ff68b172dad1027f9869643798d5932f3494c0f2be509e6626f1783e9c3bc3f481fe366c7bd709ffaeccc5f46472532f624486757efe2
7
+ data.tar.gz: c3aebe8997906f4858f562a3a0af167483ab58a5cbb301a1e0c5049820b6345cb2c01262a031b1debecae37a57766e37d4e867234605f0e8eb901f678324104a
data/CHANGELOG.md ADDED
@@ -0,0 +1,51 @@
1
+ # Changelog
2
+
3
+ * 1.0.4
4
+ * Add data-min-length attribute (alias to min-length attribute) for better consistency.
5
+ * Added railsAutocomplete.init trigger
6
+ * 1.0.3
7
+ * Fixes issue with SQLITE to support schemas
8
+ * 1.0.2
9
+ * Fixes compatibility with newer versions of Simple Form gem
10
+ * Fixes issue of not propagating events and issue of selected value not a string
11
+ * Fixes bug where form 'false' values are treated as true due to being
12
+ a string value.
13
+ * 1.0.1
14
+ * Option to enable/disable no matches found labeling
15
+ * 1.0.0
16
+ * Adds support for Rails 4
17
+
18
+ ___
19
+
20
+ Changes from here on are for the rails3-jquery-autocomplete gem release versions
21
+
22
+
23
+ * 1.0.11
24
+ * mongoid: escape regular expression in search
25
+ * When possible, use jQuery .on() rather than .live()
26
+ * 1.0.6 Postgres or non-postgres queries are now determined at model level
27
+ * 1.0.3 Fixed Formtastic 2.0 + Ruby 1.8.7 compat issue
28
+ * 1.0.2 Fixed issue #93, #94
29
+ * 1.0.1 Formtastic 2.0 compatibility fix
30
+ * 1.0.0 Rails 3.1 asset pipeline support
31
+ * 0.9.1 Fixes issues #96 and #32
32
+ * 0.9.0 Massive rewrite
33
+ * 0.8.0 Compressed JS file
34
+ * 0.7.5 Pull request #46
35
+ * 0.7.4 Allows Rails 3.1
36
+ * 0.7.3 MongoMapper
37
+ * 0.7.2 Steak helper
38
+ * 0.7.1 Fixed joined scopes (Issue #43)
39
+ * 0.7.0 Scopes
40
+ * 0.6.6 ILIKE for postgres
41
+ * 0.6.5 JS select event
42
+ * 0.6.4 Use YAJL instead of JSON
43
+ * 0.6.3 SimpleForm plugin
44
+ * 0.6.2 Fix Issue #8
45
+ * 0.6.1 Allow specifying fully qualified class name for model object as an option to autocomplete
46
+ * 0.6.0 JS Code cleanup
47
+ * 0.5.1 Add STI support
48
+ * 0.5.0 Formtastic support
49
+ * 0.4.0 MongoID support
50
+ * 0.3.6 Using .live() to put autocomplete on dynamic fields
51
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 David Padilla
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,453 @@
1
+ ## Status
2
+
3
+ This is a maintained fork of [rails-jquery-autocomplete](http://github.com/bigtunacan/rails-jquery-autocomplete)
4
+
5
+ An easy way to use jQuery's autocomplete with Rails.
6
+
7
+ Supports ActiveRecord only.
8
+
9
+ Works with [Formtastic](http://github.com/justinfrench/formtastic)
10
+ and [SimpleForm](https://github.com/plataformatec/simple_form)
11
+
12
+ ## ActiveRecord
13
+
14
+ You can find a [detailed example](http://github.com/crowdint/rails3-jquery-autocomplete-app)
15
+ on how to use this gem with ActiveRecord [here](http://github.com/crowdint/rails3-jquery-autocomplete-app).
16
+
17
+ ## Before you start
18
+
19
+ Make sure your project is using jQuery-UI and the autocomplete widget
20
+ before you continue.
21
+
22
+ You can find more info about that here:
23
+
24
+ * http://jquery.com/
25
+ * http://jqueryui.com/demos/autocomplete/
26
+ * http://github.com/rails/jquery-ujs
27
+
28
+ I'd encourage you to understand how to use those 3 amazing tools before attempting to use this gem.
29
+
30
+ ## Installing
31
+
32
+ Include the gem on your Gemfile
33
+
34
+ gem 'rails-autocomplete'
35
+
36
+ Install it
37
+
38
+ bundle install
39
+
40
+ #### Upgrading from older versions
41
+
42
+ If you are upgrading from a previous version, run the generator after installing to replace the javascript file.
43
+
44
+ rails generate autocomplete:install
45
+
46
+ I'd recommend you do this every time you update to make sure you have the latest JS file.
47
+
48
+ #### Uncompressed Javascript file
49
+
50
+ If you want to make changes to the JS file, you can install the
51
+ uncompressed version by running:
52
+
53
+ rails generate autocomplete:uncompressed
54
+
55
+ ### Rails 4 and higher
56
+
57
+ Just add it to your app/assets/javascripts/application.js file
58
+
59
+ //= require jquery
60
+ //= require jquery_ujs
61
+ //= require jquery-ui/autocomplete
62
+ //= require autocomplete-rails
63
+
64
+ ## Usage
65
+
66
+ ### Demo Application by [Yifei](https://github.com/yifeiwu)
67
+
68
+ A live demo can be seen
69
+
70
+ [here](https://rocky-thicket-9286.herokuapp.com/)
71
+
72
+ [source](https://github.com/yifeiwu/rails4-autocomplete-demo)
73
+
74
+ As a new developer, I had some issues getting this to work by following the documentation. However after trying some things and reading [Yoni Weisbrod](http://www.yoniweisbrod.com/autocomplete-magic-with-rails/)'s blog post, I was able to make the autocomplete work and implement a few useful features.
75
+
76
+ ## Some implemented features
77
+
78
+ 1. The css has been changed such that the results show up better against the box of suggestions. See `<app/assets/stylesheets/food.scss>` for details. I obtained this from a gist(forgive me I don't remember who the author is at the moment, please contact me if you do and I'll give credit). Upon mouseover/arrowkey presses, the selection will be highlighted.
79
+
80
+ 2. One Yoni's improvements, you can click on a suggested item to submit the search, instead of having to click on the submit button after clicking on the item. This is an example of how to hook onto the *railsAutocomplete.select* event.
81
+
82
+ 3. The autocomplete is implemented in the context of a search form with a simple scope search(see the food model and controller).
83
+
84
+ ### Model Example
85
+
86
+ Assuming you have a Brand model that contains a name attribute:
87
+
88
+ class Brand < ActiveRecord::Base
89
+ end
90
+
91
+ create_table :brand do |t|
92
+ t.column :name, :string
93
+ end
94
+
95
+ ### Controller
96
+
97
+ To set up the required action on your controller, all you have to do is call it with the class name and the method
98
+ as in the following example:
99
+
100
+ class ProductsController < Admin::BaseController
101
+ autocomplete :brand, :name
102
+ end
103
+
104
+ This will create an action _autocomplete_brand_name_ on your controller, don't forget to add it on your routes file
105
+
106
+ resources :products do
107
+ get :autocomplete_brand_name, :on => :collection
108
+ end
109
+
110
+ Verify this path using `rake routes`, you will need it later for the **view** section.
111
+
112
+ ### Options
113
+
114
+ #### :full => true
115
+
116
+ By default, the search starts from the beginning of the string you're searching for. If you want to do a full search, set the _full_ parameter to true.
117
+
118
+ class ProductsController < Admin::BaseController
119
+ autocomplete :brand, :name, :full => true
120
+ end
121
+
122
+ The following terms would match the query 'un':
123
+
124
+ * Luna
125
+ * Unacceptable
126
+ * Rerun
127
+
128
+ #### :full => false (default behavior)
129
+
130
+ Only the following terms would match the query 'un':
131
+
132
+ * Unacceptable
133
+
134
+ #### :limit => 10 (default behavior)
135
+
136
+ By default your search result set is limited to the first 10 records. This can be overridden by specifying the limit option.
137
+
138
+ #### :extra_data
139
+
140
+ By default, your search will only return the required columns from the database needed to populate your form, namely id and the column you are searching (name, in the above example).
141
+
142
+ Passing an array of attributes/column names to this option will fetch and return the specified data.
143
+
144
+ class ProductsController < Admin::BaseController
145
+ autocomplete :brand, :name, :extra_data => [:slogan]
146
+ end
147
+
148
+ #### :display_value
149
+
150
+ If you want to display a different version of what you're looking for, you can use the :display_value option.
151
+
152
+ This options receives a method name as the parameter, and that method will be called on the instance when displaying the results.
153
+
154
+ class Brand < ActiveRecord::Base
155
+ def funky_method
156
+ "#{self.name}.camelize"
157
+ end
158
+ end
159
+
160
+
161
+ class ProductsController < Admin::BaseController
162
+ autocomplete :brand, :name, :display_value => :funky_method
163
+ end
164
+
165
+ In the example above, you will search by _name_, but the autocomplete list will display the result of _funky_method_
166
+
167
+ This wouldn't really make much sense unless you use it with the "id_element" attribute. (See below)
168
+
169
+ Only the object's id and the column you are searching on will be returned in JSON, so if your display_value method requires another parameter, make sure to fetch it with the :extra_data option
170
+
171
+ #### :hstore
172
+
173
+ Added option to support searching in hstore columns.
174
+
175
+ Pass a hash with two keys: `:method` and `:key` with values: the hstore field name and the key of the hstore to search.
176
+
177
+ e.g `autocomplete :feature, :name, :hstore => {:method => 'name_translations', :key => 'en'}`
178
+
179
+
180
+ #### :scopes
181
+
182
+ Added option to use scopes. Pass scopes in an array.
183
+ e.g `:scopes => [:scope1, :scope2]`
184
+
185
+ #### :column_name
186
+
187
+ By default autocomplete uses method name as column name. Now it can be specified using column_name options
188
+ `:column_name => 'name'`
189
+
190
+ #### :case_sensitive
191
+
192
+ Normally autocomplete performs a case insensitive search. In cases where this is not desirable, or causes too high
193
+ a performance penalty, search can be made case sensitive by specifying `:case_sensitive => true`
194
+
195
+ #### json encoder
196
+
197
+ Autocomplete uses Yajl as JSON encoder/decoder, but you can specify your own
198
+
199
+ class ProductsController < Admin::BaseController
200
+ autocomplete :brand, :name do |items|
201
+ CustomJSON::Encoder.encode(items)
202
+ end
203
+ end
204
+
205
+ ### View
206
+
207
+ On your view, all you have to do is include the attribute autocomplete on the text field
208
+ using the url to the autocomplete action as the value.
209
+
210
+ form_for @product do |f|
211
+ f.autocomplete_field :brand_name, autocomplete_brand_name_products_path
212
+ end
213
+
214
+ This will generate an HTML tag that looks like:
215
+
216
+ <input type="text" data-autocomplete="products/autocomplete_brand_name">
217
+
218
+ If you are not using a FormBuilder (form_for) or you just want to include an autocomplete field without the form, you can use the
219
+ *autocomplete_field_tag* helper.
220
+
221
+ form_tag 'some/path'
222
+ autocomplete_field_tag 'address', '', address_autocomplete_path, :size => 75
223
+ end
224
+
225
+ #### Multiple Values Separated by Delimiter
226
+
227
+ To generate an autocomplete input field that accepts multiple values separated by a given delimiter, add the `'data-delimiter'` and `:multiple` options:
228
+
229
+ form_for @product do |f|
230
+ f.autocomplete_field :brand_names, autocomplete_brand_name_products_path,
231
+ 'data-delimiter' => ',', :multiple => true
232
+ end
233
+
234
+ NOTE: Setting the `:multiple` option to `true` will result in the chosen values being submitted as an array. Leaving this option off will result in the values being passed as a single string, with the values separated by your chosen delimiter.
235
+
236
+ #### Automatically focus on the first autocompleted item
237
+
238
+ To have the first item be automatically focused on when the autocomplete menu is shown, add the `'data-auto-focus'` option and set it to `true`.
239
+
240
+ form_for @product do |f|
241
+ f.autocomplete_field :brand_names, autocomplete_brand_name_products_path,
242
+ 'data-auto-focus' => true
243
+ end
244
+
245
+ Now your autocomplete code is unobtrusive, Rails style.
246
+
247
+ #### Client-side config
248
+
249
+ To configure the behaviour if no matches are found, you can set the following options:
250
+
251
+ jQuery.railsAutocomplete.options.showNoMatches //default true
252
+ jQuery.railsAutocomplete.options.noMatchesLabel //default 'no existing match'
253
+
254
+ These will change the behaviour globally. To set them on a single input field use:
255
+
256
+ f.autocomplete_field :brand_names, autocomplete_brand_name_products_path,
257
+ 'data-showNoMatches' => false
258
+ #or
259
+ f.autocomplete_field :brand_names, autocomplete_brand_name_products_path,
260
+ 'data-noMatchesLabel' => 'no brands found'
261
+
262
+
263
+ ### Getting the object id
264
+
265
+ If you need to use the id of the selected object, you can use the *id_element* attribute too:
266
+
267
+ f.autocomplete_field :brand_name, autocomplete_brand_name_products_path, :id_element => '#some_element'
268
+
269
+ This will update the field with id *#some_element with the id of the selected object. The value for this option can be any jQuery selector.
270
+
271
+ ### Changing destination element
272
+
273
+ If you need to change destination element where the autocomplete box will be appended to, you can use the **:append_to** option which generates a **data-append-to** HTML attribute that is used in jQuery autocomplete as append_to attribute.
274
+
275
+ The :append_to option accepts a string containing jQuery selector for destination element:
276
+
277
+ f.autocomplete_field :product_name, '/products/autocomplete_product_name', :append_to => "#product_modal"
278
+
279
+ The previous example would append the autocomplete box containing suggestions to element jQuery('#product_modal').
280
+ This is very useful on page where you use various z-indexes and you need to append the box to the topmost element, for example using modal window.
281
+
282
+ ### Sending extra search fields
283
+
284
+ If you want to send extra fields from your form to the search action,
285
+ you can use the **:fields** options which generates a **data-autocomplete-fields**
286
+ HTML attribute.
287
+
288
+ The :fields option accepts a hash where the keys represent the Ajax request
289
+ parameter name and the values represent the jQuery selectors to retrieve the
290
+ form elements to get the values:
291
+
292
+ f.autocomplete_field :product_name, '/products/autocomplete_product_name', :fields => {:brand_id => '#brand_element', :country => '#country_element'}
293
+
294
+ class ProductsController < Admin::BaseController
295
+ def autocomplete_product_name
296
+ term = params[:term]
297
+ brand_id = params[:brand_id]
298
+ country = params[:country]
299
+ products = Product.where('brand = ? AND country = ? AND name LIKE ?', brand_id, country, "%#{term}%").order(:name).all
300
+ render :json => products.map { |product| {:id => product.id, :label => product.name, :value => product.name} }
301
+ end
302
+ end
303
+
304
+ ### Getting extra object data
305
+
306
+ If you need to extra data about the selected object, you can use the *:update_elements* HTML attribute.
307
+
308
+ The :update_elements attribute accepts a hash where the keys represent the object attribute/column data to use to update and the values are jQuery selectors to retrieve the HTML element to update:
309
+
310
+ f.autocomplete_field :brand_name, autocomplete_brand_name_products_path, :update_elements => {:id => '#id_element', :slogan => '#some_other_element'}
311
+
312
+ class ProductsController < Admin::BaseController
313
+ autocomplete :brand, :name, :extra_data => [:slogan]
314
+ end
315
+
316
+ The previous example would fetch the extra attribute slogan and update jQuery('#some_other_element') with the slogan value.
317
+
318
+ ### Running custom code on selection
319
+
320
+ A javascript event named *railsAutocomplete.select* is fired on the input field when a value is selected from the autocomplete drop down. If you need to do something more complex than update fields with data, you can hook into this event, like so:
321
+
322
+ $('#my_autocomplete_field').bind('railsAutocomplete.select', function(event, data){
323
+ /* Do something here */
324
+ alert(data.item.id);
325
+ });
326
+
327
+ ## Formtastic
328
+
329
+ If you are using [Formtastic](http://github.com/justinfrench/formtastic), you automatically get the *autocompleted_input* helper on *semantic_form_for*:
330
+
331
+ semantic_form_for @product do |f|
332
+ f.input :brand_name, :as => :autocomplete, :url => autocomplete_brand_name_products_path
333
+ end
334
+
335
+ The only difference with the original helper is that you must specify the autocomplete url using the *:url* option.
336
+
337
+ ## SimpleForm
338
+
339
+ If you want to use it with simple_form, all you have to do is use the
340
+ :as option on the input and set the autocomplete path with the :url
341
+ option.
342
+
343
+
344
+ simple_form_for @product do |form|
345
+ form.input :name
346
+ form.input :brand_name, :url => autocomplete_brand_name_products_path, :as => :autocomplete
347
+
348
+ # Cucumber
349
+
350
+ I have created a step to test your autocomplete with Cucumber and Capybara, all you have to do is add the following lines to your *env.rb* file:
351
+
352
+ require 'cucumber/autocomplete'
353
+
354
+ Then you'll have access to the following step:
355
+
356
+ I choose "([^"]*)" in the autocomplete list
357
+
358
+ An example on how to use it:
359
+
360
+ @javascript
361
+ Scenario: Autocomplete
362
+ Given the following brands exists:
363
+ | name |
364
+ | Alpha |
365
+ | Beta |
366
+ | Gamma |
367
+ And I go to the home page
368
+ And I fill in "Brand name" with "al"
369
+ And I choose "Alpha" in the autocomplete list
370
+ Then the "Brand name" field should contain "Alpha"
371
+
372
+ I have only tested this using Capybara, no idea if it works with something else, to see it in action, check the [example app](http://github.com/crowdint/rails3-jquery-autocomplete-app).
373
+
374
+ # Steak
375
+
376
+ I have created a helper to test your autocomplete with Steak and Capybara, all you have to do is add the following lines to your *acceptance_helper.rb* file:
377
+
378
+ require 'steak/autocomplete'
379
+
380
+ Then you'll have access to the following helper:
381
+
382
+ choose_autocomplete_result
383
+
384
+ An example on how to use it:
385
+
386
+ scenario "Autocomplete" do
387
+ lambda do
388
+ Brand.create! [
389
+ {:name => "Alpha"},
390
+ {:name => "Beta"},
391
+ {:name => "Gamma"}
392
+ ]
393
+ end.should change(Brand, :count).by(3)
394
+
395
+ visit home_page
396
+ fill_in "Brand name", :with => "al"
397
+ choose_autocomplete_result "Alpha"
398
+ find_field("Brand name").value.should include("Alpha")
399
+ end
400
+
401
+ I have only tested this using Capybara, no idea if it works with something else.
402
+
403
+ # Development
404
+
405
+ If you want to make changes to the gem, first install bundler 1.0.0:
406
+
407
+ gem install bundler
408
+
409
+ And then, install all your dependencies:
410
+
411
+ bundle install
412
+
413
+ ## Running the test suite
414
+
415
+ To run all the tests once, simply use
416
+
417
+ rake test
418
+
419
+ while you're developing, it is recommended that you run
420
+
421
+ bundle exec guard
422
+
423
+ to have the relevant test run every time you save a file.
424
+
425
+ ## Integration tests
426
+
427
+ If you make changes or add features to the jQuery part, please make sure
428
+ you write a cucumber test for it.
429
+
430
+ You can find an example Rails app on the *integration* folder.
431
+
432
+ You can run the integration tests with the cucumber command while on the
433
+ integration folder:
434
+
435
+ cd integration
436
+ rake db:migrate
437
+ cucumber
438
+
439
+ ## Where to test what
440
+
441
+ If you're making or tweaking a plugin (such as the formtastic plugin or
442
+ simple\_form plugin), check out the simple\_form\_plugin\_test for an
443
+ example of how to test it as part of the main `rake test` run.
444
+ Historically, plugins like these had been tested (shoddily) as part of
445
+ the integration tests. Feel free to remove them from the integration
446
+ suite and move them into the main suite. Your tests will run much
447
+ faster, and there will be less likelihood of your feature breaking in
448
+ the future. Thanks!
449
+
450
+ # Thanks to
451
+
452
+ Everyone on [this list](https://github.com/crowdint/rails3-jquery-autocomplete/contributors)
453
+