gmaps4rails 1.5.8 → 2.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/.gitignore +12 -0
  2. data/.travis.yml +9 -0
  3. data/Gemfile +7 -0
  4. data/Gemfile.lock +149 -0
  5. data/Guardfile.old +24 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.rdoc +129 -0
  8. data/Rakefile +4 -0
  9. data/app/views/gmaps4rails/_gmaps4rails.html.erb +20 -0
  10. data/gmaps4rails.gemspec +38 -0
  11. data/lib/generators/gmaps4rails/install_generator.rb +43 -0
  12. data/lib/generators/templates/README +2 -0
  13. data/lib/gmaps4rails.rb +26 -0
  14. data/lib/gmaps4rails/acts_as_gmappable.rb +54 -0
  15. data/lib/gmaps4rails/api_wrappers/base_net_methods.rb +40 -0
  16. data/lib/gmaps4rails/api_wrappers/direction.rb +87 -0
  17. data/lib/gmaps4rails/api_wrappers/geocoder.rb +54 -0
  18. data/lib/gmaps4rails/api_wrappers/places.rb +74 -0
  19. data/lib/gmaps4rails/base.rb +126 -0
  20. data/lib/gmaps4rails/extensions/enumerable.rb +14 -0
  21. data/lib/gmaps4rails/extensions/hash.rb +9 -0
  22. data/lib/gmaps4rails/helper/gmaps4rails_helper.rb +33 -0
  23. data/lib/gmaps4rails/js_builder.rb +154 -0
  24. data/lib/gmaps4rails/json_builder.rb +140 -0
  25. data/lib/gmaps4rails/model_handler.rb +101 -0
  26. data/lib/gmaps4rails/version.rb +3 -0
  27. data/lib/gmaps4rails/view_helper.rb +172 -0
  28. data/lib/tasks/jasmine.rake +8 -0
  29. data/public/javascripts/gmaps4rails/all.js +2144 -0
  30. data/public/javascripts/gmaps4rails/base.js +792 -0
  31. data/public/javascripts/gmaps4rails/google.js +556 -0
  32. data/public/javascripts/gmaps4rails/openlayers.js +377 -0
  33. data/public/stylesheets/gmaps4rails.css +24 -0
  34. data/spec/dummy/.gitignore +3 -0
  35. data/spec/dummy/.rspec +1 -0
  36. data/spec/dummy/README.rdoc +261 -0
  37. data/spec/dummy/Rakefile +7 -0
  38. data/spec/dummy/app/assets/javascripts/application.js +18 -0
  39. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  40. data/spec/dummy/app/assets/stylesheets/gmaps4rails.css +24 -0
  41. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  42. data/spec/dummy/app/controllers/users_controller.rb +105 -0
  43. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  44. data/spec/dummy/app/mailers/.gitkeep +0 -0
  45. data/spec/dummy/app/models/.gitkeep +0 -0
  46. data/spec/dummy/app/models/place.rb +16 -0
  47. data/spec/dummy/app/models/user.rb +12 -0
  48. data/spec/dummy/app/views/layouts/application.html.erb +17 -0
  49. data/spec/dummy/app/views/users/_form.html.erb +37 -0
  50. data/spec/dummy/app/views/users/edit.html.erb +6 -0
  51. data/spec/dummy/app/views/users/index.html.erb +268 -0
  52. data/spec/dummy/app/views/users/new.html.erb +5 -0
  53. data/spec/dummy/app/views/users/show.html.erb +30 -0
  54. data/spec/dummy/config.ru +4 -0
  55. data/spec/dummy/config/application.rb +62 -0
  56. data/spec/dummy/config/boot.rb +10 -0
  57. data/spec/dummy/config/database.yml +25 -0
  58. data/spec/dummy/config/environment.rb +5 -0
  59. data/spec/dummy/config/environments/development.rb +37 -0
  60. data/spec/dummy/config/environments/production.rb +67 -0
  61. data/spec/dummy/config/environments/test.rb +37 -0
  62. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  63. data/spec/dummy/config/initializers/gmaps4rails.rb +1 -0
  64. data/spec/dummy/config/initializers/inflections.rb +15 -0
  65. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  66. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  67. data/spec/dummy/config/initializers/session_store.rb +8 -0
  68. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  69. data/spec/dummy/config/locales/en.yml +5 -0
  70. data/spec/dummy/config/routes.rb +60 -0
  71. data/spec/dummy/db/migrate/20120408170155_create_users.rb +16 -0
  72. data/spec/dummy/db/schema.rb +29 -0
  73. data/spec/dummy/lib/assets/.gitkeep +0 -0
  74. data/spec/dummy/log/.gitkeep +0 -0
  75. data/spec/dummy/public/404.html +26 -0
  76. data/spec/dummy/public/422.html +26 -0
  77. data/spec/dummy/public/500.html +25 -0
  78. data/spec/dummy/public/favicon.ico +0 -0
  79. data/spec/dummy/public/javascripts/Player.js +22 -0
  80. data/spec/dummy/public/javascripts/Song.js +7 -0
  81. data/spec/dummy/public/logo.png +0 -0
  82. data/spec/dummy/script/rails +6 -0
  83. data/spec/factories/place_factory.rb +16 -0
  84. data/spec/factories/user_factory.rb +23 -0
  85. data/spec/fixtures/google_direction_valid.json +65 -0
  86. data/spec/fixtures/google_geocoding_toulon_france.json +58 -0
  87. data/spec/fixtures/google_places_valid.json +45 -0
  88. data/spec/fixtures/google_wrong_geocoding.json +4 -0
  89. data/spec/launchers/all_but_requests.rb +0 -0
  90. data/spec/launchers/all_specs.rb +0 -0
  91. data/spec/launchers/requests.rb +0 -0
  92. data/spec/lib/base_spec.rb +59 -0
  93. data/spec/lib/direction_spec.rb +53 -0
  94. data/spec/lib/geocoder_spec.rb +46 -0
  95. data/spec/lib/js_builder_spec.rb +134 -0
  96. data/spec/lib/json_builder_spec.rb +232 -0
  97. data/spec/lib/places_spec.rb +25 -0
  98. data/spec/models/place_spec.rb +39 -0
  99. data/spec/models/user_spec.rb +187 -0
  100. data/spec/spec_helper.rb +38 -0
  101. data/spec/support/geocoding.rb +40 -0
  102. data/spec/support/matchers.rb +73 -0
  103. data/todo +0 -0
  104. data/vendor/assets/javascripts/gmaps4rails/all.js +1 -0
  105. data/vendor/assets/javascripts/gmaps4rails/base.coffee +1 -0
  106. data/vendor/assets/javascripts/gmaps4rails/base/common.coffee +43 -0
  107. data/vendor/assets/javascripts/gmaps4rails/base/configuration.coffee +7 -0
  108. data/vendor/assets/javascripts/gmaps4rails/base/gmaps.coffee +13 -0
  109. data/vendor/assets/javascripts/gmaps4rails/base/interfaces/basic_interface.coffee +15 -0
  110. data/vendor/assets/javascripts/gmaps4rails/base/interfaces/controller_interface.coffee +15 -0
  111. data/vendor/assets/javascripts/gmaps4rails/base/interfaces/map_interface.coffee +27 -0
  112. data/vendor/assets/javascripts/gmaps4rails/base/interfaces/marker_interface.coffee +15 -0
  113. data/vendor/assets/javascripts/gmaps4rails/base/main.coffee +124 -0
  114. data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/circle_controller.coffee +29 -0
  115. data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/kml_controller.coffee +5 -0
  116. data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/marker_controller.coffee +76 -0
  117. data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/polygon_controller.coffee +29 -0
  118. data/vendor/assets/javascripts/gmaps4rails/base/main_controller_extensions/polyline_controller.coffee +29 -0
  119. data/vendor/assets/javascripts/gmaps4rails/base/objects/circle.coffee +12 -0
  120. data/vendor/assets/javascripts/gmaps4rails/base/objects/kml.coffee +8 -0
  121. data/vendor/assets/javascripts/gmaps4rails/base/objects/map.coffee +62 -0
  122. data/vendor/assets/javascripts/gmaps4rails/base/objects/marker.coffee +24 -0
  123. data/vendor/assets/javascripts/gmaps4rails/base/objects/polygon.coffee +11 -0
  124. data/vendor/assets/javascripts/gmaps4rails/base/objects/polyline.coffee +11 -0
  125. data/vendor/assets/javascripts/gmaps4rails/bing.coffee +1 -0
  126. data/vendor/assets/javascripts/gmaps4rails/bing/main.coffee +29 -0
  127. data/vendor/assets/javascripts/gmaps4rails/bing/objects/map.coffee +63 -0
  128. data/vendor/assets/javascripts/gmaps4rails/bing/objects/marker.coffee +78 -0
  129. data/vendor/assets/javascripts/gmaps4rails/bing/shared.coffee +20 -0
  130. data/vendor/assets/javascripts/gmaps4rails/google.coffee +1 -0
  131. data/vendor/assets/javascripts/gmaps4rails/google/main.coffee +47 -0
  132. data/vendor/assets/javascripts/gmaps4rails/google/objects/circle.coffee +38 -0
  133. data/vendor/assets/javascripts/gmaps4rails/google/objects/kml.coffee +16 -0
  134. data/vendor/assets/javascripts/gmaps4rails/google/objects/map.coffee +71 -0
  135. data/vendor/assets/javascripts/gmaps4rails/google/objects/marker.coffee +114 -0
  136. data/vendor/assets/javascripts/gmaps4rails/google/objects/polygon.coffee +38 -0
  137. data/vendor/assets/javascripts/gmaps4rails/google/objects/polyline.coffee +49 -0
  138. data/vendor/assets/javascripts/gmaps4rails/google/shared.coffee +27 -0
  139. data/vendor/assets/javascripts/gmaps4rails/openlayers.coffee +1 -0
  140. data/vendor/assets/javascripts/gmaps4rails/openlayers/main.coffee +138 -0
  141. data/vendor/assets/javascripts/gmaps4rails/openlayers/objects/map.coffee +49 -0
  142. data/vendor/assets/javascripts/gmaps4rails/openlayers/objects/marker.coffee +68 -0
  143. data/vendor/assets/javascripts/gmaps4rails/openlayers/objects/polyline.coffee +39 -0
  144. data/vendor/assets/javascripts/gmaps4rails/openlayers/shared.coffee +19 -0
  145. metadata +378 -14
  146. checksums.yaml +0 -7
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Direction" do
4
+
5
+ let(:geocoding) { File.read "spec/fixtures/google_direction_valid.json" }
6
+
7
+ context "valid request" do
8
+ before(:each) do
9
+ stub_request(:get, "http://maps.googleapis.com/maps/api/directions/json?destination=stubbed&language=en&origin=stubbed&sensor=false").
10
+ to_return(:status => 200, :body => geocoding, :headers => {})
11
+ end
12
+
13
+ it "should render info from only start_end args" do
14
+ result = Gmaps4rails.destination( {:from => "stubbed", :to => "stubbed"})
15
+ result.should eq([
16
+ {
17
+ "duration" => { "text" => "20 hours 40 mins", "value" => 74384.0 },
18
+ "distance" => { "text" => "1,328 mi", "value" => 2137146.0 },
19
+ "steps" => [
20
+ { "travel_mode" => "DRIVING",
21
+ "start_location" => { "lat" => 41.85073, "lng" => -87.65126 },
22
+ "end_location" => { "lat" => 41.85258, "lng" => -87.65141 },
23
+ "duration" => { "value" => 19, "text" => "1 min" },
24
+ "html_instructions" => "Head <b>north</b> on <b>S Morgan St</b> toward <b>W Cermak Rd</b>",
25
+ "distance" => { "value" => 207, "text" => "0.1 mi" }
26
+ }
27
+ ],
28
+ "polylines" =>"[{\"coded_array\":\"a~l~Fjk~uOwHJy@P\"}]"
29
+ }
30
+ ])
31
+ end
32
+
33
+ it "should clean output" do
34
+ result = Gmaps4rails.destination( {:from => "stubbed", :to => "stubbed"}, {}, "clean")
35
+ result.should eq([
36
+ {
37
+ "duration" => { "text" => "20 hours 40 mins", "value" => 74384.0 },
38
+ "distance" => { "text" => "1,328 mi", "value" => 2137146.0 },
39
+ "steps" => [
40
+ { "travel_mode" => "DRIVING",
41
+ "start_location" => { "lat" => 41.85073, "lng" => -87.65126 },
42
+ "end_location" => { "lat" => 41.85258, "lng" => -87.65141 },
43
+ "duration" => { "value" => 19, "text" => "1 min" },
44
+ "html_instructions" => "Head <b>north</b> on <b>S Morgan St</b> toward <b>W Cermak Rd</b>",
45
+ "distance" => { "value" => 207, "text" => "0.1 mi" }
46
+ }
47
+ ]
48
+ }
49
+ ])
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Geocode" do
4
+
5
+ context "data extraction from google's json" do
6
+
7
+ let(:result) { Gmaps4rails.geocode("stubbed").first }
8
+ let(:geocoding) { File.read "spec/fixtures/google_geocoding_toulon_france.json" }
9
+
10
+ before(:each) do
11
+ stub_request(:get, "http://maps.googleapis.com/maps/api/geocode/json?address=stubbed&language=en&sensor=false").
12
+ to_return(:status => 200, :body => geocoding, :headers => {})
13
+ end
14
+
15
+ it "should extract lat and lng" do
16
+ result[:lat].should eq 43.1242280
17
+ result[:lng].should eq 5.9280
18
+ end
19
+
20
+ it "should extract matched address" do
21
+ result[:matched_address].should eq "Toulon, France"
22
+ end
23
+
24
+ end
25
+
26
+ context "expected errors" do
27
+
28
+ it "should raise an error when geocoding has no answer" do
29
+ wrong_geocoding = File.read "spec/fixtures/google_wrong_geocoding.json"
30
+ stub_request(:get, "http://maps.googleapis.com/maps/api/geocode/json?address=stubbed&language=en&sensor=false").
31
+ to_return(:status => 200, :body => wrong_geocoding, :headers => {})
32
+
33
+ lambda { Gmaps4rails.geocode("stubbed")}.should raise_error Gmaps4rails::GeocodeStatus
34
+ end
35
+
36
+ it "should raise an error when google server fails" do
37
+ stub_request(:get, "http://maps.googleapis.com/maps/api/geocode/json?address=stubbed&language=en&sensor=false").
38
+ to_return(:status => 404, :body => "", :headers => {})
39
+
40
+ lambda { Gmaps4rails.geocode("stubbed")}.should raise_error Gmaps4rails::GeocodeNetStatus
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+
3
+ describe "to_gmaps4rails for hash" do
4
+ it "should accept hashes with indifferent access" do
5
+ hash1 = {:markers => {:data => @json, :options => {:do_clustering => true, :draggable => true } }}
6
+ hash2 = {"markers" => {"data" => @json, "options" => {"do_clustering" => true, "draggable" => true } }}
7
+ hash1.to_gmaps4rails.should eq hash2.to_gmaps4rails
8
+ end
9
+
10
+ context "rendered js" do
11
+ it "should format polygons" do
12
+ expected_polygons = '[[
13
+ {"longitude": -80.190262, "latitude": 25.774252},
14
+ {"longitude": -66.118292, "latitude": 18.466465},
15
+ {"longitude": -64.75737, "latitude": 32.321384}
16
+ ]]'
17
+ options_hash = {
18
+ :polygons => { :data => expected_polygons }
19
+ }
20
+ result = options_hash.to_gmaps4rails
21
+ actual_polygons = result.scan(/Gmaps\.map\.addPolygons\((.*?)\);/m).first.first
22
+
23
+ actual_polygons.should be_same_json_as expected_polygons
24
+ end
25
+
26
+ it "should format polylines" do
27
+ expected_polylines = '[[
28
+ {"longitude": -122.214897, "latitude": 37.772323},
29
+ {"longitude": -157.821856, "latitude": 21.291982},
30
+ {"longitude": 178.431, "latitude": -18.142599},
31
+ {"longitude": 153.027892, "latitude": -27.46758}
32
+ ],
33
+ [
34
+ {"longitude": -120.214897, "latitude": 30.772323, "strokeColor": "#000", "strokeWeight" : 2 },
35
+ {"longitude": -10.821856, "latitude": 50.291982}
36
+ ]]'
37
+ options_hash = {
38
+ :polylines => { :data => expected_polylines }
39
+ }
40
+ result = options_hash.to_gmaps4rails
41
+ actual_polylines = result.scan(/Gmaps\.map\.addPolylines\((.*?)\);/m).first.first
42
+
43
+ actual_polylines.should be_same_json_as expected_polylines
44
+ end
45
+
46
+ it "should format circles" do
47
+ expected_circles = '[
48
+ {"longitude": -122.214897, "latitude": 37.772323, "radius": 1000000},
49
+ {"longitude": 122.214897, "latitude": 37.772323, "radius": 1000000, "strokeColor": "#FF0000"}
50
+ ]'
51
+ options_hash = {
52
+ :circles => { :data => expected_circles }
53
+ }
54
+ result = options_hash.to_gmaps4rails
55
+ actual_circles = result.scan(/Gmaps\.map\.addCircles\((.*?)\);/m).first.first
56
+
57
+ actual_circles.should be_same_json_as expected_circles
58
+ end
59
+
60
+ it "should format markers" do
61
+ expected_markers = '[{ "description": "", "title": "", "longitude": "5.9311119", "latitude": "43.1251606", "picture": "", "width": "", "height": "" } ,{ "description": "", "title": "", "longitude": "2.3509871", "latitude": "48.8566667", "picture": "", "width": "", "height": "" } ]'
62
+ options_hash = {
63
+ :markers => { :data => expected_markers }
64
+ }
65
+ result = options_hash.to_gmaps4rails
66
+ actual_markers = result.scan(/Gmaps\.map\.addMarkers\((.*?)\);/m).first.first
67
+
68
+ actual_markers.should be_same_json_as expected_markers
69
+ end
70
+
71
+ it "should format map" do
72
+ options_hash = {
73
+ :map_options => { :type => "SATELLITE", :center_longitude => 180, "zoom" => 3, :raw => '{ok: true}'}
74
+ }
75
+ result = options_hash.to_gmaps4rails
76
+
77
+ result.should include "Gmaps.map = new Gmaps4RailsGoogle();"
78
+ result.should include "Gmaps.map.map_options.center_longitude = 180;"
79
+ result.should include "Gmaps.map.map_options.type = \"SATELLITE\""
80
+ result.should include "Gmaps.map.map_options.raw = {ok: true};"
81
+ result.should include "Gmaps.map.map_options.zoom = 3;"
82
+ result.should include "Gmaps.map.initialize();"
83
+ end
84
+
85
+ it "should invoke callback" do
86
+ result = {}.to_gmaps4rails
87
+ result.should include "Gmaps.map.callback();"
88
+ end
89
+
90
+ it "should format kml" do
91
+ expected_kml = '[{ url: "http://www.searcharoo.net/SearchKml/newyork.kml"}, { url: "http://gmaps-samples.googlecode.com/svn/trunk/ggeoxml/cta.kml", options: {clickable: false } }]'
92
+ options_hash = {
93
+ :kml => {:data => expected_kml }
94
+ }
95
+ result = options_hash.to_gmaps4rails
96
+ actual_kml = result.scan(/Gmaps\.map\.addKml\((.*?)\);/m).first.first
97
+
98
+ actual_kml.should eq expected_kml
99
+ end
100
+
101
+ it "should format directions" do
102
+ options_hash = {
103
+ "direction" => {
104
+ "data" => { "from" => "toulon, france", "to" => "paris, france"} ,
105
+ "options" => {"waypoints" => ["toulouse, france", "brest, france"], "travelMode" => "DRIVING", "display_panel" => true, "panel_id" => "instructions"}
106
+ }
107
+ }
108
+ result = options_hash.to_gmaps4rails
109
+ result.should include "Gmaps.map.direction_conf.origin = 'toulon, france';\nGmaps.map.direction_conf.destination = 'paris, france';"
110
+ result.should include "Gmaps.map.direction_conf.display_panel = true;"
111
+ result.should include "Gmaps.map.direction_conf.panel_id = \"instructions\";"
112
+ result.should include "Gmaps.map.direction_conf.travelMode = \"DRIVING\";"
113
+ result.should include "Gmaps.map.create_direction();"
114
+
115
+ actual_directions = result.scan(/Gmaps\.map\.direction_conf\.waypoints = (.*?);/m).first.first
116
+ actual_directions.should be_same_json_as "[{\"stopover\":true,\"location\":\"toulouse, france\"},{\"stopover\":true,\"location\":\"brest, france\"}]"
117
+ end
118
+
119
+ it "should not call map builder if not last_map" do
120
+ hash = {:last_map => false}
121
+ hash.to_gmaps4rails.should_not include "window.onload"
122
+ end
123
+
124
+ it "should call map builder if last_map" do
125
+ trigger_script = "Gmaps.oldOnload = window.onload;\n window.onload = function() { Gmaps.triggerOldOnload(); Gmaps.loadMaps(); };"
126
+ hash = {:last_map => true}
127
+ hash.to_gmaps4rails.should include trigger_script
128
+ hash = {}
129
+ hash.to_gmaps4rails.should include trigger_script
130
+ end
131
+
132
+ end
133
+
134
+ end
@@ -0,0 +1,232 @@
1
+ require 'spec_helper'
2
+
3
+ include Geocoding
4
+
5
+ set_gmaps4rails_options!
6
+
7
+ describe "Json Builder" do
8
+
9
+ before(:each) do
10
+ Geocoding.stub_geocoding
11
+ end
12
+
13
+ it "should render a valid json even if there is no instance in the db" do
14
+ User.all.to_gmaps4rails.should == "[]"
15
+ end
16
+
17
+ context "standard configuration, valid user" do
18
+ let!(:user) { Factory(:user) }
19
+
20
+ it "should render a valid json from an array of objects" do
21
+ Factory(:user_paris)
22
+ JSON.parse(User.all.to_gmaps4rails).should == [{ "lng" => TOULON[:longitude], "lat" => TOULON[:latitude] },{"lng" => PARIS[:longitude], "lat" => PARIS[:latitude] } ]
23
+ end
24
+
25
+ it "should render a valid json from a single object" do
26
+ JSON.parse(user.to_gmaps4rails).should == [{"lng" => TOULON[:longitude], "lat" => TOULON[:latitude] }]
27
+ end
28
+
29
+ describe "to_gmaps4rails block" do
30
+ it "should extend json string for Arrays" do
31
+ Factory(:user_paris)
32
+ JSON.parse(User.all.to_gmaps4rails do |u, marker|
33
+ '"model":"' + u.class.to_s + '"'
34
+ end).should == [{"model" => "User", "lng" => TOULON[:longitude], "lat" => TOULON[:latitude]},{"model" => "User", "lng" => PARIS[:longitude], "lat" => PARIS[:latitude] }]
35
+ end
36
+
37
+ it "should extend json string for Arrays and custom hash" do
38
+ Factory(:user_paris)
39
+ JSON.parse(User.all.to_gmaps4rails do |u, marker|
40
+ marker.json({ :model => u.class.to_s })
41
+ end).should == [{"model" => "User", "lng" => TOULON[:longitude], "lat" => TOULON[:latitude]},{"model" => "User", "lng" => PARIS[:longitude], "lat" => PARIS[:latitude] }]
42
+ end
43
+
44
+ it "should extend json string for a single object" do
45
+ JSON.parse(user.to_gmaps4rails do |u, marker|
46
+ "\"model\":\"" + u.class.to_s + "\""
47
+ end).should == [{ "model" => "User", "lng" =>TOULON[:longitude], "lat" => TOULON[:latitude] }]
48
+ end
49
+
50
+ it "json method should produce same result as raw string" do
51
+ from_method = JSON.parse(user.to_gmaps4rails do |u, marker|
52
+ marker.json({ :model => u.class.to_s })
53
+ end)
54
+
55
+ from_string = JSON.parse(user.to_gmaps4rails do |u, marker|
56
+ "\"model\":\"" + u.class.to_s + "\""
57
+ end)
58
+
59
+ from_string.should eq from_method
60
+ end
61
+
62
+ it "infowindow content should be included in json" do
63
+ user.to_gmaps4rails do |u, marker|
64
+ marker.infowindow "in infowindow"
65
+ end.should include "\"description\":\"in infowindow\""
66
+ end
67
+
68
+ it "marker_picture should be included in json" do
69
+ user.to_gmaps4rails do |u, marker|
70
+ marker.picture({
71
+ :picture => "http://www.blankdots.com/img/github-32x32.png",
72
+ :width => "32",
73
+ :height => "32"
74
+ })
75
+ end.should include "\"picture\":\"http://www.blankdots.com/img/github-32x32.png\""
76
+ end
77
+
78
+ it "title content should be included in json" do
79
+ user.to_gmaps4rails do |u, marker|
80
+ marker.title "i'm the title"
81
+ end.should include "\"title\":\"i'm the title\""
82
+ end
83
+
84
+ it "sidebar content should be included in json" do
85
+ user.to_gmaps4rails do |u, marker|
86
+ marker.sidebar "i'm the sidebar"
87
+ end.should include "\"sidebar\":\"i'm the sidebar\""
88
+ end
89
+ end
90
+
91
+ end
92
+
93
+ context "model customization" do
94
+ let(:user) { Factory(:user) }
95
+
96
+ context "instance methods" do
97
+ let(:user_with_pic) { Factory(:user_with_pic) }
98
+
99
+ it "should take into account the description provided in the model" do
100
+ user_with_pic.instance_eval do
101
+ def gmaps4rails_infowindow
102
+ "My Beautiful Picture: #{picture}"
103
+ end
104
+ end
105
+ user_with_pic.to_gmaps4rails.should include "\"description\":\"My Beautiful Picture: http://www.blankdots.com/img/github-32x32.png\""
106
+ end
107
+
108
+ it "should take into account the picture provided in the model" do
109
+ user.instance_eval do
110
+ def gmaps4rails_marker_picture
111
+ {
112
+ "picture" => "http://www.blankdots.com/img/github-32x32.png",
113
+ "width" => "32",
114
+ "height" => "32"
115
+ }
116
+ end
117
+ end
118
+ result = user.to_gmaps4rails
119
+ result.should include "\"picture\":\"http://www.blankdots.com/img/github-32x32.png\""
120
+ result.should include "\"width\":\"32\""
121
+ result.should include "\"height\":\"32\""
122
+ end
123
+
124
+ it "should take into account the picture and shadow provided in the model" do
125
+ user.instance_eval do
126
+ def gmaps4rails_marker_picture
127
+ {
128
+ "picture" => "http://www.blankdots.com/img/github-32x32.png",
129
+ "width" => "32",
130
+ "height" => "32",
131
+ "marker_anchor" => [10, 20],
132
+ "shadow_picture" => "http://code.google.com/apis/maps/documentation/javascript/examples/images/beachflag_shadow.png" ,
133
+ "shadow_width" => "40",
134
+ "shadow_height" => "40",
135
+ "shadow_anchor" => [5, 10]
136
+ }
137
+ end
138
+ end
139
+ result = user.to_gmaps4rails
140
+ result.should include "\"shadow_width\":\"40\""
141
+ result.should include "\"shadow_height\":\"40\""
142
+ result.should include "\"shadow_picture\":\"http://code.google.com/apis/maps/documentation/javascript/examples/images/beachflag_shadow.png\""
143
+ result.should include "\"shadow_anchor\":[5,10]"
144
+ result.should include "\"marker_anchor\":[10,20]"
145
+ end
146
+
147
+ it "should take into account the title provided in the model" do
148
+ user.instance_eval do
149
+ def gmaps4rails_title
150
+ "Sweet Title"
151
+ end
152
+ end
153
+ JSON.parse(user.to_gmaps4rails).should == [{"title" => "Sweet Title", "lng" => TOULON[:longitude], "lat" => TOULON[:latitude]}]
154
+ end
155
+
156
+ it "should take into account the sidebar content provided in the model" do
157
+ user.instance_eval do
158
+ def gmaps4rails_sidebar
159
+ "sidebar content"
160
+ end
161
+ end
162
+ user.to_gmaps4rails.should include "\"sidebar\":\"sidebar content\""
163
+ end
164
+
165
+ it "should take into account all additional data provided in the model" do
166
+ user.instance_eval do
167
+ def gmaps4rails_infowindow
168
+ "My Beautiful Picture: #{picture}"
169
+ end
170
+
171
+ def gmaps4rails_marker_picture
172
+ {
173
+ "picture" => "http://www.blankdots.com/img/github-32x32.png",
174
+ "width" => "32",
175
+ "height" => "32"
176
+ }
177
+ end
178
+
179
+ def gmaps4rails_title
180
+ "Sweet Title"
181
+ end
182
+
183
+ def gmaps4rails_sidebar
184
+ "sidebar content"
185
+ end
186
+ end
187
+ result = user.to_gmaps4rails
188
+ result.should include "\"description\":\"My Beautiful Picture: \""
189
+ result.should include "\"title\":\"Sweet Title\""
190
+ result.should include "\"sidebar\":\"sidebar content\""
191
+ result.should include "\"picture\":\"http://www.blankdots.com/img/github-32x32.png\""
192
+ end
193
+
194
+ end
195
+
196
+ it "block info should take precedence over model methods" do
197
+ user.instance_eval do
198
+ def gmaps4rails_infowindow
199
+ "defined in model"
200
+ end
201
+ end
202
+ user.to_gmaps4rails.should include "defined in model"
203
+ result = user.to_gmaps4rails do |u, marker|
204
+ marker.infowindow "defined in block"
205
+ end
206
+ result.should include "defined in block"
207
+ result.should_not include "defined in model"
208
+ end
209
+
210
+ it "block info should take precedence over model methods, particular case: picture" do
211
+ user.instance_eval do
212
+ def gmaps4rails_marker_picture
213
+ {
214
+ "picture" => "/model.png",
215
+ "width" => 32,
216
+ "height" => 32}
217
+ end
218
+ end
219
+ user.to_gmaps4rails.should include "model.png"
220
+ result = user.to_gmaps4rails do |u, marker|
221
+ marker.picture({
222
+ "picture" => "/block.png",
223
+ "width" => 32,
224
+ "height" => 32})
225
+ end
226
+ result.should include "block.png"
227
+ result.should_not include "model.png"
228
+ end
229
+
230
+ end
231
+
232
+ end