gmaps-autocomplete-rails 0.1.2.1 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +265 -50
  3. data/gmaps-autocomplete-rails.gemspec +7 -32
  4. data/vendor/assets/javascripts/gmaps-auto-complete.js +297 -0
  5. metadata +20 -63
  6. data/.document +0 -5
  7. data/.rspec +0 -1
  8. data/CHANGELOG +0 -6
  9. data/Gemfile.lock +0 -109
  10. data/Rakefile +0 -49
  11. data/VERSION +0 -1
  12. data/spec/index.html +0 -64
  13. data/spec/init.js +0 -4
  14. data/spec/jquery-ui/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  15. data/spec/jquery-ui/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  16. data/spec/jquery-ui/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  17. data/spec/jquery-ui/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  18. data/spec/jquery-ui/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  19. data/spec/jquery-ui/images/ui-bg_glass_75_ffffff_1x400.png +0 -0
  20. data/spec/jquery-ui/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  21. data/spec/jquery-ui/images/ui-bg_inset-soft_95_fef1ec_1x100.png +0 -0
  22. data/spec/jquery-ui/images/ui-icons_222222_256x240.png +0 -0
  23. data/spec/jquery-ui/images/ui-icons_2e83ff_256x240.png +0 -0
  24. data/spec/jquery-ui/images/ui-icons_454545_256x240.png +0 -0
  25. data/spec/jquery-ui/images/ui-icons_888888_256x240.png +0 -0
  26. data/spec/jquery-ui/images/ui-icons_cd0a0a_256x240.png +0 -0
  27. data/spec/jquery-ui/images/ui-icons_f6cf3b_256x240.png +0 -0
  28. data/spec/jquery-ui/jquery-ui-1.8.16.custom.css +0 -1320
  29. data/spec/jquery-ui/jquery.ui.1.8.16.ie.css +0 -6
  30. data/spec/main.css +0 -89
  31. data/spec/spec_helper.rb +0 -12
  32. data/vendor/assets/javascripts/gmaps-autocomplete.js +0 -300
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 90efee2225603ef14f7dba29ae069649a04e3181
4
+ data.tar.gz: de4e8faefab4ffb298a31825ce0ede6e29a43ce7
5
+ SHA512:
6
+ metadata.gz: 2b835b6e9dccda0a073bb22b696498a89279eed95e0a8bf85b0e21135ebd33b79f89eee50af3ff77e6100ea5232812237a7ef78c616549c7b8025d7ed695c0d3
7
+ data.tar.gz: 207a866bef3c9ae88f103e93f81e1d2ccf2de916368d30dc929fafec7190b6931699f367a72f94fdf042db24ca5914f6ec5b7da2e8f232ceda1c371672a43507
data/README.md CHANGED
@@ -2,33 +2,57 @@
2
2
 
