gmaps4rails 0.8.7 → 0.8.8

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.
@@ -34,11 +34,11 @@ module Gmaps4rails
34
34
  return ", \"picture\": \"#{object.gmaps4rails_marker_picture['picture']}\", \"width\": \"#{object.gmaps4rails_marker_picture['width']}\", \"height\": \"#{object.gmaps4rails_marker_picture['height']}\"" if object.respond_to?("gmaps4rails_marker_picture")
35
35
  end
36
36
 
37
- def Gmaps4rails.geocode(address, raw = false)
37
+ def Gmaps4rails.geocode(address, lang="en", raw = false)
38
38
  if address.nil? || address.empty?
39
39
  raise Gmaps4rails::GeocodeInvalidQuery, "You must provide an address"
40
40
  else #coordinates are valid
41
- geocoder = "http://maps.googleapis.com/maps/api/geocode/json?address="
41
+ geocoder = "http://maps.googleapis.com/maps/api/geocode/json?language=#{lang}&address="
42
42
  output = "&sensor=false"
43
43
  #send request to the google api to get the lat/lng
44
44
  request = geocoder + address + output
@@ -58,7 +58,8 @@ module Gmaps4rails
58
58
  :lat => result["geometry"]["location"]["lat"],
59
59
  :lng => result["geometry"]["location"]["lng"],
60
60
  :matched_address => result["formatted_address"],
61
- :bounds => result["geometry"]["bounds"]
61
+ :bounds => result["geometry"]["bounds"],
62
+ :full_data => result
62
63
  }
63
64
  end
64
65
  return array
@@ -139,7 +140,7 @@ module Gmaps4rails
139
140
  #to prevent geocoding each time a save is made
140
141
  return true if gmaps4rails_options[:check_process] == true && self.send(gmaps4rails_options[:checker]) == true
141
142
  begin
142
- coordinates = Gmaps4rails.geocode(self.send(gmaps4rails_options[:address]))
143
+ coordinates = Gmaps4rails.geocode(self.send(gmaps4rails_options[:address]), gmaps4rails_options[:language])
143
144
  rescue GeocodeStatus, GeocodeInvalidQuery #address was invalid, add error to base.
144
145
  errors[gmaps4rails_options[:address]] << gmaps4rails_options[:msg] if gmaps4rails_options[:validation]
145
146
  rescue GeocodeNetStatus => e #connection error, No need to prevent save.
@@ -151,6 +152,8 @@ module Gmaps4rails
151
152
  unless gmaps4rails_options[:normalized_address].nil?
152
153
  self.send(gmaps4rails_options[:normalized_address].to_s+"=", coordinates.first[:matched_address])
153
154
  end
155
+ # Call the callback method to let the user do what he wants with the data
156
+ self.send(gmaps4rails_options[:callback], coordinates.first[:full_data]) unless gmaps4rails_options[:callback].nil?
154
157
  if gmaps4rails_options[:check_process] == true
155
158
  self[gmaps4rails_options[:checker]] = true
156
159
  end
@@ -182,7 +185,9 @@ module Gmaps4rails
182
185
  :msg => args[:msg] || "Address invalid",
183
186
  :validation => args[:validation].nil? ? true : args[:validation],
184
187
  :normalized_address => args[:normalized_address],
185
- :address => args[:address] || "gmaps4rails_address"
188
+ :address => args[:address] || "gmaps4rails_address",
189
+ :callback => args[:callback],
190
+ :language => args[:language] || "en"
186
191
  #TODO: address as a proc?
187
192
  }
188
193
  end
@@ -42,6 +42,7 @@ var Gmaps4Rails = {
42
42
  list_container: null, // id of the ul that will host links to all markers
43
43
  custom_cluster_pictures: null,
44
44
  custom_infowindow_class: null,
45
+ offset: 0 //used when adding_markers to an existing map. Because new markers are concated with previous one, offset is here to avoid the existing are re-created.
45
46
  },
46
47
 
47
48
  //Stored variables
