autocomplete-activeadmin 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e0faffdd0011ba143aad5af847a73d015bb62f2b
4
+ data.tar.gz: 65a6ac649ea934c5aa65630dcc838d2e6f403d67
5
+ SHA512:
6
+ metadata.gz: 5fd681e2519a3663c3b75bd07587bc87f958b5455c3a94830154e92d9fefa4a433731a1d1e888fe8881222eba47486c61a5987c13c98d5faf0101b76693590fb
7
+ data.tar.gz: c809e139bd74dd8bbfe3bfe95063d49758d80be634c735dba1902158398d9f5faf465c92b54415ef3ebc7db8b39ac4166becabb554e5fe582af175043ece5590
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,477 @@
1
+ ## Status
2
+
3
+ 1. This is the officially maintained fork of
4
+ [rails3-jquery-autocomplete](http://github.com/crowdint/rails3-jquery-autocomplete)
5
+ 2. All new features and support of Rails 4 and above will occur here
6
+ going forward. Rails 3 is also supported on this fork; bug fixes and security fixes for Rails 3 will continue until Rails 3 is EOL.
7
+
8
+ # autocomplete-activeadmin
9
+
10
+ [![Build Status](https://secure.travis-ci.org/bigtunacan/autocomplete-activeadmin.png)](http://travis-ci.org/bigtunacan/autocomplete-activeadmin) [![Gem Version](https://badge.fury.io/rb/autocomplete-activeadmin.png)](http://badge.fury.io/rb/autocomplete-activeadmin)
11
+
12
+ An easy way to use jQuery's autocomplete with Rails.
13
+
14
+ Supports both ActiveRecord, [mongoid](https://github.com/mongodb/mongoid), and [MongoMapper](https://github.com/jnunemaker/mongomapper).
15
+
16
+ Works with [Formtastic](http://github.com/justinfrench/formtastic)
17
+ and [SimpleForm](https://github.com/plataformatec/simple_form)
18
+
19
+ ## ActiveRecord
20
+
21
+ You can find a [detailed example](http://github.com/crowdint/rails3-jquery-autocomplete-app)
22
+ on how to use this gem with ActiveRecord [here](http://github.com/crowdint/rails3-jquery-autocomplete-app).
23
+
24
+ ## MongoID
25
+
26
+ You can find a [detailed example](http://github.com/crowdint/rails3-jquery-autocomplete-app/tree/mongoid)
27
+ on how to use this gem with MongoID [here](http://github.com/crowdint/rails3-jquery-autocomplete-app/tree/mongoid). (Same thing, different branch)
28
+
29
+ ## Before you start
30
+
31
+ Make sure your project is using jQuery-UI and the autocomplete widget
32
+ before you continue.
33
+
34
+ You can find more info about that here:
35
+
36
+ * http://jquery.com/
37
+ * http://jqueryui.com/demos/autocomplete/
38
+ * http://github.com/rails/jquery-ujs
39
+
40
+ I'd encourage you to understand how to use those 3 amazing tools before attempting to use this gem.
41
+
42
+ ## Installing
43
+
44
+ Include the gem on your Gemfile
45
+
46
+ gem 'autocomplete-activeadmin'
47
+
48
+ Install it
49
+
50
+ bundle install
51
+
52
+ ### Rails < 4.x.x
53
+
54
+ Run the generator
55
+
56
+ rails generate autocomplete:install
57
+
58
+ And include autocomplete-rails.js on your layouts
59
+
60
+ javascript_include_tag "autocomplete-rails.js"
61
+
62
+ #### Upgrading from older versions
63
+
64
+ If you are upgrading from a previous version, run the generator after installing to replace the javascript file.
65
+
66
+ rails generate autocomplete:install
67
+
68
+ I'd recommend you do this every time you update to make sure you have the latest JS file.
69
+
70
+ #### Uncompressed Javascript file
71
+
72
+ If you want to make changes to the JS file, you can install the
73
+ uncompressed version by running:
74
+
75
+ rails generate autocomplete:uncompressed
76
+
77
+ ### Rails 4 and higher
78
+
79
+ Just add it to your app/assets/javascripts/application.js file
80
+
81
+ //= require jquery
82
+ //= require jquery_ujs
83
+ //= require jquery-ui/autocomplete
84
+ //= require autocomplete-rails
85
+
86
+ ## Usage
87
+
88
+ ### Demo Application by [Yifei](https://github.com/yifeiwu)
89
+
90
+ A live demo can be seen
91
+
92
+ [here](https://rocky-thicket-9286.herokuapp.com/)
93
+
94
+ [source](https://github.com/yifeiwu/rails4-autocomplete-demo)
95
+
96
+ 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.
97
+
98
+ ##Some implemented features
99
+
100
+ 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.
101
+
102
+ 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.
103
+
104
+ 3. The autocomplete is implemented in the context of a search form with a simple scope search(see the food model and controller).
105
+
106
+ ### Model Example
107
+
108
+ Assuming you have a Brand model that contains a name attribute:
109
+
110
+ class Brand < ActiveRecord::Base
111
+ end
112
+
113
+ create_table :brand do |t|
114
+ t.column :name, :string
115
+ end
116
+
117
+ ### Controller
118
+
119
+ To set up the required action on your controller, all you have to do is call it with the class name and the method
120
+ as in the following example:
121
+
122
+ class ProductsController < Admin::BaseController
123
+ autocomplete :brand, :name
124
+ end
125
+
126
+ This will create an action _autocomplete_brand_name_ on your controller, don't forget to add it on your routes file
127
+
128
+ resources :products do
129
+ get :autocomplete_brand_name, :on => :collection
130
+ end
131
+
132
+ Verify this path using `rake routes`, you will need it later for the **view** section.
133
+
134
+ ### Options
135
+
136
+ #### :full => true
137
+
138
+ 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.
139
+
140
+ class ProductsController < Admin::BaseController
141
+ autocomplete :brand, :name, :full => true
142
+ end
143
+
144
+ The following terms would match the query 'un':
145
+
146
+ * Luna
147
+ * Unacceptable
148
+ * Rerun
149
+
150
+ #### :full => false (default behavior)
151
+
152
+ Only the following terms would match the query 'un':
153
+
154
+ * Unacceptable
155
+
156
+ #### :limit => 10 (default behavior)
157
+
158
+ By default your search result set is limited to the first 10 records. This can be overridden by specifying the limit option.
159
+
160
+ #### :extra_data
161
+
162
+ 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).
163
+
164
+ Passing an array of attributes/column names to this option will fetch and return the specified data.
165
+
166
+ class ProductsController < Admin::BaseController
167
+ autocomplete :brand, :name, :extra_data => [:slogan]
168
+ end
169
+
170
+ #### :display_value
171
+
172
+ If you want to display a different version of what you're looking for, you can use the :display_value option.
173
+
174
+ This options receives a method name as the parameter, and that method will be called on the instance when displaying the results.
175
+
176
+ class Brand < ActiveRecord::Base
177
+ def funky_method
178
+ "#{self.name}.camelize"
179
+ end
180
+ end
181
+
182
+
183
+ class ProductsController < Admin::BaseController
184
+ autocomplete :brand, :name, :display_value => :funky_method
185
+ end
186
+
187
+ In the example above, you will search by _name_, but the autocomplete list will display the result of _funky_method_
188
+
189
+ This wouldn't really make much sense unless you use it with the "id_element" attribute. (See below)
190
+
191
+ 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
192
+
193
+ #### :hstore
194
+
195
+ Added option to support searching in hstore columns.
196
+
197
+ Pass a hash with two keys: `:method` and `:key` with values: the hstore field name and the key of the hstore to search.
198
+
199
+ e.g `autocomplete :feature, :name, :hstore => {:method => 'name_translations', :key => 'en'}`
200
+
201
+
202
+ #### :scopes
203
+
204
+ Added option to use scopes. Pass scopes in an array.
205
+ e.g `:scopes => [:scope1, :scope2]`
206
+
207
+ #### :column_name
208
+
209
+ By default autocomplete uses method name as column name. Now it can be specified using column_name options
210
+ `:column_name => 'name'`
211
+
212
+ #### :case_sensitive
213
+
214
+ Normally autocomplete performs a case insensitive search. In cases where this is not desirable, or causes too high
215
+ a performance penalty, search can be made case sensitive by specifying `:case_sensitive => true`
216
+
217
+ #### json encoder
218
+
219
+ Autocomplete uses Yajl as JSON encoder/decoder, but you can specify your own
220
+
221
+ class ProductsController < Admin::BaseController
222
+ autocomplete :brand, :name do |items|
223
+ CustomJSON::Encoder.encode(items)
224
+ end
225
+ end
226
+
227
+ ### View
228
+
229
+ On your view, all you have to do is include the attribute autocomplete on the text field
230
+ using the url to the autocomplete action as the value.
231
+
232
+ form_for @product do |f|
233
+ f.autocomplete_field :brand_name, autocomplete_brand_name_products_path
234
+ end
235
+
236
+ This will generate an HTML tag that looks like:
237
+
238
+ <input type="text" data-autocomplete="products/autocomplete_brand_name">
239
+
240
+ 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
241
+ *autocomplete_field_tag* helper.
242
+
243
+ form_tag 'some/path'
244
+ autocomplete_field_tag 'address', '', address_autocomplete_path, :size => 75
245
+ end
246
+
247
+ #### Multiple Values Separated by Delimiter
248
+
249
+ To generate an autocomplete input field that accepts multiple values separated by a given delimiter, add the `'data-delimiter'` and `:multiple` options:
250
+
251
+ form_for @product do |f|
252
+ f.autocomplete_field :brand_names, autocomplete_brand_name_products_path,
253
+ 'data-delimiter' => ',', :multiple => true
254
+ end
255
+
256
+ 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.
257
+
258
+ #### Automatically focus on the first autocompleted item
259
+
260
+ 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`.
261
+
262
+ form_for @product do |f|
263
+ f.autocomplete_field :brand_names, autocomplete_brand_name_products_path,
264
+ 'data-auto-focus' => true
265
+ end
266
+
267
+ Now your autocomplete code is unobtrusive, Rails style.
268
+
269
+ #### Client-side config
270
+
271
+ To configure the behaviour if no matches are found, you can set the following options:
272
+
273
+ jQuery.railsAutocomplete.options.showNoMatches //default true
274
+ jQuery.railsAutocomplete.options.noMatchesLabel //default 'no existing match'
275
+
276
+ These will change the behaviour globally. To set them on a single input field use:
277
+
278
+ f.autocomplete_field :brand_names, autocomplete_brand_name_products_path,
279
+ 'data-showNoMatches' => false
280
+ #or
281
+ f.autocomplete_field :brand_names, autocomplete_brand_name_products_path,
282
+ 'data-noMatchesLabel' => 'no brands found'
283
+
284
+
285
+ ### Getting the object id
286
+
287
+ If you need to use the id of the selected object, you can use the *id_element* attribute too:
288
+
289
+ f.autocomplete_field :brand_name, autocomplete_brand_name_products_path, :id_element => '#some_element'
290
+
291
+ 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.
292
+
293
+ ### Changing destination element
294
+
295
+ 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.
296
+
297
+ The :append_to option accepts a string containing jQuery selector for destination element:
298
+
299
+ f.autocomplete_field :product_name, '/products/autocomplete_product_name', :append_to => "#product_modal"
300
+
301
+ The previous example would append the autocomplete box containing suggestions to element jQuery('#product_modal').
302
+ 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.
303
+
304
+ ### Sending extra search fields
305
+
306
+ If you want to send extra fields from your form to the search action,
307
+ you can use the **:fields** options which generates a **data-autocomplete-fields**
308
+ HTML attribute.
309
+
310
+ The :fields option accepts a hash where the keys represent the Ajax request
311
+ parameter name and the values represent the jQuery selectors to retrieve the
312
+ form elements to get the values:
313
+
314
+ f.autocomplete_field :product_name, '/products/autocomplete_product_name', :fields => {:brand_id => '#brand_element', :country => '#country_element'}
315
+
316
+ class ProductsController < Admin::BaseController
317
+ def autocomplete_product_name
318
+ term = params[:term]
319
+ brand_id = params[:brand_id]
320
+ country = params[:country]
321
+ products = Product.where('brand = ? AND country = ? AND name LIKE ?', brand_id, country, "%#{term}%").order(:name).all
322
+ render :json => products.map { |product| {:id => product.id, :label => product.name, :value => product.name} }
323
+ end
324
+ end
325
+
326
+ ### Getting extra object data
327
+
328
+ If you need to extra data about the selected object, you can use the *:update_elements* HTML attribute.
329
+
330
+ 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:
331
+
332
+ f.autocomplete_field :brand_name, autocomplete_brand_name_products_path, :update_elements => {:id => '#id_element', :slogan => '#some_other_element'}
333
+
334
+ class ProductsController < Admin::BaseController
335
+ autocomplete :brand, :name, :extra_data => [:slogan]
336
+ end
337
+
338
+ The previous example would fetch the extra attribute slogan and update jQuery('#some_other_element') with the slogan value.
339
+
340
+ ### Running custom code on selection
341
+
342
+ 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:
343
+
344
+ $('#my_autocomplete_field').bind('railsAutocomplete.select', function(event, data){
345
+ /* Do something here */
346
+ alert(data.item.id);
347
+ });
348
+
349
+ ## Formtastic
350
+
351
+ If you are using [Formtastic](http://github.com/justinfrench/formtastic), you automatically get the *autocompleted_input* helper on *semantic_form_for*:
352
+
353
+ semantic_form_for @product do |f|
354
+ f.input :brand_name, :as => :autocomplete, :url => autocomplete_brand_name_products_path
355
+ end
356
+
357
+ The only difference with the original helper is that you must specify the autocomplete url using the *:url* option.
358
+
359
+ ## SimpleForm
360
+
361
+ If you want to use it with simple_form, all you have to do is use the
362
+ :as option on the input and set the autocomplete path with the :url
363
+ option.
364
+
365
+
366
+ simple_form_for @product do |form|
367
+ form.input :name
368
+ form.input :brand_name, :url => autocomplete_brand_name_products_path, :as => :autocomplete
369
+
370
+ # Cucumber
371
+
372
+ 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:
373
+
374
+ require 'cucumber/autocomplete'
375
+
376
+ Then you'll have access to the following step:
377
+
378
+ I choose "([^"]*)" in the autocomplete list
379
+
380
+ An example on how to use it:
381
+
382
+ @javascript
383
+ Scenario: Autocomplete
384
+ Given the following brands exists:
385
+ | name |
386
+ | Alpha |
387
+ | Beta |
388
+ | Gamma |
389
+ And I go to the home page
390
+ And I fill in "Brand name" with "al"
391
+ And I choose "Alpha" in the autocomplete list
392
+ Then the "Brand name" field should contain "Alpha"
393
+
394
+ 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).
395
+
396
+ # Steak
397
+
398
+ 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:
399
+
400
+ require 'steak/autocomplete'
401
+
402
+ Then you'll have access to the following helper:
403
+
404
+ choose_autocomplete_result
405
+
406
+ An example on how to use it:
407
+
408
+ scenario "Autocomplete" do
409
+ lambda do
410
+ Brand.create! [
411
+ {:name => "Alpha"},
412
+ {:name => "Beta"},
413
+ {:name => "Gamma"}
414
+ ]
415
+ end.should change(Brand, :count).by(3)
416
+
417
+ visit home_page
418
+ fill_in "Brand name", :with => "al"
419
+ choose_autocomplete_result "Alpha"
420
+ find_field("Brand name").value.should include("Alpha")
421
+ end
422
+
423
+ I have only tested this using Capybara, no idea if it works with something else.
424
+
425
+ # Development
426
+
427
+ If you want to make changes to the gem, first install bundler 1.0.0:
428
+
429
+ gem install bundler
430
+
431
+ And then, install all your dependencies:
432
+
433
+ bundle install
434
+
435
+ ## Running the test suite
436
+
437
+ <strike>You need to have an instance of MongoDB running on your computer or all the mongo tests will fail miserably.</strike>
438
+
439
+ To run all the tests once, simply use
440
+
441
+ rake test
442
+
443
+ while you're developing, it is recommended that you run
444
+
445
+ bundle exec guard
446
+
447
+ to have the relevant test run every time you save a file.
448
+
449
+ ## Integration tests
450
+
451
+ If you make changes or add features to the jQuery part, please make sure
452
+ you write a cucumber test for it.
453
+
454
+ You can find an example Rails app on the *integration* folder.
455
+
456
+ You can run the integration tests with the cucumber command while on the
457
+ integration folder:
458
+
459
+ cd integration
460
+ rake db:migrate
461
+ cucumber
462
+
463
+ ## Where to test what
464
+
465
+ If you're making or tweaking a plugin (such as the formtastic plugin or
466
+ simple\_form plugin), check out the simple\_form\_plugin\_test for an
467
+ example of how to test it as part of the main `rake test` run.
468
+ Historically, plugins like these had been tested (shoddily) as part of
469
+ the integration tests. Feel free to remove them from the integration
470
+ suite and move them into the main suite. Your tests will run much
471
+ faster, and there will be less likelihood of your feature breaking in
472
+ the future. Thanks!
473
+
474
+ # Thanks to
475
+
476
+ Everyone on [this list](https://github.com/crowdint/rails3-jquery-autocomplete/contributors)
477
+