3
3
  Extracted from [gmaps-autocomplete](https://github.com/rjshade/gmaps-autocomplete) and improved markedly and then packaged for use with Rails as an asset gem :)
4
4
 
5
- See it in action here: [http://rjshade.com/projects/gmaps-autocomplete/](http://rjshade.com/projects/gmaps-autocomplete/)
5
+ ## General info
6
6
 
7
- Some more explanation here: [https://github.com/rjshade/gmaps-autocomplete](https://github.com/rjshade/gmaps-autocomplete)
7
+ The script is now compiled from Coffeescript and allows you to have multiple fields linked to multiple instances of the `GmapsCompleter` class on the same page.
8
+
9
+ I recommend that you also check out: [google maps and RoR](http://stackoverflow.com/questions/7466872/google-maps-and-ror-3-1)
10
+
11
+ ## Upgrading
12
+
13
+ Version 1.3+ now comes only with a Class based GmapsCompleter. The old static GmapsCompleter container, used in version 1.2 (and below) has been deprecated.
14
+
15
+ Please upgrade your configuration functionality as demonstrated in the usage/config instructions below.
8
16
 
9
17
  ## Install
10
18
 
19
+ ## Update gem dependencies
20
+
21
+ In your project `Gemfile`
22
+
23
+ ```ruby
24
+ gem 'jquery-rails'
25
+ gem 'jquery-ui-rails'
11
26
  gem 'gmaps-autocomplete-rails'
27
+ ```
12
28
 
13
- You will also need:
14
- * gem 'jquery-rails'
15
- * gem 'jquery-ui-rails'
29
+ Or to always use the most recent version, use `gem 'gmaps-autocomplete-rails', github: "kristianmandrup/gmaps-autocomplete-rails"``
16
30
 
17
- And run `bundle install`;)
31
+ Then run `bundle install`;)
18
32
 
19
33
  Packed and ready for use with the Asset pipeline :)
20
34
 
21
- Add to javascript manifest file, fx `application.js`
35
+ ### Update javascript dependencies
36
+
37
+ Add to javascript manifest file, fx `app/assets/javascripts/application.js`
22
38
 
23
39
  ```
24
40
  //= require jquery_ujs
25
- //= require jquery.ui.all
26
- //= require gmaps-autocomplete
41
+ //= require jquery-ui
42
+ //= require gmaps-auto-complete
27
43
  ```
28
44
 
29
- And include the google maps script before `application.js`, fx in your layout file:
45
+ Note: Depending on your setup/needs, including jquery-ui is optional (see customization).
46
+
47
+ ### Update style dependencies (optional)
48
+
49
+ Add to stylesheets manifest file, fx `app/assets/stylesheets/application.css`
50
+
51
+ ```
52
+ *= require jquery-ui
53
+ ```
30
54
 
31
- See [google maps and RoR](http://stackoverflow.com/questions/7466872/google-maps-and-ror-3-1)
55
+ Include the google maps script before `application.js`, fx in your layout file:
32
56
 
33
57
  *application.html.erb*
34
58
 
@@ -37,34 +61,77 @@ See [google maps and RoR](http://stackoverflow.com/questions/7466872/google-maps
37
61
  <%= javascript_include_tag "application" %>
38
62
  ```
39
63
 
40
- Note also that the autocomplete script depends on jQuery 1.6+.
64
+ Note also that the autocomplete script currently depends on jQuery 1.6+.
65
+ Please feel free to remove this dependency with a pull request :)
66
+
67
+ ## Customization
68
+
69
+ If you want to customize the gem to use another autocomplete "engine" than the default (assumed to be *jquery-ui*), then fork the gem and change the following
70
+ in the `gmaps-auto-complete.coffee` script to suit your needs and compile to js.
71
+
72
+ ```
73
+ # around line 237
74
+ autocompleteOpts = $.extend true, defaultAutocompleteOpts, autocompleteOpts
75
+
76
+ $(@inputField).autocomplete(autocompleteOpts)
77
+
78
+ # triggered when user presses a key in the address box
79
+ $(@inputField).bind 'keydown', @keyDownHandler
80
+ # autocomplete_init
81
+
82
+ keyDownHandler: (event) =>
83
+ if (event.keyCode == 13)
84
+ @geocodeLookup 'address', $(@inputField).val(), true
85
+ # ensures dropdown disappears when enter is pressed
86
+ $(@inputField).autocomplete "disable"
87
+ else
88
+ # re-enable if previously disabled above
89
+ $(@inputField).autocomplete "enable"
90
+ ```
91
+
92
+ PS: I'm not sure if this is the only place that requires changes...
41
93
 
42
94
  ## Initialize
43
95
 
44
- *application.js*
96
+ Add functionality that is executed after the document (and all scripts) have been fully loaded. Example:
97
+
98
+ *mypage.js*
45
99
 
46
100
  ```javascript
47
- $(document).ready(function() {
48
- GmapsAutoComplete.init();
49
- GmapsAutoComplete.autoCompleteInit();
101
+ jQuery(function() {
102
+ var completer;
103
+
104
+ completer = new GmapsCompleter({
105
+ inputField: '#gmaps-input-address',
106
+ errorField: '#gmaps-error'
107
+ });
108
+
109
+ completer.autoCompleteInit({
110
+ country: "us"
111
+ });
50
112
  });
51
113
  ```
52
- or coffeescript
53
114
 
54
- *yourmodel.js.coffee*
115
+ or using Coffeescript
116
+
117
+ *mypage.js.coffee*
55
118
 
56
119
  ```
57
- $ ->
58
- GmapsAutoComplete.init()
59
- GmapsAutoComplete.autoCompleteInit()
120
+ jQuery ->
121
+ completer = new GmapsCompleter
122
+ inputField: '#gmaps-input-address'
123
+ errorField: '#gmaps-error'
124
+
125
+ completer.autoCompleteInit
126
+ country: "us"
60
127
  ```
61
128
 
62
129
  ### Configuration options
63
130
 
64
- `init()` can take an option hash, using the following defaults:
131
+ The constructor function can take a configuration option hash that can configure the specific workings of the `GmapsCompleter`. It uses the following defaults:
65
132
 
66
133
  ```javascript
67
- defaultOptions = {
134
+ {
68
135
  mapElem: "#gmaps-canvas",
69
136
  zoomLevel: 2,
70
137
  mapType: google.maps.MapTypeId.ROADMAP,
@@ -75,31 +142,50 @@ defaultOptions = {
75
142
  };
76
143
  ```
77
144
 
78
- ### Advanced Configuration
145
+ The following default methods can be replaced by configuration:
79
146
 
80
- `init()` can take an option hash, using the following defaults:
147
+ * positionOutputter
148
+ * updateUI
149
+ * updateMap
81
150
 
82
- ```javascript
83
- defaultOptions = {
84
- mapElem: "#gmaps-canvas",
85
- zoomLevel: 2,
86
- mapType: google.maps.MapTypeId.ROADMAP,
87
- pos: [51.751724, -1.255284],
88
- inputField: '#gmaps-input-address',
89
- errorField: '#gmaps-error',
90
- debugOn: false
91
- };
92
- ```
151
+ These methods are used to control how the gmaps data is used to update the UI on the page, such as the position output and map position update. Customize these needed.
93
152
 
94
- The following default methods can be replaced by configuration:
153
+ The default logic (taken from GmapsCompleterDefaultAssist) is:
95
154
 
96
- ```javascript
97
- positionOutputter: this.defaultPositionOutputter,
98
- updateUI : this.defaultUpdateUI,
99
- updateMap : this.defaultUpdateMap
155
+ ```coffeescript
156
+ defaultUpdateMap: (geometry) ->
157
+ map = @map
158
+ marker = @marker
159
+
160
+ map.fitBounds(geometry.viewport) if map
161
+ marker.setPosition(geometry.location) if marker
162
+
163
+ # fill in the UI elements with new position data
164
+ defaultUpdateUI: (address, latLng) ->
165
+ $(@inputField).autocomplete 'close'
166
+
167
+ @debug 'country', @country
168
+
169
+ updateAdr = address.replace ', ' + @country, ''
170
+ updateAdr = address
171
+
172
+ @debug 'updateAdr', updateAdr
173
+
174
+ $(@inputField).val updateAdr
175
+ @positionOutputter latLng
176
+
177
+ defaultPositionOutputter: (latLng) ->
178
+ $('#gmaps-output-latitude').html latLng.lat()
179
+ $('#gmaps-output-longitude').html latLng.lng()
100
180
  ```
101
181
 
102
- `autoCompleteInit` also takes an option hash, but currently only [region](https://developers.google.com/maps/documentation/geocoding/#RegionCodes) and country can be used.
182
+ The default update UI logic removes the country from the address displayed.
183
+
184
+ ## autoCompleteInit
185
+
186
+ The function `autoCompleteInit`, called on an instance of GmapsCompleter, can takes an option hash. Currently only [region](https://developers.google.com/maps/documentation/geocoding/#RegionCodes), `country` and `autocomplete` can be used.
187
+
188
+ Example:
103
189
 
104
190
  ```javascript
105
191
  autoCompleteInit({region: 'DK', country: 'Denmark'});
@@ -107,7 +193,76 @@ autoCompleteInit({region: 'DK', country: 'Denmark'});
107
193
 
108
194
  Will make searches in the DK region and remove `', Denmark'` from the result.
109
195
 
110
- ## Use with Rails form helpers
196
+ Note: Not sure if this is still useful with the new instance based approach!?
197
+
198
+ Parameter `autocomplete` allows to configure JQuery autocomplete widget
199
+
200
+ Example:
201
+ ```javascript
202
+ autoCompleteInit({
203
+ region: 'DK',
204
+ country: 'Denmark',
205
+ autocomplete: {
206
+ minLength: 4,
207
+ position: {
208
+ my: "center top",
209
+ at: "center bottom"
210
+ }
211
+ }
212
+ });
213
+ ```
214
+
215
+ ## Assist object
216
+
217
+ The options hash for the constructor can now take an `assist` object as an argument.
218
+ The `assist` object can be a class or a simple object containing the following:
219
+
220
+ ```coffeescript
221
+ options:
222
+ mapElem: '#gmaps-canvas'
223
+ zoomLevel: 2
224
+ mapType: google.maps.MapTypeId.ROADMAP
225
+ pos: [0, 0]
226
+ inputField: '#gmaps-input-address'
227
+ errorField: '#gmaps-error'
228
+ debugOn: true
229
+
230
+ # update marker and map
231
+ updateMap: (geometry) ->
232
+
233
+ # fill in the UI elements with new position data
234
+ updateUI: (address, latLng) ->
235
+
236
+ # display current position
237
+ positionOutputter: (latLng) ->
238
+
239
+ # optionally also message functions (see below)
240
+ ```
241
+
242
+ If you use a class you can use Coffeescript `extends` (see http://coffeescript.org/#classes) to subclass the default implementation. You can then optionally use `super` to call the base implementation.
243
+
244
+ Example:
245
+
246
+ ```coffeescript
247
+ class MyCompleterAssist extends GmapsCompleterDefaultAssist
248
+ updateUI: (address, latLng) ->
249
+ console.log "Doing my own thang!"
250
+ // ...
251
+
252
+ super (address, latLng)
253
+ ```
254
+
255
+ Usage (config):
256
+
257
+ ```coffeescript
258
+ completer = new GmapsCompleter
259
+ inputField: '#gmaps-my-input-address'
260
+ assist: MyCompleterAssist
261
+ ```
262
+
263
+ ## Usage with Rails form helpers
264
+
265
+ Simple form example:
111
266
 
112
267
  ```ruby
113
268
  = simple_form_for(@post) do |f|
@@ -116,7 +271,9 @@ Will make searches in the DK region and remove `', Denmark'` from the result.
116
271
 
117
272
  ## Examples
118
273
 
119
- See `spec/index.html` for an example page using this "plugin". Note that if you set `mapElem`to null or leave out that element on the page, the autocomplete will function without attempting to update the map :)
274
+ See `spec/test-gmaps-auto-coffee.html` for an example page using this "plugin". Note that if you set `mapElem` to null or leave out that element on the page, the autocomplete will function without attempting to update the map :)
275
+
276
+ This is very useful in scenarios where you want to geolocate the address without displaying on the map.
120
277
 
121
278
  ## Advanced Customization
122
279
 
@@ -148,9 +305,11 @@ updateUi: function( address, latLng ) {
148
305
  }
149
306
  ```
150
307
 
308
+ Note that you can encapsulate this kind of customization using the `assist` option and an Assist object/class as demonstrated above.
309
+
151
310
  ## Customizing messages
152
311
 
153
- For now, directly define your own implementation (override) the following functions directly on GmapsAutoComplete
312
+ The following message functions can be customized, either by passing in the options hash, overriding directly on the GmapsCompleter object or even by using the Assist object/class.
154
313
 
155
314
  * geocodeErrorMsg: function()
156
315
  * invalidAddressMsg: function(value)
@@ -160,11 +319,17 @@ Example:
160
319
 
161
320
  ```javascript
162
321
 
163
- GmapsAutoComplete.geocodeErrorMsg = function() {
322
+ GmapsCompleter.prototype.geocodeErrorMsg = function() {
164
323
  "Geocode error!"
165
324
  }
166
325
  ```
167
326
 
327
+ Here, ensuring that ALL instances of `GmapsCompleter` will use this functionality as the baseline (since overriding the prototype function).
328
+
329
+ ### Localizing messages
330
+
331
+ For localization/internationalization support, you could customize your Assist object constructor to set the current locale and then have your custom `xMsg` functions use this locale to display the localized message.
332
+
168
333
  ## Formtastic example
169
334
 
170
335
  For [formtastic](https://github.com/justinfrench/formtastic) something like:
@@ -175,21 +340,71 @@ For [formtastic](https://github.com/justinfrench/formtastic) something like:
175
340
  %span#address_error
176
341
  ```
177
342
 
343
+ Or,
344
+
345
+ ```ruby
346
+ <%= semantic_form_for(@search) do |f| %>
347
+ <%= f.input :pickupAddress, :as => :string, :label => "House/Apt Number and Street", :input_html => { :id => "gmaps-input-address", :style => "width:350px; font-size:14px", :placeholder => "Start typing an address or location" } %>
348
+ ...
349
+ ```
350
+
178
351
  And matching configuration in your javascript:
179
352
 
180
353
  ```javascript
181
354
  $(document).ready(function() {
182
- GmapsAutoComplete.init({inputField: 'form#search #address', errorField: 'form#search #address_error'});
183
- GmapsAutoComplete.autoCompleteInit({region: 'DK'});
355
+ var completer;
356
+ completer = new GmapsCompleter({inputField: 'form#search #address', errorField: 'form#search #address_error'});
357
+ completer.autoCompleteInit({region: 'DK'});
184
358
  });
185
359
  ```
186
360
 
361
+ ### Tips
362
+
363
+ To avoid loading google maps script on all pages, either use turbolinks or alternatively conditionally load it depending on whether the page needs it.
364
+ For this you could use a simple `Page` model, something like this:
365
+
366
+ ```ruby
367
+ class Page
368
+ include ::ActiveModel::Model
369
+
370
+ attr_accessor :name, :type, :mode
371
+
372
+ def map?
373
+ mode && mode.to_s =~ /location/
374
+ end
375
+ ```
376
+
377
+ Then use the Page in the controller
378
+
379
+ ```ruby
380
+ class PropertiesController < BaseController
381
+ def show
382
+ @name = params[:id]
383
+ @mode = params[:mode] || 'gallery'
384
+ @page = Page.new name: :property, mode: @mode
385
+ end
386
+ end
387
+ ```
388
+
389
+ Then use page instance to have fine-grained control over how to display the page!
390
+
391
+ ```erb
392
+ <% if @page.map? %>
393
+ <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
394
+ <% end %>
395
+ <%= javascript_include_tag "application" %>
396
+ ```
397
+
398
+ This could fx be integrated into your page layouts (layout files) or similar.
399
+
400
+ Alternatively perhaps use RequireJS via the `requirejs-rails` gem, and load it "AMD" style, and then use a HTML data attribute to set if the page should load the google map script or not. There are many ways to achieve this...
401
+
187
402
  Enjoy!
188
403
 
189
404
  ## TODO
190
405
 
191
- * better Javascript encapsulation
192
- * translation to Coffeescript using Coffee classes :)
406
+ * even better class based functionality encapsulation
407
+ * possibly remove `autoCompleteInit` ??
193
408
 
194
409
  Please help out with suggestions and improvements etc!
195
410
 
@@ -2,14 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
+ # stub: gmaps-autocomplete-rails 0.1.3 ruby lib
5
6
 
6
7
  Gem::Specification.new do |s|
7
8
  s.name = "gmaps-autocomplete-rails"
8
- s.version = "0.1.2.1"
9
+ s.version = "0.1.4"
9
10
 
10
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
11
13
  s.authors = ["Kristian Mandrup"]
12
- s.date = "2013-03-04"
14
+ s.date = "2014-08-23"
13
15
  s.description = "Easily add autocomplete geo-functionality to your Rails app :)"
14
16
  s.email = "kmandrup@gmail.com"
15
17
  s.extra_rdoc_files = [
@@ -17,48 +19,21 @@ Gem::Specification.new do |s|
17
19
  "README.md"
18
20
  ]
19
21
  s.files = [
20
- ".document",
21
- ".rspec",
22
- "CHANGELOG",
23
22
  "Gemfile",
24
- "Gemfile.lock",
25
23
  "LICENSE.txt",
26
24
  "README.md",
27
- "Rakefile",
28
- "VERSION",
29
25
  "gmaps-autocomplete-rails.gemspec",
30
26
  "lib/gmaps-autocomplete-rails.rb",
31
27
  "lib/gmaps-autocomplete/view_helper.rb",
32
- "spec/index.html",
33
- "spec/init.js",
34
- "spec/jquery-ui/images/ui-bg_flat_0_aaaaaa_40x100.png",
35
- "spec/jquery-ui/images/ui-bg_glass_55_fbf9ee_1x400.png",
36
- "spec/jquery-ui/images/ui-bg_glass_65_ffffff_1x400.png",
37
- "spec/jquery-ui/images/ui-bg_glass_75_dadada_1x400.png",
38
- "spec/jquery-ui/images/ui-bg_glass_75_e6e6e6_1x400.png",
39
- "spec/jquery-ui/images/ui-bg_glass_75_ffffff_1x400.png",
40
- "spec/jquery-ui/images/ui-bg_highlight-soft_75_cccccc_1x100.png",
41
- "spec/jquery-ui/images/ui-bg_inset-soft_95_fef1ec_1x100.png",
42
- "spec/jquery-ui/images/ui-icons_222222_256x240.png",
43
- "spec/jquery-ui/images/ui-icons_2e83ff_256x240.png",
44
- "spec/jquery-ui/images/ui-icons_454545_256x240.png",
45
- "spec/jquery-ui/images/ui-icons_888888_256x240.png",
46
- "spec/jquery-ui/images/ui-icons_cd0a0a_256x240.png",
47
- "spec/jquery-ui/images/ui-icons_f6cf3b_256x240.png",
48
- "spec/jquery-ui/jquery-ui-1.8.16.custom.css",
49
- "spec/jquery-ui/jquery.ui.1.8.16.ie.css",
50
- "spec/main.css",
51
- "spec/spec_helper.rb",
52
- "vendor/assets/javascripts/gmaps-autocomplete.js"
28
+ "vendor/assets/javascripts/gmaps-auto-complete.js"
53
29
  ]
54
30
  s.homepage = "http://github.com/kristianmandrup/gmaps-autocomplete-rails"
55
31
  s.licenses = ["MIT"]
56
- s.require_paths = ["lib"]
57
- s.rubygems_version = "1.8.25"
32
+ s.rubygems_version = "2.2.2"
58
33
  s.summary = "Google Maps v3 search with jQuery UI Autocomplete, ready for use with Rails asset pipeline"
59
34
 
60
35
  if s.respond_to? :specification_version then
61
- s.specification_version = 3
36
+ s.specification_version = 4
62
37
 
63
38
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
64
39
  s.add_runtime_dependency(%q<rails>, [">= 3.1.0"])