address_picker-rails 0.2.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.
Files changed (50) hide show
  1. data/.document +5 -0
  2. data/Gemfile +23 -0
  3. data/Gemfile.lock +155 -0
  4. data/Guardfile +37 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.md +212 -0
  7. data/Rakefile +49 -0
  8. data/VERSION +1 -0
  9. data/address_picker-rails.gemspec +115 -0
  10. data/lib/address_picker-rails.rb +4 -0
  11. data/lib/address_picker-rails/engine.rb +11 -0
  12. data/spec/dummy/README.rdoc +261 -0
  13. data/spec/dummy/Rakefile +7 -0
  14. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  15. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  16. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  17. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  18. data/spec/dummy/app/mailers/.gitkeep +0 -0
  19. data/spec/dummy/app/models/.gitkeep +0 -0
  20. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  21. data/spec/dummy/config.ru +4 -0
  22. data/spec/dummy/config/application.rb +65 -0
  23. data/spec/dummy/config/boot.rb +10 -0
  24. data/spec/dummy/config/database.yml +25 -0
  25. data/spec/dummy/config/environment.rb +5 -0
  26. data/spec/dummy/config/environments/development.rb +37 -0
  27. data/spec/dummy/config/environments/production.rb +67 -0
  28. data/spec/dummy/config/environments/test.rb +37 -0
  29. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  30. data/spec/dummy/config/initializers/inflections.rb +15 -0
  31. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  32. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  33. data/spec/dummy/config/initializers/session_store.rb +8 -0
  34. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  35. data/spec/dummy/config/locales/en.yml +5 -0
  36. data/spec/dummy/config/routes.rb +58 -0
  37. data/spec/dummy/lib/assets/.gitkeep +0 -0
  38. data/spec/dummy/log/.gitignore +1 -0
  39. data/spec/dummy/log/.gitkeep +0 -0
  40. data/spec/dummy/public/404.html +26 -0
  41. data/spec/dummy/public/422.html +26 -0
  42. data/spec/dummy/public/500.html +25 -0
  43. data/spec/dummy/public/favicon.ico +0 -0
  44. data/spec/dummy/script/rails +6 -0
  45. data/spec/spec_helper.rb +79 -0
  46. data/vendor/assets/javascripts/address_picker-rails.js +189 -0
  47. data/vendor/assets/javascripts/address_picker.js +209 -0
  48. data/vendor/assets/javascripts/address_picker_gmaps.js +130 -0
  49. data/vendor/assets/stylesheets/address_picker-rails.css +0 -0
  50. metadata +260 -0
