gmaps4rails 1.4.8 → 1.5.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/.gitignore +11 -0
  2. data/.travis.yml +10 -0
  3. data/Gemfile +9 -0
  4. data/Gemfile.lock +175 -0
  5. data/Guardfile +38 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.rdoc +26 -37
  8. data/Rakefile +4 -0
  9. data/app/assets/javascripts/gmaps4rails/gmaps4rails.base.js.coffee +40 -34
  10. data/app/assets/javascripts/gmaps4rails/gmaps4rails.googlemaps.js.coffee +4 -2
  11. data/app/views/gmaps4rails/_gmaps4rails.html.erb +13 -12
  12. data/gmaps4rails.gemspec +38 -0
  13. data/lib/gmaps4rails.rb +0 -8
  14. data/lib/gmaps4rails/acts_as_gmappable.rb +55 -33
  15. data/lib/gmaps4rails/base.rb +7 -12
  16. data/lib/gmaps4rails/helper/gmaps4rails_helper.rb +16 -42
  17. data/lib/gmaps4rails/js_builder.rb +121 -0
  18. data/lib/gmaps4rails/json_builder.rb +127 -0
  19. data/lib/gmaps4rails/version.rb +3 -0
  20. data/lib/gmaps4rails/view_helper.rb +171 -0
  21. data/lib/tasks/gmaps4rails_tasks.rake +4 -0
  22. data/lib/tasks/jasmine.rake +8 -0
  23. data/public/javascripts/gmaps4rails/gmaps4rails.base.js +81 -30
  24. data/public/javascripts/gmaps4rails/gmaps4rails.bing.js +34 -11
  25. data/public/javascripts/gmaps4rails/gmaps4rails.googlemaps.js +45 -21
  26. data/public/javascripts/gmaps4rails/gmaps4rails.mapquest.js +33 -14
  27. data/public/javascripts/gmaps4rails/gmaps4rails.openlayers.js +33 -20
  28. data/spec/base/base_spec.rb +185 -0
  29. data/spec/base/geocoding_spec.rb +17 -0
  30. data/spec/dummy/.gitignore +2 -0
  31. data/spec/dummy/.rspec +1 -0
  32. data/spec/dummy/README.rdoc +261 -0
  33. data/spec/dummy/Rakefile +7 -0
  34. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  35. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  36. data/spec/dummy/app/assets/stylesheets/gmaps4rails.css +24 -0
  37. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  38. data/spec/dummy/app/controllers/users_controller.rb +96 -0
  39. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  40. data/spec/dummy/app/mailers/.gitkeep +0 -0
  41. data/spec/dummy/app/models/.gitkeep +0 -0
  42. data/spec/dummy/app/models/user.rb +9 -0
  43. data/spec/dummy/app/views/layouts/application.html.erb +16 -0
  44. data/spec/dummy/app/views/users/_form.html.erb +37 -0
  45. data/spec/dummy/app/views/users/_my_template.html.erb +2 -0
  46. data/spec/dummy/app/views/users/_my_template2.html.erb +1 -0
  47. data/spec/dummy/app/views/users/ajax_data.js.erb +2 -0
  48. data/spec/dummy/app/views/users/ajax_test.html.erb +22 -0
  49. data/spec/dummy/app/views/users/edit.html.erb +6 -0
  50. data/spec/dummy/app/views/users/index.html.erb +238 -0
  51. data/spec/dummy/app/views/users/new.html.erb +5 -0
  52. data/spec/dummy/app/views/users/show.html.erb +30 -0
  53. data/spec/dummy/config.ru +4 -0
  54. data/spec/dummy/config/application.rb +62 -0
  55. data/spec/dummy/config/boot.rb +10 -0
  56. data/spec/dummy/config/database.yml +25 -0
  57. data/spec/dummy/config/environment.rb +5 -0
  58. data/spec/dummy/config/environments/development.rb +37 -0
  59. data/spec/dummy/config/environments/production.rb +67 -0
  60. data/spec/dummy/config/environments/test.rb +37 -0
  61. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  62. data/spec/dummy/config/initializers/inflections.rb +15 -0
  63. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  64. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  65. data/spec/dummy/config/initializers/session_store.rb +8 -0
  66. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  67. data/spec/dummy/config/locales/en.yml +5 -0
  68. data/spec/dummy/config/routes.rb +60 -0
  69. data/spec/dummy/db/migrate/20120408170155_create_users.rb +16 -0
  70. data/spec/dummy/db/schema.rb +29 -0
  71. data/spec/dummy/lib/assets/.gitkeep +0 -0
  72. data/spec/dummy/log/.gitkeep +0 -0
  73. data/spec/dummy/public/404.html +26 -0
  74. data/spec/dummy/public/422.html +26 -0
  75. data/spec/dummy/public/500.html +25 -0
  76. data/spec/dummy/public/favicon.ico +0 -0
  77. data/spec/dummy/public/javascripts/Player.js +22 -0
  78. data/spec/dummy/public/javascripts/Song.js +7 -0
  79. data/spec/dummy/script/rails +6 -0
  80. data/spec/factories/user_factory.rb +23 -0
  81. data/spec/javascripts/basic_methods_spec.js +103 -0
  82. data/spec/javascripts/helpers/.gitkeep +0 -0
  83. data/spec/javascripts/helpers/SpecHelper.js +87 -0
  84. data/spec/javascripts/map_spec.js +49 -0
  85. data/spec/javascripts/markers_methods_spec.js +368 -0
  86. data/spec/javascripts/mocks_spec.js +27 -0
  87. data/spec/javascripts/support/jasmine.yml +76 -0
  88. data/spec/javascripts/support/jasmine_config.rb +23 -0
  89. data/spec/javascripts/support/jasmine_runner.rb +20 -0
  90. data/spec/launchers/all_but_requests.rb +0 -0
  91. data/spec/launchers/all_specs.rb +0 -0
  92. data/spec/launchers/requests.rb +0 -0
  93. data/spec/models/user_spec.rb +447 -0
  94. data/spec/spec_helper.rb +44 -0
  95. data/spec/support/geocoding.rb +14 -0
  96. data/spec/support/matchers.rb +43 -0
  97. metadata +364 -31
  98. data/LICENSE.txt +0 -21
  99. data/app/assets/javascripts/gmaps4rails/all_apis.js +0 -5
  100. data/app/assets/javascripts/gmaps4rails/bing.js +0 -2
  101. data/app/assets/javascripts/gmaps4rails/googlemaps.js +0 -2
  102. data/app/assets/javascripts/gmaps4rails/mapquest.js +0 -2
  103. data/app/assets/javascripts/gmaps4rails/openlayers.js +0 -2
  104. data/app/views/gmaps4rails/_html.html.erb +0 -12
  105. data/app/views/gmaps4rails/_scripts.html.erb +0 -37
  106. data/lib/gmaps4rails/js_handler.rb +0 -112
  107. data/lib/gmaps4rails/json_handler.rb +0 -116
  108. data/public/images/marker.png +0 -0