@@ -362,7 +363,7 @@ var Gmaps4Rails = {
362
363
 
363
364
  //create google.maps Markers from data provided by user
364
365
  create_google_markers_from_markers: function() {
365
- for (var i = 0; i < this.markers.length; ++i) {
366
+ for (var i = this.markers_conf.offset; i < this.markers.length; ++i) {
366
367
  //check if the marker has not already been created
367
368
  if (!this.exists(this.markers[i].google_object)) {
368
369
  //test if value passed or use default
@@ -404,6 +405,7 @@ var Gmaps4Rails = {
404
405
  this.create_sidebar(this.markers[i]);
405
406
  }
406
407
  }
408
+ this.markers_conf.offset = this.markers.length;
407
409
  },
408
410
 
409
411
  // calculate anchor point for MarkerImage
@@ -453,9 +455,10 @@ var Gmaps4Rails = {
453
455
 
454
456
  // clear markers
455
457
  clear_markers: function() {
456
- if (this.markerClusterer !== null){
457
- this.markerClusterer.clearMarkers();
458
- }
458
+ // //clear clusterer first
459
+ // if (this.markerClusterer !== null){
460
+ // this.markerClusterer.clearMarkers();
461
+ // }
459
462
  for (var i = 0; i < this.markers.length; ++i) {
460
463
  this.clear_marker(this.markers[i]);
461
464
  }
@@ -488,6 +491,8 @@ var Gmaps4Rails = {
488
491
 
489
492
  // replace old markers with new markers on an existing map
490
493
  replace_markers: function(new_markers){
494
+ //reset the offset
495
+ this.markers_conf.offset = 0;
491
496
  //reset previous markers
492
497
  this.markers = new Array;
493
498
  //reset current bounds
@@ -501,7 +506,7 @@ var Gmaps4Rails = {
501
506
  //add new markers to on an existing map
502
507
  add_markers: function(new_markers){
503
508
  //clear the whole map
504
- this.clear_markers();
509
+ //this.clear_markers();
505
510
  //update the list of markers to take into account
506
511
  this.markers = this.markers.concat(new_markers);
507
512
  //put markers on the map
@@ -513,6 +518,11 @@ var Gmaps4Rails = {
513
518
  {
514
519
  if (this.markers_conf.do_clustering === true)
515
520
  {
521
+ //first clear the existing clusterer if any
522
+ if (this.markerClusterer !== null) {
523
+ this.markerClusterer.clearMarkers();
524
+ }
525
+
516
526
  var gmarkers_array = new Array;
517
527
  for (var i = 0; i < this.markers.length; ++i) {
518
528
  gmarkers_array.push(this.markers[i].google_object);
@@ -6,11 +6,6 @@ class User < ActiveRecord::Base
6
6
  sec_address
7
7
  end
8
8
 
9
- #
10
- def gmaps4rails_infowindow
11
- "My Beautiful Name: #{name}"
12
- end
13
-
14
9
  # def gmaps4rails_marker_picture
15
10
  # {
16
11
  # "picture" => "http://www.blankdots.com/img/github-32x32.png",
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe "Geocode" do
4
4
 
5
5
  it "should geocode properly an address" do
6
- Gmaps4rails.geocode("alaska").should == [{:matched_address=>"Alaska, USA", :bounds=>{"northeast"=>{"lng"=>-129.979511, "lat"=>71.441059}, "southwest"=>{"lng"=>172.347846, "lat"=>51.175092}}, :lat=>63.588753, :lng=>-154.4930619}]
6
+ Gmaps4rails.geocode("alaska").should be_an(Array)
7
7
  end
8
8
 
9
9
  it "should raise an error when address invalid" do
@@ -13,10 +13,13 @@ describe "Acts as gmappable" do
13
13
  :checker => "gmaps",
14
14
  :msg => "Address invalid",
15
15
  :validation => true,
16
- :address => "gmaps4rails_address"
16
+ :address => "gmaps4rails_address",
17
+ :language => "en"
17
18
  }
18
19
  end
19
20
  end
21
+ @toulon = { :latitude => 43.124228, :longitude => 5.928}
22
+ @paris = { :latitude => 48.856614, :longitude => 2.3522219}
20
23
  end
21
24
 
22
25
  describe "standard configuration, valid user" do
@@ -25,8 +28,8 @@ describe "Acts as gmappable" do
25
28
  end
26
29
 
27
30
  it "should have a geocoded position" do
28
- @user.latitude.should == 43.1251606
29
- @user.longitude.should == 5.9311119
31
+ @user.latitude.should == @toulon[:latitude]
32
+ @user.longitude.should == @toulon[:longitude]
30
33
  end
31
34
 
32
35
  it "should set boolean to true once user is created" do
@@ -35,26 +38,26 @@ describe "Acts as gmappable" do
35
38
 
36
39
  it "should render a valid json from an array of ojects" do
37
40
  @user2 = Factory(:user_paris)
38
- User.all.to_gmaps4rails.should == "[{\"longitude\": \"5.9311119\", \"latitude\": \"43.1251606\"},\n{\"longitude\": \"2.3509871\", \"latitude\": \"48.8566667\"}]"
41
+ User.all.to_gmaps4rails.should == "[{\"longitude\": \"" + @toulon[:longitude].to_s + "\", \"latitude\": \"" + @toulon[:latitude].to_s + "\"},\n{\"longitude\": \"" + @paris[:longitude].to_s + "\", \"latitude\": \"" + @paris[:latitude].to_s + "\"}]"
39
42
  end
40
43
 
41
44
  it "should render a valid json from a single object" do
42
- @user.to_gmaps4rails.should == "[{\"longitude\": \"5.9311119\", \"latitude\": \"43.1251606\"}]"
45
+ @user.to_gmaps4rails.should == "[{\"longitude\": \"" + @toulon[:longitude].to_s + "\", \"latitude\": \"" + @toulon[:latitude].to_s + "\"}]"
43
46
  end
44
47
 
45
48
  it "should not geocode again after address changes if checker is true" do
46
49
  @user.sec_address = "paris, France"
47
50
  @user.save
48
- @user.latitude.should == 43.1251606
49
- @user.longitude.should == 5.9311119
51
+ @user.latitude.should == @toulon[:latitude]
52
+ @user.longitude.should == @toulon[:longitude]
50
53
  end
51
54
 
52
55
  it "should geocode after address changes if checker is false" do
53
56
  @user.sec_address = "paris, France"
54
57
  @user.gmaps = false
55
58
  @user.save
56
- @user.latitude.should == 48.8566667
57
- @user.longitude.should == 2.3509871
59
+ @user.latitude.should == @paris[:latitude]
60
+ @user.longitude.should == @paris[:longitude]
58
61
  end
59
62
  end
60
63
 
@@ -90,13 +93,14 @@ describe "Acts as gmappable" do
90
93
  :checker => "gmaps",
91
94
  :msg => "Address invalid",
92
95
  :validation => true,
93
- :address => "sec_address"
96
+ :address => "sec_address",
97
+ :language => "en"
94
98
  }
95
99
  end
96
100
  end
97
101
  @user = Factory(:user)
98
- @user.latitude.should == 43.1251606
99
- @user.longitude.should == 5.9311119
102
+ @user.latitude.should == @toulon[:latitude]
103
+ @user.longitude.should == @toulon[:longitude]
100
104
  end
101
105
 
102
106
  it "should save the normalized address if requested" do
@@ -110,7 +114,8 @@ describe "Acts as gmappable" do
110
114
  :msg => "Address invalid",
111
115
  :validation => true,
112
116
  :normalized_address => "norm_address",
113
- :address => "gmaps4rails_address"
117
+ :address => "gmaps4rails_address",
118
+ :language => "en"
114
119
  }
115
120
  end
116
121
  end
@@ -129,7 +134,8 @@ describe "Acts as gmappable" do
129
134
  :msg => "Custom Address invalid",
130
135
  :validation => true,
131
136
  :normalized_address => "sec_address",
132
- :address => "gmaps4rails_address"
137
+ :address => "gmaps4rails_address",
138
+ :language => "en"
133
139
  }
134
140
  end
135
141
  end
@@ -147,7 +153,8 @@ describe "Acts as gmappable" do
147
153
  :checker => "gmaps",
148
154
  :msg => "Custom Address invalid",
149
155
  :validation => true,
150
- :address => "gmaps4rails_address"
156
+ :address => "gmaps4rails_address",
157
+ :language => "en"
151
158
  }
152
159
  end
153
160
  end
@@ -165,7 +172,8 @@ describe "Acts as gmappable" do
165
172
  :checker => "gmaps",
166
173
  :msg => "Address invalid",
167
174
  :validation => false,
168
- :address => "gmaps4rails_address"
175
+ :address => "gmaps4rails_address",
176
+ :language => "en"
169
177
  }
170
178
  end
171
179
  end
@@ -183,13 +191,14 @@ describe "Acts as gmappable" do
183
191
  :checker => "gmaps",
184
192
  :msg => "Address invalid",
185
193
  :validation => true,
186
- :address => "gmaps4rails_address"
194
+ :address => "gmaps4rails_address",
195
+ :language => "en"
187
196
  }
188
197
  end
189
198
  end
190
199
  @user = Factory(:user)
191
- @user.lat_test.should == 43.1251606
192
- @user.long_test.should == 5.9311119
200
+ @user.lat_test.should == @toulon[:latitude]
201
+ @user.long_test.should == @toulon[:longitude]
193
202
  @user.longitude.should == nil
194
203
  @user.latitude.should == nil
195
204
  end
@@ -204,7 +213,8 @@ describe "Acts as gmappable" do
204
213
  :checker => "gmaps",
205
214
  :msg => "Address invalid",
206
215
  :validation => true,
207
- :address => "gmaps4rails_address"
216
+ :address => "gmaps4rails_address",
217
+ :language => "en"
208
218
  }
209
219
  end
210
220
  end
@@ -222,15 +232,16 @@ describe "Acts as gmappable" do
222
232
  :checker => "gmaps",
223
233
  :msg => "Address invalid",
224
234
  :validation => true,
225
- :address => "gmaps4rails_address"
235
+ :address => "gmaps4rails_address",
236
+ :language => "en"
226
237
  }
227
238
  end
228
239
  end
229
240
  @user = Factory(:user)
230
241
  @user.sec_address = "paris, France"
231
242
  @user.save
232
- @user.latitude.should == 48.8566667
233
- @user.longitude.should == 2.3509871
243
+ @user.latitude.should == @paris[:latitude]
244
+ @user.longitude.should == @paris[:longitude]
234
245
  end
235
246
 
236
247
  it "should save to the proper boolean checker set in checker" do
@@ -243,7 +254,8 @@ describe "Acts as gmappable" do
243
254
  :checker => "bool_test",
244
255
  :msg => "Address invalid",
245
256
  :validation => true,
246
- :address => "gmaps4rails_address"
257
+ :address => "gmaps4rails_address",
258
+ :language => "en"
247
259
  }
248
260
  end
249
261
  end
@@ -259,7 +271,7 @@ describe "Acts as gmappable" do
259
271
  "My Beautiful Picture: #{picture}"
260
272
  end
261
273
  end
262
- @user.to_gmaps4rails.should == "[{\"description\": \"My Beautiful Picture: http://www.blankdots.com/img/github-32x32.png\", \"longitude\": \"5.9311119\", \"latitude\": \"43.1251606\"}]"
274
+ @user.to_gmaps4rails.should == "[{\"description\": \"My Beautiful Picture: http://www.blankdots.com/img/github-32x32.png\", \"longitude\": \"" + @toulon[:longitude].to_s + "\", \"latitude\": \"" + @toulon[:latitude].to_s + "\"}]"
263
275
  end
264
276
 
265
277
  it "should take into account the picture provided in the model" do
@@ -273,7 +285,7 @@ describe "Acts as gmappable" do
273
285
  }
274
286
  end
275
287
  end
276
- @user.to_gmaps4rails.should == "[{\"longitude\": \"5.9311119\", \"latitude\": \"43.1251606\", \"picture\": \"http://www.blankdots.com/img/github-32x32.png\", \"width\": \"32\", \"height\": \"32\"}]"
288
+ @user.to_gmaps4rails.should == "[{\"longitude\": \"" + @toulon[:longitude].to_s + "\", \"latitude\": \"" + @toulon[:latitude].to_s + "\", \"picture\": \"http://www.blankdots.com/img/github-32x32.png\", \"width\": \"32\", \"height\": \"32\"}]"
277
289
  end
278
290
 
279
291
  it "should take into account the title provided in the model" do
@@ -283,7 +295,7 @@ describe "Acts as gmappable" do
283
295
  "Sweet Title"
284
296
  end
285
297
  end
286
- @user.to_gmaps4rails.should == "[{\"title\": \"Sweet Title\", \"longitude\": \"5.9311119\", \"latitude\": \"43.1251606\"}]"
298
+ @user.to_gmaps4rails.should == "[{\"title\": \"Sweet Title\", \"longitude\": \"" + @toulon[:longitude].to_s + "\", \"latitude\": \"" + @toulon[:latitude].to_s + "\"}]"
287
299
  end
288
300
 
289
301
  it "should take into account the sidebar content provided in the model" do
@@ -293,7 +305,7 @@ describe "Acts as gmappable" do
293
305
  "sidebar content"
294
306
  end
295
307
  end
296
- @user.to_gmaps4rails.should == "[{\"sidebar\": \"sidebar content\",\"longitude\": \"5.9311119\", \"latitude\": \"43.1251606\"}]"
308
+ @user.to_gmaps4rails.should == "[{\"sidebar\": \"sidebar content\",\"longitude\": \"" + @toulon[:longitude].to_s + "\", \"latitude\": \"" + @toulon[:latitude].to_s + "\"}]"
297
309
  end
298
310
 
299
311
  it "should take into account all additional data provided in the model" do
@@ -320,7 +332,53 @@ describe "Acts as gmappable" do
320
332
  "sidebar content"
321
333
  end
322
334
  end
323
- @user.to_gmaps4rails.should == "[{\"description\": \"My Beautiful Picture: \", \"title\": \"Sweet Title\", \"sidebar\": \"sidebar content\",\"longitude\": \"5.9311119\", \"latitude\": \"43.1251606\", \"picture\": \"http://www.blankdots.com/img/github-32x32.png\", \"width\": \"32\", \"height\": \"32\"}]"
335
+ @user.to_gmaps4rails.should == "[{\"description\": \"My Beautiful Picture: \", \"title\": \"Sweet Title\", \"sidebar\": \"sidebar content\",\"longitude\": \"" + @toulon[:longitude].to_s + "\", \"latitude\": \"" + @toulon[:latitude].to_s + "\", \"picture\": \"http://www.blankdots.com/img/github-32x32.png\", \"width\": \"32\", \"height\": \"32\"}]"
336
+ end
337
+
338
+ it "should call a callback in the model if asked to" do
339
+ User.class_eval do
340
+ def gmaps4rails_options
341
+ {
342
+ :lat_column => "latitude",
343
+ :lng_column => "longitude",
344
+ :check_process => true,
345
+ :checker => "gmaps",
346
+ :msg => "Address invalid",
347
+ :validation => true,
348
+ :address => "gmaps4rails_address",
349
+ :language => "en",
350
+ :callback => "save_callback"
351
+ }
352
+ end
353
+
354
+ def save_callback(data)
355
+ self.called_back = true
356
+ end
357
+
358
+ attr_accessor :called_back
359
+ end
360
+ @user = Factory(:user)
361
+ @user.called_back.should == true
362
+ end
363
+
364
+ it "should return results in the specified language" do
365
+ User.class_eval do
366
+ def gmaps4rails_options
367
+ {
368
+ :lat_column => "latitude",
369
+ :lng_column => "longitude",
370
+ :check_process => true,
371
+ :checker => "gmaps",
372
+ :msg => "Address invalid",
373
+ :validation => true,
374
+ :address => "gmaps4rails_address",
375
+ :language => "de",
376
+ :normalized_address => "norm_address"
377
+ }
378
+ end
379
+ end
380
+ @user = Factory(:user)
381
+ @user.norm_address.should == "Toulon, Frankreich"
324
382
  end
325
383
  end
326
384
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gmaps4rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 49
4
+ hash: 47
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 8
9
- - 7
10
- version: 0.8.7
9
+ - 8
10
+ version: 0.8.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Benjamin Roth
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-05-28 00:00:00 +02:00
19
+ date: 2011-06-11 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency