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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OTIyMDlkNDA3MmY3MmYwZjlmYzJlNzE1ODgzMTYxMzNjYTU1Nzc4Ng==
5
+ data.tar.gz: !binary |-
6
+ YTQyYjQyYjk2MTg1ZWYwOTZiMGMwYzNmMDE0ZWM5ZjJhMTI3NmFiZQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ODAzMDE0YTVmNmFlYjJmMjI0YzNlYzljODQ0OWYzMjVjZTI0MDExNWQyOTZm
10
+ YjEwNjI0ZWU4YWFmZGExNmNiYjJiMjlhMmY3ZWU5YzM0Y2RhZjMxMGM0MTYy
11
+ Y2Y2MjBkMWI0YzY3MzBhNGFiZDRjYTlhMWEyZmVkMzI0OGEwNDU=
12
+ data.tar.gz: !binary |-
13
+ ZmM5YWNmNWUwMDQyZDZkNGQzY2RjYTU2OGY1OWZmYWRlMTI1ZWYzZmE4NDhl
14
+ ZWViNzY5ZmRmMTQyYWY3Y2ZjODFiNGNjYWMwZmRlYTUxNzJkMTA2OGVmZmRl
15
+ NjllYWIwYTgxOWQ5NTQ3NTBiM2Q0ZDM2MTBhMmM1ZjNiNzAyMGI=
@@ -0,0 +1,31 @@
1
+ # Changelog
2
+
3
+ * 1.0.11
4
+ * mongoid: escape regular expression in search
5
+ * When possible, use jQuery .on() rather than .live()
6
+ * 1.0.6 Postgres or non-postgres queries are now determined at model level
7
+ * 1.0.3 Fixed Formtastic 2.0 + Ruby 1.8.7 compat issue
8
+ * 1.0.2 Fixed issue #93, #94
9
+ * 1.0.1 Formtastic 2.0 compatibility fix
10
+ * 1.0.0 Rails 3.1 asset pipeline support
11
+ * 0.9.1 Fixes issues #96 and #32
12
+ * 0.9.0 Massive rewrite
13
+ * 0.8.0 Compressed JS file
14
+ * 0.7.5 Pull request #46
15
+ * 0.7.4 Allows Rails 3.1
16
+ * 0.7.3 MongoMapper
17
+ * 0.7.2 Steak helper
18
+ * 0.7.1 Fixed joined scopes (Issue #43)
19
+ * 0.7.0 Scopes
20
+ * 0.6.6 ILIKE for postgres
21
+ * 0.6.5 JS select event
22
+ * 0.6.4 Use YAJL instead of JSON
23
+ * 0.6.3 SimpleForm plugin
24
+ * 0.6.2 Fix Issue #8
25
+ * 0.6.1 Allow specifying fully qualified class name for model object as an option to autocomplete
26
+ * 0.6.0 JS Code cleanup
27
+ * 0.5.1 Add STI support
28
+ * 0.5.0 Formtastic support
29
+ * 0.4.0 MongoID support
30
+ * 0.3.6 Using .live() to put autocomplete on dynamic fields
31
+
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.
@@ -0,0 +1,476 @@
1
+ ## Status
2
+
3
+ As you may have noticed we are working through the backlog of issues and pull requests
4
+ and trying to get those caught up. Couple of quick notes related to this.
5
+
6
+ 1. A new gem with Rails 4 support HAS BEEN recently RELEASED (1.0.12).
7
+ 2. If there is a specific feature that is in master, but not in the gem I strongly suggest you
8
+ point to the specific changeset that addresses your issue rather than directly at master. There
9
+ are going to be a lot of changes going on here and while we will do our best; there will probably
10
+ be some breakage on master in the short term. Use at your discretion!
11
+ 3. Some pull requests have been submitted to address the same functionality; in those cases we will try
12
+ to pick the best/optimal solution, accept that and close out the other tickets.
13
+ 4. Backwards compatibility is considered a high priority, so if a pull request is a great idea,
14
+ but it breaks backwards compatibility, it will be closed and noted that it causes breakage
15
+ to backwards compatibility. If anyone passionately wants the feature they are welcome to update
16
+ in a way that maintiains backwards compatibility and issue new pull reqs.
17
+ 5. Performance is critical. If a feature is a nice to have, but has been implemented in such a way that it causes
18
+ performance degradation to existing searches; it will be rejected.
19
+ 6. Thanks to everyone who is submitting pull requests, issues, comments, etc... I appreciate the help
20
+ from each one of you. Sometimes it has to get worse before it gets better. But I promise,
21
+ it will get better!
22
+
23
+ -------
24
+
25
+ Thanks to David @ crowdint for creating this gem and all of his hard
26
+ work on it in the past.
27
+
28
+ Just posting this update to let everyone know this project is still
29
+ alive and has a new maintainer. Over the next couple of weeks we will
30
+ be reviewing existing pull requests and you will see updates shortly.
31
+
32
+ Thanks for your patience.
33
+
34
+ joiey-seeley@uiowa.edu
35
+
36
+ -------
37
+
38
+ Unfortunately, I don't have much time to work on this gem, I'm looking for
39
+ someone to help with its maintenance. Send me an email if you are interested in
40
+ getting push privileges to this repo.
41
+
42
+ david@crowdint.com
43
+
44
+
45
+ # rails-jquery-autocomplete
46
+
47
+ [![Build Status](https://secure.travis-ci.org/crowdint/rails3-jquery-autocomplete.png)](http://travis-ci.org/crowdint/rails3-jquery-autocomplete) [![Gem Version](https://badge.fury.io/rb/rails3-jquery-autocomplete.png)](http://badge.fury.io/rb/rails3-jquery-autocomplete)
48
+
49
+ An easy way to use jQuery's autocomplete with Rails 3.
50
+
51
+ Supports both ActiveRecord, [mongoid](http://github.com/mongoid/mongoid), and [MongoMapper](https://github.com/jnunemaker/mongomapper).
52
+
53
+ Works with [Formtastic](http://github.com/justinfrench/formtastic)
54
+ and [SimpleForm](https://github.com/plataformatec/simple_form)
55
+
56
+ ## ActiveRecord
57
+
58
+ You can find a [detailed example](http://github.com/crowdint/rails3-jquery-autocomplete-app)
59
+ on how to use this gem with ActiveRecord [here](http://github.com/crowdint/rails3-jquery-autocomplete-app).
60
+
61
+ ## MongoID
62
+
63
+ You can find a [detailed example](http://github.com/crowdint/rails3-jquery-autocomplete-app/tree/mongoid)
64
+ on how to use this gem with MongoID [here](http://github.com/crowdint/rails3-jquery-autocomplete-app/tree/mongoid). (Same thing, different branch)
65
+
66
+ ## Before you start
67
+
68
+ Make sure your project is using jQuery-UI and the autocomplete widget
69
+ before you continue.
70
+
71
+ You can find more info about that here:
72
+
73
+ * http://jquery.com/
74
+ * http://jqueryui.com/demos/autocomplete/
75
+ * http://github.com/rails/jquery-ujs
76
+
77
+ I'd encourage you to understand how to use those 3 amazing tools before attempting to use this gem.
78
+
79
+ ## Installing
80
+
81
+ Include the gem on your Gemfile
82
+
83
+ gem 'rails-jquery-autocomplete'
84
+
85
+ Install it
86
+
87
+ bundle install
88
+
89
+ ### Rails 3.0.x
90
+
91
+ Run the generator
92
+
93
+ rails generate autocomplete:install
94
+
95
+ And include autocomplete-rails.js on your layouts
96
+
97
+ javascript_include_tag "autocomplete-rails.js"
98
+
99
+ #### Upgrading from older versions
100
+
101
+ If you are upgrading from a previous version, run the generator after installing to replace the javascript file.
102
+
103
+ rails generate autocomplete:install
104
+
105
+ I'd recommend you do this every time you update to make sure you have the latest JS file.
106
+
107
+ #### Uncompressed Javascript file
108
+
109
+ If you want to make changes to the JS file, you can install the
110
+ uncompressed version by running:
111
+
112
+ rails generate autocomplete:uncompressed
113
+
114
+ ### Rails 3.1.x and higher
115
+
116
+ Just add it to your app/assets/javascripts/application.js file
117
+
118
+ //= require jquery
119
+ //= require jquery_ujs
120
+ //= require jquery-ui
121
+ //= require autocomplete-rails
122
+
123
+ ## Usage
124
+
125
+ ### Model Example
126
+
127
+ Assuming you have a Brand model:
128
+
129
+ class Brand < ActiveRecord::Base
130
+ end
131
+
132
+ create_table :brand do |t|
133
+ t.column :name, :string
134
+ end
135
+
136
+ ### Controller
137
+
138
+ To set up the required action on your controller, all you have to do is call it with the class name and the method
139
+ as in the following example:
140
+
141
+ class ProductsController < Admin::BaseController
142
+ autocomplete :brand, :name
143
+ end
144
+
145
+ This will create an action _autocomplete_brand_name_ on your controller, don't forget to add it on your routes file
146
+
147
+ resources :products do
148
+ get :autocomplete_brand_name, :on => :collection
149
+ end
150
+
151
+ ### Options
152
+
153
+ #### :full => true
154
+
155
+ 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.
156
+
157
+ class ProductsController < Admin::BaseController
158
+ autocomplete :brand, :name, :full => true
159
+ end
160
+
161
+ The following terms would match the query 'un':
162
+
163
+ * Luna
164
+ * Unacceptable
165
+ * Rerun
166
+
167
+ #### :full => false (default behavior)
168
+
169
+ Only the following terms mould match the query 'un':
170
+
171
+ * Unacceptable
172
+
173
+ #### :limit => 10 (default behavior)
174
+
175
+ By default your search result set is limited to the first 10 records. This can be overridden by specifying the limit option.
176
+
177
+ #### :extra_data
178
+
179
+ 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).
180
+
181
+ Passing an array of attributes/column names to this option will fetch and return the specified data.
182
+
183
+ class ProductsController < Admin::BaseController
184
+ autocomplete :brand, :name, :extra_data => [:slogan]
185
+ end
186
+
187
+ #### :display_value
188
+
189
+ If you want to display a different version of what you're looking for, you can use the :display_value option.
190
+
191
+ This options receives a method name as the parameter, and that method will be called on the instance when displaying the results.
192
+
193
+ class Brand < ActiveRecord::Base
194
+ def funky_method
195
+ "#{self.name}.camelize"
196
+ end
197
+ end
198
+
199
+
200
+ class ProductsController < Admin::BaseController
201
+ autocomplete :brand, :name, :display_value => :funky_method
202
+ end
203
+
204
+ In the example above, you will search by _name_, but the autocomplete list will display the result of _funky_method_
205
+
206
+ This wouldn't really make much sense unless you use it with the "id_element" attribute. (See below)
207
+
208
+ 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
209
+
210
+ #### :hstore
211
+
212
+ Added option to support searching in hstore columns.
213
+
214
+ Pass a hash with two keys: `:method` and `:key` with values: the hstore field name and the key of the hstore to search.
215
+
216
+ e.g `autocomplete :feature, :name, :hstore => {:method => 'name_translations', :key => 'en'}`
217
+
218
+
219
+ #### :scopes
220
+ Added option to use scopes. Pass scopes in an array.
221
+ e.g `:scopes => [:scope1, :scope2]`
222
+
223
+ #### :column_name
224
+ By default autocomplete uses method name as column name. Now it can be specified using column_name options
225
+ `:column_name => 'name'`
226
+
227
+ #### json encoder
228
+ Autocomplete uses Yajl as JSON encoder/decoder, but you can specify your own
229
+
230
+ class ProductsController < Admin::BaseController
231
+ autocomplete :brand, :name do |items|
232
+ CustomJSON::Encoder.encode(items)
233
+ end
234
+ end
235
+
236
+ ### View
237
+
238
+ On your view, all you have to do is include the attribute autocomplete on the text field
239
+ using the url to the autocomplete action as the value.
240
+
241
+ form_for @product do |f|
242
+ f.autocomplete_field :brand_name, autocomplete_brand_name_products_path
243
+ end
244
+
245
+ This will generate an HTML tag that looks like:
246
+
247
+ <input type="text" data-autocomplete="products/autocomplete_brand_name">
248
+
249
+ 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
250
+ *autocomplete_field_tag* helper.
251
+
252
+ form_tag 'some/path'
253
+ autocomplete_field_tag 'address', '', address_autocomplete_path, :size => 75
254
+ end
255
+
256
+ #### Multiple Values Separated by Delimiter
257
+
258
+ To generate an autocomplete input field that accepts multiple values separated by a given delimiter, add the `'data-delimiter'` and `:multiple` options:
259
+
260
+ form_for @product do |f|
261
+ f.autocomplete_field :brand_names, autocomplete_brand_name_products_path,
262
+ 'data-delimiter' => ',', :multiple => true
263
+ end
264
+
265
+ 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.
266
+
267
+ #### Automatically focus on the first autocompleted item
268
+
269
+ 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`.
270
+
271
+ form_for @product do |f|
272
+ f.autocomplete_field :brand_names, autocomplete_brand_name_products_path,
273
+ 'data-auto-focus' => true
274
+ end
275
+
276
+ Now your autocomplete code is unobtrusive, Rails 3 style.
277
+
278
+ ### Getting the object id
279
+
280
+ If you need to use the id of the selected object, you can use the *id_element* attribute too:
281
+
282
+ f.autocomplete_field :brand_name, autocomplete_brand_name_products_path, :id_element => '#some_element'
283
+
284
+ 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.
285
+
286
+ ### Changing destination element
287
+
288
+ 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.
289
+
290
+ The :append_to option accepts a string containing jQuery selector for destination element:
291
+
292
+ f.autocomplete_field :product_name, '/products/autocomplete_product_name', :append_to => "#product_modal"
293
+
294
+ The previous example would append the autocomplete box containing suggestions to element jQuery('#product_modal').
295
+ 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.
296
+
297
+ ### Sending extra search fields
298
+
299
+ If you want to send extra fields from your form to the search action,
300
+ you can use the **:fields** options which generates a **data-autocomplete-fields**
301
+ HTML attribute.
302
+
303
+ The :fields option accepts a hash where the keys represent the Ajax request
304
+ parameter name and the values represent the jQuery selectors to retrieve the
305
+ form elements to get the values:
306
+
307
+ f.autocomplete_field :product_name, '/products/autocomplete_product_name', :fields => {:brand_id => '#brand_element', :country => '#country_element'}
308
+
309
+ class ProductsController < Admin::BaseController
310
+ def autocomplete_product_name
311
+ term = params[:term]
312
+ brand_id = params[:brand_id]
313
+ country = params[:country]
314
+ products = Product.where('brand = ? AND country = ? AND name LIKE ?', brand_id, country, "%#{term}%").order(:name).all
315
+ render :json => products.map { |product| {:id => product.id, :label => product.name, :value => product.name} }
316
+ end
317
+ end
318
+
319
+ ### Getting extra object data
320
+
321
+ If you need to extra data about the selected object, you can use the *:update_elements* HTML attribute.
322
+
323
+ 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:
324
+
325
+ f.autocomplete_field :brand_name, autocomplete_brand_name_products_path, :update_elements => {:id => '#id_element', :slogan => '#some_other_element'}
326
+
327
+ class ProductsController < Admin::BaseController
328
+ autocomplete :brand, :name, :extra_data => [:slogan]
329
+ end
330
+
331
+ The previous example would fetch the extra attribute slogan and update jQuery('#some_other_element') with the slogan value.
332
+
333
+ ### Running custom code on selection
334
+
335
+ 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:
336
+
337
+ $('#my_autocomplete_field').bind('railsAutocomplete.select', function(event, data){
338
+ /* Do something here */
339
+ alert(data.item.id);
340
+ });
341
+
342
+ ## Formtastic
343
+
344
+ If you are using [Formtastic](http://github.com/justinfrench/formtastic), you automatically get the *autocompleted_input* helper on *semantic_form_for*:
345
+
346
+ semantic_form_for @product do |f|
347
+ f.input :brand_name, :as => :autocomplete, :url => autocomplete_brand_name_products_path
348
+ end
349
+
350
+ The only difference with the original helper is that you must specify the autocomplete url using the *:url* option.
351
+
352
+ ## SimpleForm
353
+
354
+ If you want to use it with simple_form, all you have to do is use the
355
+ :as option on the input and set the autocomplete path with the :url
356
+ option.
357
+
358
+
359
+ simple_form_for @product do |form|
360
+ form.input :name
361
+ form.input :brand_name, :url => autocomplete_brand_name_products_path, :as => :autocomplete
362
+
363
+ # Cucumber
364
+
365
+ 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:
366
+
367
+ require 'cucumber/autocomplete'
368
+
369
+ Then you'll have access to the following step:
370
+
371
+ I choose "([^"]*)" in the autocomplete list
372
+
373
+ An example on how to use it:
374
+
375
+ @javascript
376
+ Scenario: Autocomplete
377
+ Given the following brands exists:
378
+ | name |
379
+ | Alpha |
380
+ | Beta |
381
+ | Gamma |
382
+ And I go to the home page
383
+ And I fill in "Brand name" with "al"
384
+ And I choose "Alpha" in the autocomplete list
385
+ Then the "Brand name" field should contain "Alpha"
386
+
387
+ 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).
388
+
389
+ # Steak
390
+
391
+ 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:
392
+
393
+ require 'steak/autocomplete'
394
+
395
+ Then you'll have access to the following helper:
396
+
397
+ choose_autocomplete_result
398
+
399
+ An example on how to use it:
400
+
401
+ scenario "Autocomplete" do
402
+ lambda do
403
+ Brand.create! [
404
+ {:name => "Alpha"},
405
+ {:name => "Beta"},
406
+ {:name => "Gamma"}
407
+ ]
408
+ end.should change(Brand, :count).by(3)
409
+
410
+ visit home_page
411
+ fill_in "Brand name", :with => "al"
412
+ choose_autocomplete_result "Alpha"
413
+ find_field("Brand name").value.should include("Alpha")
414
+ end
415
+
416
+ I have only tested this using Capybara, no idea if it works with something else.
417
+
418
+ # Development
419
+
420
+ If you want to make changes to the gem, first install bundler 1.0.0:
421
+
422
+ gem install bundler
423
+
424
+ And then, install all your dependencies:
425
+
426
+ bundle install
427
+
428
+ ## Running the test suite
429
+
430
+ <strike>You need to have an instance of MongoDB running on your computer or all the mongo tests will fail miserably.</strike>
431
+
432
+ To run all the tests once, simply use
433
+
434
+ rake test
435
+
436
+ while you're developing, it is recommended that you run
437
+
438
+ bundle exec guard
439
+
440
+ to have the relevant test run every time you save a file.
441
+
442
+ ## Integration tests
443
+
444
+ If you make changes or add features to the jQuery part, please make sure
445
+ you write a cucumber test for it.
446
+
447
+ You can find an example Rails app on the *integration* folder.
448
+
449
+ You can run the integration tests with the cucumber command while on the
450
+ integration folder:
451
+
452
+ cd integration
453
+ rake db:migrate
454
+ cucumber
455
+
456
+ ## Where to test what
457
+
458
+ If you're making or tweaking a plugin (such as the formtastic plugin or
459
+ simple\_form plugin), check out the simple\_form\_plugin\_test for an
460
+ example of how to test it as part of the main `rake test` run.
461
+ Historically, plugins like these had been tested (shoddily) as part of
462
+ the integration tests. Feel free to remove them from the integration
463
+ suite and move them into the main suite. Your tests will run much
464
+ faster, and there will be less likelihood of your feature breaking in
465
+ the future. Thanks!
466
+
467
+ # Thanks to
468
+
469
+ Everyone on [this list](https://github.com/crowdint/rails3-jquery-autocomplete/contributors)
470
+
471
+ # About the Author
472
+
473
+ [Crowd Interactive](http://www.crowdint.com) is an American web design and development company that happens to work in Colima, Mexico.
474
+ We specialize in building and growing online retail stores. We don’t work with everyone – just companies we believe in. Call us today to see if there’s a fit.
475
+ Find more info [here](http://www.crowdint.com)!
476
+