@@ -1,14 +1,11 @@
1
1
  (function() {
2
- var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
3
- for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
4
- function ctor() { this.constructor = child; }
5
- ctor.prototype = parent.prototype;
6
- child.prototype = new ctor;
7
- child.__super__ = parent.prototype;
8
- return child;
9
- };
10
- this.Gmaps4RailsOpenlayers = (function() {
11
- __extends(Gmaps4RailsOpenlayers, Gmaps4Rails);
2
+ var __hasProp = Object.prototype.hasOwnProperty,
3
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
4
+
5
+ this.Gmaps4RailsOpenlayers = (function(_super) {
6
+
7
+ __extends(Gmaps4RailsOpenlayers, _super);
8
+
12
9
  function Gmaps4RailsOpenlayers() {
13
10
  Gmaps4RailsOpenlayers.__super__.constructor.apply(this, arguments);
14
11
  this.map_options = {};
@@ -19,22 +16,26 @@
19
16
  this.markersLayer = null;
20
17
  this.markersControl = null;
21
18
  }
19
+
22
20
  Gmaps4RailsOpenlayers.prototype.createPoint = function(lat, lng) {};
21
+
23
22
  Gmaps4RailsOpenlayers.prototype.createLatLng = function(lat, lng) {
24
23
  return new OpenLayers.LonLat(lng, lat).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
25
24
  };
25
+
26
26
  Gmaps4RailsOpenlayers.prototype.createAnchor = function(offset) {
27
- if (offset === null) {
28
- return null;
29
- }
27
+ if (offset === null) return null;
30
28
  return new OpenLayers.Pixel(offset[0], offset[1]);
31
29
  };
30
+
32
31
  Gmaps4RailsOpenlayers.prototype.createSize = function(width, height) {
33
32
  return new OpenLayers.Size(width, height);
34
33
  };
34
+
35
35
  Gmaps4RailsOpenlayers.prototype.createLatLngBounds = function() {
36
36
  return new OpenLayers.Bounds();
37
37
  };
38
+
38
39
  Gmaps4RailsOpenlayers.prototype.createMap = function() {
39
40
  var map;
40
41
  map = new OpenLayers.Map(this.map_options.id);
@@ -42,6 +43,7 @@
42
43
  map.setCenter(this.createLatLng(this.map_options.center_latitude, this.map_options.center_longitude), this.map_options.zoom);
43
44
  return map;
44
45
  };
46
+
45
47
  Gmaps4RailsOpenlayers.prototype.createMarker = function(args) {
46
48
  var marker, style_mark;
47
49
  style_mark = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
@@ -82,16 +84,19 @@
82
84
  this.markersLayer.addFeatures([marker]);
83
85
  return marker;
84
86
  };
87
+
85
88
  Gmaps4RailsOpenlayers.prototype.clearMarkers = function() {
86
89
  this.clearMarkersLayerIfExists();
87
90
  this.markersLayer = null;
88
91
  return this.boundsObject = new OpenLayers.Bounds();
89
92
  };
93
+
90
94
  Gmaps4RailsOpenlayers.prototype.clearMarkersLayerIfExists = function() {
91
95
  if (this.markersLayer !== null && this.map.getLayer(this.markersLayer.id) !== null) {
92
96
  return this.map.removeLayer(this.markersLayer);
93
97
  }
94
98
  };
99
+
95
100
  Gmaps4RailsOpenlayers.prototype.extendBoundsWithMarkers = function() {
96
101
  var marker, _i, _len, _ref, _results;
97
102
  _ref = this.markers;
@@ -102,6 +107,7 @@
102
107
  }
103
108
  return _results;
104
109
  };
110
+
105
111
  Gmaps4RailsOpenlayers.prototype.createClusterer = function(markers_array) {
106
112
  var clusters, funcs, options, strategy, style;
107
113
  options = {
@@ -123,9 +129,7 @@
123
129
  radius: function(feature) {
124
130
  var pix;
125
131
  pix = 2;
126
- if (feature.cluster) {
127
- pix = Math.min(feature.attributes.count, 7) + 2;
128
- }
132
+ if (feature.cluster) pix = Math.min(feature.attributes.count, 7) + 2;
129
133
  return pix;
130
134
  }
131
135
  }
@@ -147,12 +151,11 @@
147
151
  clusters.addFeatures(markers_array);
148
152
  return clusters;
149
153
  };
154
+
150
155
  Gmaps4RailsOpenlayers.prototype.clusterize = function() {
151
156
  var marker, markers_array, _i, _len, _ref;
152
157
  if (this.markers_conf.do_clustering === true) {
153
- if (this.markerClusterer !== null) {
154
- this.clearClusterer();
155
- }
158
+ if (this.markerClusterer !== null) this.clearClusterer();
156
159
  markers_array = new Array;
157
160
  _ref = this.markers;
158
161
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@@ -162,17 +165,21 @@
162
165
  return this.markerClusterer = this.createClusterer(markers_array);
163
166
  }
164
167
  };
168
+
165
169
  Gmaps4RailsOpenlayers.prototype.clearClusterer = function() {
166
170
  return this.map.removeLayer(this.markerClusterer);
167
171
  };
172
+
168
173
  Gmaps4RailsOpenlayers.prototype.createInfoWindow = function(marker_container) {
169
174
  if (marker_container.description != null) {
170
175
  return marker_container.serviceObject.infoWindow = marker_container.description;
171
176
  }
172
177
  };
178
+
173
179
  Gmaps4RailsOpenlayers.prototype.onPopupClose = function(evt) {
174
180
  return this.markersControl.unselect(this.feature);
175
181
  };
182
+
176
183
  Gmaps4RailsOpenlayers.prototype.onFeatureSelect = function(evt) {
177
184
  var feature, popup;
178
185
  feature = evt.feature;
@@ -181,6 +188,7 @@
181
188
  popup.feature = feature;
182
189
  return this.map.addPopup(popup);
183
190
  };
191
+
184
192
  Gmaps4RailsOpenlayers.prototype.onFeatureUnselect = function(evt) {
185
193
  var feature;
186
194
  feature = evt.feature;
@@ -190,12 +198,17 @@
190
198
  return feature.popup = null;
191
199
  }
192
200
  };
201
+
193
202
  Gmaps4RailsOpenlayers.prototype.fitBounds = function() {
194
203
  return this.map.zoomToExtent(this.boundsObject, true);
195
204
  };
205
+
196
206
  Gmaps4RailsOpenlayers.prototype.centerMapOnUser = function() {
197
207
  return this.map.setCenter(this.userLocation);
198
208
  };
209
+
199
210
  return Gmaps4RailsOpenlayers;
200
- })();
211
+
212
+ })(Gmaps4Rails);
213
+
201
214
  }).call(this);
@@ -0,0 +1,185 @@
1
+ require 'spec_helper'
2
+
3
+ describe "JS building methods" do
4
+
5
+ subject { ::Gmaps4rails::JsBuilder.new }
6
+
7
+ describe "constructor name retrieval" do
8
+ it "should render google if nothing passed" do
9
+ subject.get_constructor(nil).should eq "Gmaps4RailsGoogle()"
10
+ end
11
+
12
+ it "should render proper provider when provided" do
13
+ options_hash = { "provider" => "bing" }
14
+ subject.get_constructor(options_hash.with_indifferent_access).should eq "Gmaps4RailsBing()"
15
+
16
+ options_hash = { "provider" => "mapquest" }
17
+ subject.get_constructor(options_hash.with_indifferent_access).should eq "Gmaps4RailsMapquest()"
18
+
19
+ options_hash = { "provider" => "openlayers" }
20
+ subject.get_constructor(options_hash.with_indifferent_access).should eq "Gmaps4RailsOpenlayers()"
21
+ end
22
+ end
23
+
24
+ describe "map ID retrieval" do
25
+ it "should return the default ID when nothing is passed" do
26
+ options_hash = nil
27
+ subject.get_map_id(options_hash).should eq subject.class::DEFAULT_MAP_ID
28
+ end
29
+
30
+ it "should return the default ID when no id is passed within map_options" do
31
+ options_hash = { "foo" => "bar" }
32
+ subject.get_map_id(options_hash).should eq subject.class::DEFAULT_MAP_ID
33
+ end
34
+
35
+ it "should return the proper ID when id passed" do
36
+ options_hash = { "id" => "foo" }
37
+ subject.get_map_id(options_hash.with_indifferent_access).should eq "foo"
38
+ end
39
+
40
+ end
41
+ end
42
+
43
+ describe "to_gmaps4rails for hash" do
44
+ it "should accept hashes with indifferent access" do
45
+ hash1 = {:markers => {:data => @json, :options => {:do_clustering => true, :draggable => true } }}
46
+ hash2 = {"markers" => {"data" => @json, "options" => {"do_clustering" => true, "draggable" => true } }}
47
+ hash1.to_gmaps4rails.should eq hash2.to_gmaps4rails
48
+ end
49
+
50
+ context "rendered js" do
51
+ it "should format polygons" do
52
+ expected_polygons = '[[
53
+ {"longitude": -80.190262, "latitude": 25.774252},
54
+ {"longitude": -66.118292, "latitude": 18.466465},
55
+ {"longitude": -64.75737, "latitude": 32.321384}
56
+ ]]'
57
+ options_hash = {
58
+ :polygons => { :data => expected_polygons }
59
+ }
60
+ result = options_hash.to_gmaps4rails
61
+ actual_polygons = result.scan(/Gmaps\.map\.polygons = (.*?);/m).first.first
62
+
63
+ actual_polygons.should be_same_json_as expected_polygons
64
+ result.should include "Gmaps.map.create_polygons();"
65
+ end
66
+
67
+ it "should format polylines" do
68
+ expected_polylines = '[[
69
+ {"longitude": -122.214897, "latitude": 37.772323},
70
+ {"longitude": -157.821856, "latitude": 21.291982},
71
+ {"longitude": 178.431, "latitude": -18.142599},
72
+ {"longitude": 153.027892, "latitude": -27.46758}
73
+ ],
74
+ [
75
+ {"longitude": -120.214897, "latitude": 30.772323, "strokeColor": "#000", "strokeWeight" : 2 },
76
+ {"longitude": -10.821856, "latitude": 50.291982}
77
+ ]]'
78
+ options_hash = {
79
+ :polylines => { :data => expected_polylines }
80
+ }
81
+ result = options_hash.to_gmaps4rails
82
+ actual_polylines = result.scan(/Gmaps\.map\.polylines = (.*?);/m).first.first
83
+
84
+ actual_polylines.should be_same_json_as expected_polylines
85
+ result.should include "Gmaps.map.create_polylines();"
86
+ end
87
+
88
+ it "should format circles" do
89
+ expected_circles = '[
90
+ {"longitude": -122.214897, "latitude": 37.772323, "radius": 1000000},
91
+ {"longitude": 122.214897, "latitude": 37.772323, "radius": 1000000, "strokeColor": "#FF0000"}
92
+ ]'
93
+ options_hash = {
94
+ :circles => { :data => expected_circles }
95
+ }
96
+ result = options_hash.to_gmaps4rails
97
+ actual_circles = result.scan(/Gmaps\.map\.circles = (.*?);/m).first.first
98
+
99
+ actual_circles.should be_same_json_as expected_circles
100
+ result.should include "Gmaps.map.create_circles();"
101
+ end
102
+
103
+ it "should format markers" do
104
+ expected_markers = '[{ "description": "", "title": "", "longitude": "5.9311119", "latitude": "43.1251606", "picture": "", "width": "", "height": "" } ,{ "description": "", "title": "", "longitude": "2.3509871", "latitude": "48.8566667", "picture": "", "width": "", "height": "" } ]'
105
+ options_hash = {
106
+ :markers => { :data => expected_markers }
107
+ }
108
+ result = options_hash.to_gmaps4rails
109
+ actual_markers = result.scan(/Gmaps\.map\.markers = (.*?);/m).first.first
110
+
111
+ actual_markers.should be_same_json_as expected_markers
112
+ result.should include "Gmaps.map.create_markers();"
113
+ end
114
+
115
+ it "should format map" do
116
+ options_hash = {
117
+ :map_options => { :type => "SATELLITE", :center_longitude => 180, "zoom" => 3, :raw => '{ok: true}'}
118
+ }
119
+ result = options_hash.to_gmaps4rails
120
+
121
+ result.should include "Gmaps.map = new Gmaps4RailsGoogle();"
122
+ result.should include "Gmaps.map.map_options.center_longitude = 180;"
123
+ result.should include "Gmaps.map.map_options.type = \"SATELLITE\""
124
+ result.should include "Gmaps.map.map_options.raw = {ok: true};"
125
+ result.should include "Gmaps.map.map_options.zoom = 3;"
126
+ result.should include "Gmaps.map.initialize();"
127
+ end
128
+
129
+ it "should invoke callback" do
130
+ result = {}.to_gmaps4rails
131
+ result.should include "Gmaps.map.callback();"
132
+ end
133
+
134
+ it "should format kml" do
135
+ expected_kml = '[{ url: "http://www.searcharoo.net/SearchKml/newyork.kml"}, { url: "http://gmaps-samples.googlecode.com/svn/trunk/ggeoxml/cta.kml", options: {clickable: false } }]'
136
+ options_hash = {
137
+ :kml => {:data => expected_kml }
138
+ }
139
+ result = options_hash.to_gmaps4rails
140
+ actual_kml = result.scan(/Gmaps\.map\.kml = (.*?);/m).first.first
141
+
142
+ actual_kml.should eq expected_kml
143
+ result.should include "Gmaps.map.create_kml();"
144
+ end
145
+
146
+ it "should format directions" do
147
+ options_hash = {
148
+ "direction" => {
149
+ "data" => { "from" => "toulon, france", "to" => "paris, france"} ,
150
+ "options" => {"waypoints" => ["toulouse, france", "brest, france"], "travelMode" => "DRIVING", "display_panel" => true, "panel_id" => "instructions"}
151
+ }
152
+ }
153
+ result = options_hash.to_gmaps4rails
154
+ result.should include "Gmaps.map.direction_conf.origin = 'toulon, france';\nGmaps.map.direction_conf.destination = 'paris, france';"
155
+ result.should include "Gmaps.map.direction_conf.display_panel = true;"
156
+ result.should include "Gmaps.map.direction_conf.panel_id = \"instructions\";"
157
+ result.should include "Gmaps.map.direction_conf.travelMode = \"DRIVING\";"
158
+ result.should include "Gmaps.map.create_direction();"
159
+
160
+ actual_directions = result.scan(/Gmaps\.map\.direction_conf\.waypoints = (.*?);/m).first.first
161
+ actual_directions.should be_same_json_as "[{\"stopover\":true,\"location\":\"toulouse, france\"},{\"stopover\":true,\"location\":\"brest, france\"}]"
162
+ end
163
+
164
+ it "should not call map builder if not last_map" do
165
+ hash = {:last_map => false}
166
+ hash.to_gmaps4rails.should_not include "window.onload = Gmaps.loadMaps;"
167
+ end
168
+
169
+ it "should call map builder if last_map" do
170
+ trigger_script = "Gmaps.oldOnload = window.onload;\n window.onload = function() { Gmaps.triggerOldOnload(); Gmaps.loadMaps(); };"
171
+ hash = {:last_map => true}
172
+ hash.to_gmaps4rails.should include trigger_script
173
+ hash = {}
174
+ hash.to_gmaps4rails.should include trigger_script
175
+ end
176
+
177
+ end
178
+
179
+ end
180
+
181
+ describe "Destination" do
182
+ it "should render info from only start_end args"
183
+ it "should accept all options properly"
184
+ it "should format output in accordance with 'output' variable"
185
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Geocode" do
4
+
5
+ it "should raise an error when address invalid" do
6
+ lambda { Gmaps4rails.geocode("home")}.should raise_error Gmaps4rails::GeocodeStatus
7
+ end
8
+
9
+ it "should return results in the specified language" do
10
+ result = Gmaps4rails.geocode("Toulon, France", "de")
11
+ result.first[:matched_address].should eq "Toulon, Frankreich"
12
+ end
13
+
14
+ it "should raise an error when net connection failed" #TODO: Damn, I don't know how to test that!
15
+
16
+ end
17
+
@@ -0,0 +1,2 @@
1
+ tmp
2
+ db/*.sqlite3
data/spec/dummy/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,261 @@
1
+ == Welcome to Rails
2
+
3
+ Rails is a web-application framework that includes everything needed to create
4
+ database-backed web applications according to the Model-View-Control pattern.
5
+
6
+ This pattern splits the view (also called the presentation) into "dumb"
7
+ templates that are primarily responsible for inserting pre-built data in between
8
+ HTML tags. The model contains the "smart" domain objects (such as Account,
9
+ Product, Person, Post) that holds all the business logic and knows how to
10
+ persist themselves to a database. The controller handles the incoming requests
11
+ (such as Save New Account, Update Product, Show Post) by manipulating the model
12
+ and directing data to the view.
13
+
14
+ In Rails, the model is handled by what's called an object-relational mapping
15
+ layer entitled Active Record. This layer allows you to present the data from
16
+ database rows as objects and embellish these data objects with business logic
17
+ methods. You can read more about Active Record in
18
+ link:files/vendor/rails/activerecord/README.html.
19
+
20
+ The controller and view are handled by the Action Pack, which handles both
21
+ layers by its two parts: Action View and Action Controller. These two layers
22
+ are bundled in a single package due to their heavy interdependence. This is
23
+ unlike the relationship between the Active Record and Action Pack that is much
24
+ more separate. Each of these packages can be used independently outside of
25
+ Rails. You can read more about Action Pack in
26
+ link:files/vendor/rails/actionpack/README.html.
27
+
28
+
29
+ == Getting Started
30
+
31
+ 1. At the command prompt, create a new Rails application:
32
+ <tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
33
+
34
+ 2. Change directory to <tt>myapp</tt> and start the web server:
35
+ <tt>cd myapp; rails server</tt> (run with --help for options)
36
+
37
+ 3. Go to http://localhost:3000/ and you'll see:
38
+ "Welcome aboard: You're riding Ruby on Rails!"
39
+
40
+ 4. Follow the guidelines to start developing your application. You can find
41
+ the following resources handy:
42
+
43
+ * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
44
+ * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
45
+
46
+
47
+ == Debugging Rails
48
+
49
+ Sometimes your application goes wrong. Fortunately there are a lot of tools that
50
+ will help you debug it and get it back on the rails.
51
+
52
+ First area to check is the application log files. Have "tail -f" commands
53
+ running on the server.log and development.log. Rails will automatically display
54
+ debugging and runtime information to these files. Debugging info will also be
55
+ shown in the browser on requests from 127.0.0.1.
56
+
57
+ You can also log your own messages directly into the log file from your code
58
+ using the Ruby logger class from inside your controllers. Example:
59
+
60
+ class WeblogController < ActionController::Base
61
+ def destroy
62
+ @weblog = Weblog.find(params[:id])
63
+ @weblog.destroy
64
+ logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
65
+ end
66
+ end
67
+
68
+ The result will be a message in your log file along the lines of:
69
+
70
+ Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
71
+
72
+ More information on how to use the logger is at http://www.ruby-doc.org/core/
73
+
74
+ Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
75
+ several books available online as well:
76
+
77
+ * Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
78
+ * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
79
+
80
+ These two books will bring you up to speed on the Ruby language and also on
81
+ programming in general.
82
+
83
+
84
+ == Debugger
85
+
86
+ Debugger support is available through the debugger command when you start your
87
+ Mongrel or WEBrick server with --debugger. This means that you can break out of
88
+ execution at any point in the code, investigate and change the model, and then,
89
+ resume execution! You need to install ruby-debug to run the server in debugging
90
+ mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
91
+
92
+ class WeblogController < ActionController::Base
93
+ def index
94
+ @posts = Post.all
95
+ debugger
96
+ end
97
+ end
98
+
99
+ So the controller will accept the action, run the first line, then present you
100
+ with a IRB prompt in the server window. Here you can do things like:
101
+
102
+ >> @posts.inspect
103
+ => "[#<Post:0x14a6be8
104
+ @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
105
+ #<Post:0x14a6620
106
+ @attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
107
+ >> @posts.first.title = "hello from a debugger"
108
+ => "hello from a debugger"
109
+
110
+ ...and even better, you can examine how your runtime objects actually work:
111
+
112
+ >> f = @posts.first
113
+ => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
114
+ >> f.
115
+ Display all 152 possibilities? (y or n)
116
+
117
+ Finally, when you're ready to resume execution, you can enter "cont".
118
+
119
+
120
+ == Console
121
+
122
+ The console is a Ruby shell, which allows you to interact with your
123
+ application's domain model. Here you'll have all parts of the application
124
+ configured, just like it is when the application is running. You can inspect
125
+ domain models, change values, and save to the database. Starting the script
126
+ without arguments will launch it in the development environment.
127
+
128
+ To start the console, run <tt>rails console</tt> from the application
129
+ directory.
130
+
131
+ Options:
132
+
133
+ * Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
134
+ made to the database.
135
+ * Passing an environment name as an argument will load the corresponding
136
+ environment. Example: <tt>rails console production</tt>.
137
+
138
+ To reload your controllers and models after launching the console run
139
+ <tt>reload!</tt>
140
+
141
+ More information about irb can be found at:
142
+ link:http://www.rubycentral.org/pickaxe/irb.html
143
+
144
+
145
+ == dbconsole
146
+
147
+ You can go to the command line of your database directly through <tt>rails
148
+ dbconsole</tt>. You would be connected to the database with the credentials
149
+ defined in database.yml. Starting the script without arguments will connect you
150
+ to the development database. Passing an argument will connect you to a different
151
+ database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
152
+ PostgreSQL and SQLite 3.
153
+
154
+ == Description of Contents
155
+
156
+ The default directory structure of a generated Ruby on Rails application:
157
+
158
+ |-- app
159
+ | |-- assets
160
+ | |-- images
161
+ | |-- javascripts
162
+ | `-- stylesheets
163
+ | |-- controllers
164
+ | |-- helpers
165
+ | |-- mailers
166
+ | |-- models
167
+ | `-- views
168
+ | `-- layouts
169
+ |-- config
170
+ | |-- environments
171
+ | |-- initializers
172
+ | `-- locales
173
+ |-- db
174
+ |-- doc
175
+ |-- lib
176
+ | `-- tasks
177
+ |-- log
178
+ |-- public
179
+ |-- script
180
+ |-- test
181
+ | |-- fixtures
182
+ | |-- functional
183
+ | |-- integration
184
+ | |-- performance
185
+ | `-- unit
186
+ |-- tmp
187
+ | |-- cache
188
+ | |-- pids
189
+ | |-- sessions
190
+ | `-- sockets
191
+ `-- vendor
192
+ |-- assets
193
+ `-- stylesheets
194
+ `-- plugins
195
+
196
+ app
197
+ Holds all the code that's specific to this particular application.
198
+
199
+ app/assets
200
+ Contains subdirectories for images, stylesheets, and JavaScript files.
201
+
202
+ app/controllers
203
+ Holds controllers that should be named like weblogs_controller.rb for
204
+ automated URL mapping. All controllers should descend from
205
+ ApplicationController which itself descends from ActionController::Base.
206
+
207
+ app/models
208
+ Holds models that should be named like post.rb. Models descend from
209
+ ActiveRecord::Base by default.
210
+
211
+ app/views
212
+ Holds the template files for the view that should be named like
213
+ weblogs/index.html.erb for the WeblogsController#index action. All views use
214
+ eRuby syntax by default.
215
+
216
+ app/views/layouts
217
+ Holds the template files for layouts to be used with views. This models the
218
+ common header/footer method of wrapping views. In your views, define a layout
219
+ using the <tt>layout :default</tt> and create a file named default.html.erb.
220
+ Inside default.html.erb, call <% yield %> to render the view using this
221
+ layout.
222
+
223
+ app/helpers
224
+ Holds view helpers that should be named like weblogs_helper.rb. These are
225
+ generated for you automatically when using generators for controllers.
226
+ Helpers can be used to wrap functionality for your views into methods.
227
+
228
+ config
229
+ Configuration files for the Rails environment, the routing map, the database,
230
+ and other dependencies.
231
+
232
+ db
233
+ Contains the database schema in schema.rb. db/migrate contains all the
234
+ sequence of Migrations for your schema.
235
+
236
+ doc
237
+ This directory is where your application documentation will be stored when
238
+ generated using <tt>rake doc:app</tt>
239
+
240
+ lib
241
+ Application specific libraries. Basically, any kind of custom code that
242
+ doesn't belong under controllers, models, or helpers. This directory is in
243
+ the load path.
244
+
245
+ public
246
+ The directory available for the web server. Also contains the dispatchers and the
247
+ default HTML files. This should be set as the DOCUMENT_ROOT of your web
248
+ server.
249
+
250
+ script
251
+ Helper scripts for automation and generation.
252
+
253
+ test
254
+ Unit and functional tests along with fixtures. When using the rails generate
255
+ command, template test files will be generated for you and placed in this
256
+ directory.
257
+
258
+ vendor
259
+ External libraries that the application depends on. Also includes the plugins
260
+ subdirectory. If the app has frozen rails, those gems also go here, under
261
+ vendor/rails/. This directory is in the load path.