File without changes
@@ -0,0 +1 @@
1
+ /*.log
File without changes
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,79 @@
1
+ require 'rubygems'
2
+ require 'spork'
3
+ #uncomment the following line to use spork with the debugger
4
+ #require 'spork/ext/ruby-debug'
5
+
6
+ Spork.prefork do
7
+
8
+
9
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
10
+ ENV["RAILS_ENV"] ||= 'test'
11
+ require File.expand_path("../dummy/config/environment", __FILE__)
12
+ #require File.expand_path("../../config/environment", __FILE__)
13
+ require 'rspec/rails'
14
+ require 'rspec/autorun'
15
+
16
+ # Requires supporting ruby files with custom matchers and macros, etc,
17
+ # in spec/support/ and its subdirectories.
18
+ Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
19
+
20
+ RSpec.configure do |config|
21
+ # ## Mock Framework
22
+ #
23
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
24
+ #
25
+ # config.mock_with :mocha
26
+ # config.mock_with :flexmock
27
+ # config.mock_with :rr
28
+
29
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
30
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
31
+
32
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
33
+ # examples within a transaction, remove the following line or assign false
34
+ # instead of true.
35
+ config.use_transactional_fixtures = true
36
+
37
+ # If true, the base class of anonymous controllers will be inferred
38
+ # automatically. This will be the default behavior in future versions of
39
+ # rspec-rails.
40
+ config.infer_base_class_for_anonymous_controllers = false
41
+ end
42
+
43
+ end
44
+
45
+ Spork.each_run do
46
+ # This code will be run each time you run your specs.
47
+
48
+ end
49
+
50
+ # --- Instructions ---
51
+ # Sort the contents of this file into a Spork.prefork and a Spork.each_run
52
+ # block.
53
+ #
54
+ # The Spork.prefork block is run only once when the spork server is started.
55
+ # You typically want to place most of your (slow) initializer code in here, in
56
+ # particular, require'ing any 3rd-party gems that you don't normally modify
57
+ # during development.
58
+ #
59
+ # The Spork.each_run block is run each time you run your specs. In case you
60
+ # need to load files that tend to change during development, require them here.
61
+ # With Rails, your application modules are loaded automatically, so sometimes
62
+ # this block can remain empty.
63
+ #
64
+ # Note: You can modify files loaded *from* the Spork.each_run block without
65
+ # restarting the spork server. However, this file itself will not be reloaded,
66
+ # so if you change any of the code inside the each_run block, you still need to
67
+ # restart the server. In general, if you have non-trivial code in this file,
68
+ # it's advisable to move it into a separate file so you can easily edit it
69
+ # without restarting spork. (For example, with RSpec, you could move
70
+ # non-trivial code into a file spec/support/my_helper.rb, making sure that the
71
+ # spec/support/* files are require'd from inside the each_run block.)
72
+ #
73
+ # Any code that is left outside the two blocks will be run during preforking
74
+ # *and* during each_run -- that's probably not what you want.
75
+ #
76
+ # These instructions should self-destruct in 10 seconds. If they don't, feel
77
+ # free to delete them.
78
+
79
+
@@ -0,0 +1,189 @@
1
+ //= require jquery
2
+ //= require jquery_ujs
3
+ //= require jquery-ui
4
+ //= require address_picker
5
+ //= require address_picker_gmaps
6
+
7
+ var AddressPickerRails = AddressPickerRails || {};
8
+
9
+ //
10
+ //
11
+ //
12
+ // ---------------------------------------------------------------------------------------------------------------------
13
+ // AddressPickerRails.Pickers
14
+ // ---------------------------------------------------------------------------------------------------------------------
15
+ //
16
+
17
+ /**
18
+ * Static helper methods.
19
+ */
20
+ AddressPickerRails.Pickers = function (my) {
21
+
22
+ var pickers = {};
23
+
24
+ /**
25
+ * Apply the address picker to the fields having the specified CSS class prefix.
26
+ * Load the Google Maps API if needed, then call the onLoad function if provided (for every address picker).
27
+ * @param options the picker options :
28
+ * - 'classPrefix' default to 'address-picker'
29
+ * - 'onLoad' default to 'null'
30
+ */
31
+ my.apply = function (options) {
32
+ var settings = jQuery.extend({
33
+ 'classPrefix':'address-picker',
34
+ 'onLoad' :null
35
+ }, options);
36
+
37
+ //console.debug("Looking for address pickers with class prefix '%s'", settings.classPrefix);
38
+
39
+ jQuery("." + settings.classPrefix + "-input").each(function (index, element) {
40
+ //console.debug("Found address picker with ID '%s'", element.id);
41
+ var picker = new AddressPickerRails.Picker({ idPrefix:element.id })
42
+ picker.apply(settings.onLoad);
43
+ pickers[element.id] = picker;
44
+ });
45
+ };
46
+
47
+ /**
48
+ * Wrap 'AddressPickerRails.Pickers.apply' in jQuery(document).ready().
49
+ * @param options the picker options :
50
+ * - 'classPrefix' default to 'address-picker'
51
+ * - 'onLoad' default to 'null'
52
+ */
53
+ my.applyOnReady = function (options) {
54
+ jQuery(document).ready(function () {
55
+ AddressPickerRails.Pickers.apply(options);
56
+ });
57
+ };
58
+
59
+ /**
60
+ * Returns the AddressPickerRails.Picker for the specified prefix.
61
+ * @param idPrefix the prefix of the AddressPickerRails.Picker to return.
62
+ * @return the AddressPickerRails.Picker for the specified prefix.
63
+ */
64
+ my.get = function (idPrefix) {
65
+ return pickers[idPrefix];
66
+ };
67
+
68
+ return my;
69
+
70
+ }(AddressPickerRails.Pickers || {});
71
+
72
+ //
73
+ //
74
+ //
75
+ // ---------------------------------------------------------------------------------------------------------------------
76
+ // AddressPickerRails.Picker
77
+ // ---------------------------------------------------------------------------------------------------------------------
78
+ //
79
+
80
+ /**
81
+ * An address picker defined by its IDs prefix.
82
+ * The address picker will be applied to the fields identified by:
83
+ * - '<prefix>' (**required**)
84
+ * - '<prefix>_map' (optional)
85
+ * - '<prefix>_latitude' (optional)
86
+ * - '<prefix>_longitude' (optional)
87
+ * - '<prefix>_locality' (optional)
88
+ * - '<prefix>_country' (optional)
89
+ * @param options the picker options :
90
+ * - 'idPrefix' default to 'address'
91
+ */
92
+ AddressPickerRails.Picker = function (options) {
93
+
94
+ // ---------- private fields ---------------------------------------------------------------------------------------
95
+
96
+ var settings = jQuery.extend({
97
+ 'idPrefix':'address'
98
+ }, options);
99
+
100
+ var picker;
101
+ var onLoadCallback;
102
+ var that = this;
103
+
104
+ // ---------- private methods --------------------------------------------------------------------------------------
105
+
106
+ /**
107
+ * Test if a field with an ID of 'settings.idPrefix' exists.
108
+ * @return true if the field exists, false otherwise.
109
+ */
110
+ var isExisting = function () {
111
+ return jQuery("#" + settings.idPrefix).length > 0;
112
+ };
113
+
114
+ /**
115
+ * Apply the address picker to the fields identified by the ID prefix.
116
+ */
117
+ var define = function () {
118
+ //console.debug("Defining address picker with ID prefix '%s'", settings.idPrefix);
119
+
120
+ picker = $("#" + settings.idPrefix).addresspicker({
121
+ elements:{
122
+ map :"#" + settings.idPrefix + "_map",
123
+ lat :"#" + settings.idPrefix + "_latitude",
124
+ lng :"#" + settings.idPrefix + "_longitude",
125
+ locality:"#" + settings.idPrefix + "_locality",
126
+ country :"#" + settings.idPrefix + "_country"
127
+ }
128
+ });
129
+ picker.addresspicker("marker").setVisible(true);
130
+ picker.addresspicker("updatePosition");
131
+ };
132
+
133
+ /**
134
+ * !!! PRIVATE !!!
135
+ * Define the address picker then call the callback function if any.
136
+ * This is intended to be used by the Google Maps API and should not be called directly.
137
+ */
138
+ this.callback = function () {
139
+ AddressPickerRails.OriginalPicker.define();
140
+
141
+ if (isExisting()) {
142
+ // execute the callback if provided
143
+
144
+ // define the address picker
145
+ define();
146
+ if (onLoadCallback) {
147
+ onLoadCallback(that);
148
+ }
149
+ }
150
+ };
151
+
152
+ // ---------- public methods ---------------------------------------------------------------------------------------
153
+
154
+ /**
155
+ * Returns the ID prefix.
156
+ * @return the ID prefix.
157
+ */
158
+ this.getIdPrefix = function () {
159
+ return settings.idPrefix;
160
+ };
161
+
162
+ /**
163
+ * Returns the JQuery address picker.
164
+ * @return the JQuery address picker.
165
+ */
166
+ this.getPicker = function () {
167
+ return picker;
168
+ };
169
+
170
+ /**
171
+ * Apply the address picker to the fields defined by the IDs prefix.
172
+ * Load the Google Maps API if needed, then call the onLoad function if provided.
173
+ * @param onLoad the function to call after the Google Maps API is loaded and the picker is applied (optional). The
174
+ * callback may take the AddressPickerRails.Picker as argument.
175
+ */
176
+ this.apply = function (onLoad) {
177
+ //console.debug("Applying address picker to ID prefix '%s'", settings.idPrefix);
178
+
179
+ // set the callback if provided, reset it otherwise
180
+ if (arguments.length == 1 && typeof onLoad === "function") {
181
+ onLoadCallback = onLoad;
182
+ } else {
183
+ onLoadCallback = null;
184
+ }
185
+
186
+ AddressPickerRails.GMaps.instance().load(this.callback);
187
+ };
188
+
189
+ };
@@ -0,0 +1,209 @@
1
+ var AddressPickerRails = AddressPickerRails || {}
2
+
3
+ /**
4
+ * The original script, encapsulated for deferred loading, since Google Maps API may not be defined yet.
5
+ */
6
+ AddressPickerRails.OriginalPicker = function (my) {
7
+
8
+ my.define = function () {
9
+ //console.debug("Defining jquery.ui.addresspicker");
10
+
11
+ /***************************************************************************************************************
12
+ *
13
+ * From https://github.com/sgruhier/jquery-addresspicker/blob/master/src/jquery.ui.addresspicker.js
14
+ * Version of 2012.06.04 08:36:57
15
+ * Starts here
16
+ *
17
+ **************************************************************************************************************/
18
+
19
+ /*
20
+ * jQuery UI addresspicker @VERSION
21
+ *
22
+ * Copyright 2010, AUTHORS.txt (http://jqueryui.com/about)
23
+ * Dual licensed under the MIT or GPL Version 2 licenses.
24
+ * http://jquery.org/license
25
+ *
26
+ * http://docs.jquery.com/UI/Progressbar
27
+ *
28
+ * Depends:
29
+ * jquery.ui.core.js
30
+ * jquery.ui.widget.js
31
+ * jquery.ui.autocomplete.js
32
+ */
33
+ (function ($, undefined) {
34
+
35
+ $.widget("ui.addresspicker", {
36
+ options:{
37
+ appendAddressString:"",
38
+ draggableMarker :true,
39
+ regionBias :null,
40
+ mapOptions :{
41
+ zoom :5,
42
+ center :new google.maps.LatLng(46, 2),
43
+ scrollwheel:false,
44
+ mapTypeId :google.maps.MapTypeId.ROADMAP
45
+ },
46
+ elements :{
47
+ map :false,
48
+ lat :false,
49
+ lng :false,
50
+ locality:false,
51
+ country :false,
52
+ type :false
53
+ }
54
+ },
55
+
56
+ marker:function () {
57
+ return this.gmarker;
58
+ },
59
+
60
+ map:function () {
61
+ return this.gmap;
62
+ },
63
+
64
+ updatePosition:function () {
65
+ this._updatePosition(this.gmarker.getPosition());
66
+ },
67
+
68
+ reloadPosition:function () {
69
+ this.gmarker.setVisible(true);
70
+ this.gmarker.setPosition(new google.maps.LatLng(this.lat.val(), this.lng.val()));
71
+ this.gmap.setCenter(this.gmarker.getPosition());
72
+ },
73
+
74
+ selected:function () {
75
+ return this.selectedResult;
76
+ },
77
+
78
+ _create:function () {
79
+ this.geocoder = new google.maps.Geocoder();
80
+ this.element.autocomplete({
81
+ source:$.proxy(this._geocode, this),
82
+ focus :$.proxy(this._focusAddress, this),
83
+ select:$.proxy(this._selectAddress, this)
84
+ });
85
+
86
+ this.lat = $(this.options.elements.lat);
87
+ this.lng = $(this.options.elements.lng);
88
+ this.locality = $(this.options.elements.locality);
89
+ this.country = $(this.options.elements.country);
90
+ this.type = $(this.options.elements.type);
91
+ if (this.options.elements.map) {
92
+ this.mapElement = $(this.options.elements.map);
93
+ this._initMap();
94
+ }
95
+ },
96
+
97
+ _initMap:function () {
98
+ if (this.lat && this.lat.val()) {
99
+ this.options.mapOptions.center = new google.maps.LatLng(this.lat.val(), this.lng.val());
100
+ }
101
+
102
+ this.gmap = new google.maps.Map(this.mapElement[0], this.options.mapOptions);
103
+ this.gmarker = new google.maps.Marker({
104
+ position :this.options.mapOptions.center,
105
+ map :this.gmap,
106
+ draggable:this.options.draggableMarker});
107
+ google.maps.event.addListener(this.gmarker, 'dragend', $.proxy(this._markerMoved, this));
108
+ this.gmarker.setVisible(false);
109
+ },
110
+
111
+ _updatePosition:function (location) {
112
+ if (this.lat) {
113
+ this.lat.val(location.lat());
114
+ }
115
+ if (this.lng) {
116
+ this.lng.val(location.lng());
117
+ }
118
+ },
119
+
120
+ _markerMoved:function () {
121
+ this._updatePosition(this.gmarker.getPosition());
122
+ },
123
+
124
+ // Autocomplete source method: fill its suggests with google geocoder results
125
+ _geocode :function (request, response) {
126
+ var address = request.term, self = this;
127
+ this.geocoder.geocode({
128
+ 'address':address + this.options.appendAddressString,
129
+ 'region' :this.options.regionBias
130
+ }, function (results, status) {
131
+ if (status == google.maps.GeocoderStatus.OK) {
132
+ for (var i = 0; i < results.length; i++) {
133
+ results[i].label = results[i].formatted_address;
134
+ }
135
+ ;
136
+ }
137
+ response(results);
138
+ })
139
+ },
140
+
141
+ _findInfo:function (result, type) {
142
+ for (var i = 0; i < result.address_components.length; i++) {
143
+ var component = result.address_components[i];
144
+ if (component.types.indexOf(type) != -1) {
145
+ return component.long_name;
146
+ }
147
+ }
148
+ return false;
149
+ },
150
+
151
+ _focusAddress:function (event, ui) {
152
+ var address = ui.item;
153
+ if (!address) {
154
+ return;
155
+ }
156
+
157
+ if (this.gmarker) {
158
+ this.gmarker.setPosition(address.geometry.location);
159
+ this.gmarker.setVisible(true);
160
+
161
+ this.gmap.fitBounds(address.geometry.viewport);
162
+ }
163
+ this._updatePosition(address.geometry.location);
164
+
165
+ if (this.locality) {
166
+ this.locality.val(this._findInfo(address, 'locality'));
167
+ }
168
+ if (this.country) {
169
+ this.country.val(this._findInfo(address, 'country'));
170
+ }
171
+ if (this.type) {
172
+ this.type.val(address.types[0]);
173
+ }
174
+ },
175
+
176
+ _selectAddress:function (event, ui) {
177
+ this.selectedResult = ui.item;
178
+ }
179
+ });
180
+
181
+ $.extend($.ui.addresspicker, {
182
+ version:"@VERSION"
183
+ });
184
+
185
+ // make IE think it doesn't suck
186
+ if (!Array.indexOf) {
187
+ Array.prototype.indexOf = function (obj) {
188
+ for (var i = 0; i < this.length; i++) {
189
+ if (this[i] == obj) {
190
+ return i;
191
+ }
192
+ }
193
+ return -1;
194
+ }
195
+ }
196
+
197
+ })(jQuery);
198
+
199
+ /***************************************************************************************************************
200
+ *
201
+ * From https://github.com/sgruhier/jquery-addresspicker/blob/master/src/jquery.ui.addresspicker.js
202
+ * Ends here
203
+ *
204
+ **************************************************************************************************************/
205
+ };
206
+
207
+ return my;
208
+
209
+ }(AddressPickerRails.OriginalPicker || {});