railties 3.0.0.beta3 → 3.0.0.beta4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -0
- data/README +118 -123
- data/guides/source/3_0_release_notes.textile +13 -11
- data/guides/source/action_controller_overview.textile +2 -2
- data/guides/source/action_mailer_basics.textile +70 -26
- data/guides/source/action_view_overview.textile +1 -1
- data/guides/source/active_record_basics.textile +9 -1
- data/guides/source/active_record_querying.textile +2 -2
- data/guides/source/active_support_core_extensions.textile +377 -9
- data/guides/source/activerecord_validations_callbacks.textile +98 -55
- data/guides/source/association_basics.textile +1 -1
- data/guides/source/caching_with_rails.textile +1 -1
- data/guides/source/command_line.textile +23 -23
- data/guides/source/configuring.textile +1 -3
- data/guides/source/contribute.textile +27 -28
- data/guides/source/credits.html.erb +4 -4
- data/guides/source/debugging_rails_applications.textile +2 -2
- data/guides/source/form_helpers.textile +7 -6
- data/guides/source/generators.textile +19 -29
- data/guides/source/getting_started.textile +106 -49
- data/guides/source/i18n.textile +27 -27
- data/guides/source/index.html.erb +18 -8
- data/guides/source/initialization.textile +140 -514
- data/guides/source/layout.html.erb +6 -4
- data/guides/source/layouts_and_rendering.textile +5 -5
- data/guides/source/migrations.textile +7 -3
- data/guides/source/nested_model_forms.textile +2 -2
- data/guides/source/performance_testing.textile +11 -12
- data/guides/source/plugins.textile +30 -30
- data/guides/source/rails_application_templates.textile +3 -3
- data/guides/source/rails_on_rack.textile +3 -66
- data/guides/source/routing.textile +10 -4
- data/guides/source/security.textile +1 -1
- data/guides/source/testing.textile +55 -52
- data/guides/w3c_validator.rb +67 -0
- data/lib/rails.rb +1 -0
- data/lib/rails/application.rb +49 -13
- data/lib/rails/application/bootstrap.rb +7 -6
- data/lib/rails/application/configuration.rb +24 -47
- data/lib/rails/application/finisher.rb +8 -3
- data/lib/rails/backtrace_cleaner.rb +11 -12
- data/lib/rails/commands.rb +54 -54
- data/lib/rails/commands/application.rb +7 -2
- data/lib/rails/commands/{performance/benchmarker.rb → benchmarker.rb} +0 -0
- data/lib/rails/commands/dbconsole.rb +4 -3
- data/lib/rails/commands/destroy.rb +1 -0
- data/lib/rails/commands/generate.rb +1 -0
- data/lib/rails/commands/{performance/profiler.rb → profiler.rb} +0 -0
- data/lib/rails/commands/runner.rb +4 -2
- data/lib/rails/configuration.rb +36 -0
- data/lib/rails/engine.rb +24 -24
- data/lib/rails/engine/configuration.rb +0 -1
- data/lib/rails/generators.rb +48 -10
- data/lib/rails/generators/actions.rb +5 -3
- data/lib/rails/generators/base.rb +23 -17
- data/lib/rails/generators/erb/scaffold/templates/_form.html.erb +9 -8
- data/lib/rails/generators/erb/scaffold/templates/show.html.erb +1 -1
- data/lib/rails/generators/generated_attribute.rb +7 -6
- data/lib/rails/generators/rails/app/USAGE +2 -2
- data/lib/rails/generators/rails/app/app_generator.rb +242 -97
- data/lib/rails/generators/rails/app/templates/Gemfile +3 -0
- data/lib/rails/generators/rails/app/templates/README +167 -130
- data/lib/rails/generators/rails/app/templates/Rakefile +0 -3
- data/lib/rails/generators/rails/app/templates/config/boot.rb +9 -2
- data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml +5 -5
- data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +4 -0
- data/lib/rails/generators/rails/app/templates/script/rails +2 -5
- data/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt +1 -3
- data/lib/rails/generators/rails/stylesheets/templates/scaffold.css +5 -9
- data/lib/rails/generators/test_case.rb +12 -0
- data/lib/rails/generators/test_unit/integration/templates/integration_test.rb +1 -1
- data/lib/rails/generators/test_unit/performance/templates/performance_test.rb +1 -1
- data/lib/rails/info.rb +0 -33
- data/lib/rails/log_subscriber.rb +13 -6
- data/lib/rails/rack/logger.rb +4 -3
- data/lib/rails/railtie.rb +4 -0
- data/lib/rails/railtie/configuration.rb +21 -4
- data/lib/rails/tasks/documentation.rake +2 -0
- data/lib/rails/tasks/framework.rake +22 -0
- data/lib/rails/tasks/middleware.rake +1 -1
- data/lib/rails/tasks/routes.rake +5 -1
- data/lib/rails/test_help.rb +3 -1
- data/lib/rails/test_unit/testing.rake +3 -1
- data/lib/rails/version.rb +1 -1
- metadata +12 -19
- data/lib/rails/application/metal_loader.rb +0 -50
- data/lib/rails/dispatcher.rb +0 -24
- data/lib/rails/generators/rails/mailer/USAGE +0 -15
- data/lib/rails/generators/rails/mailer/mailer_generator.rb +0 -14
- data/lib/rails/generators/rails/mailer/templates/mailer.rb +0 -16
- data/lib/rails/generators/rails/metal/USAGE +0 -8
- data/lib/rails/generators/rails/metal/metal_generator.rb +0 -11
- data/lib/rails/generators/rails/metal/templates/metal.rb +0 -12
@@ -68,14 +68,16 @@
|
|
68
68
|
<dd><a href="security.html">Securing Rails Applications</a></dd>
|
69
69
|
<dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
|
70
70
|
<dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
|
71
|
-
<dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
|
72
71
|
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
|
73
|
-
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
|
74
72
|
<dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
|
75
73
|
<dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
|
76
74
|
<dd><a href="contributing_to_rails.html">Contributing to Rails</a></dd>
|
77
|
-
|
78
|
-
|
75
|
+
|
76
|
+
<dt>Extending Rails</dt>
|
77
|
+
<dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
|
78
|
+
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
|
79
|
+
<dd><a href="generators.html">Adding a Generator to Your Plugin</a></dd>
|
80
|
+
|
79
81
|
<dt>Release Notes</dt>
|
80
82
|
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
|
81
83
|
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
|
@@ -747,7 +747,7 @@ You can even use dynamic paths such as +cache/#{current_site}/main/display+.
|
|
747
747
|
|
748
748
|
h5. Linking to CSS Files with +stylesheet_link_tag+
|
749
749
|
|
750
|
-
The +stylesheet_link_tag+ helper returns an HTML +<link>+ tag for each source provided. Rails looks in +public/stylesheets+ for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include +public/stylesheets/main.
|
750
|
+
The +stylesheet_link_tag+ helper returns an HTML +<link>+ tag for each source provided. Rails looks in +public/stylesheets+ for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include +public/stylesheets/main.css+:
|
751
751
|
|
752
752
|
<erb>
|
753
753
|
<%= stylesheet_link_tag "main" %>
|
@@ -806,7 +806,7 @@ You can even use dynamic paths such as +cache/#{current_site}/main/display+.
|
|
806
806
|
|
807
807
|
h5. Linking to Images with +image_tag+
|
808
808
|
|
809
|
-
The +image_tag+ helper builds an HTML +<
|
809
|
+
The +image_tag+ helper builds an HTML +<img />+ tag to the specified file. By default, files are loaded from +public/images+, note, you must specify the extension, previous versions of Rails would allow you to just call the image name and would append +.png+ if no extension was given, Rails 3.0 does not.
|
810
810
|
|
811
811
|
<erb>
|
812
812
|
<%= image_tag "header.png" %>
|
@@ -895,10 +895,10 @@ The +audio_tag+ helper builds an HTML 5 +<audio>+ tag to the specified fil
|
|
895
895
|
<%= audio_tag "music.mp3" %>
|
896
896
|
</erb>
|
897
897
|
|
898
|
-
You can supply a path to the
|
898
|
+
You can supply a path to the audio file if you like:
|
899
899
|
|
900
900
|
<erb>
|
901
|
-
<%=
|
901
|
+
<%= audio_tag "music/first_song.mp3" %>
|
902
902
|
</erb>
|
903
903
|
|
904
904
|
You can also supply a hash of additional options, such as +:id+, +:class+ etc.
|
@@ -1154,7 +1154,7 @@ h4. Using Nested Layouts
|
|
1154
1154
|
|
1155
1155
|
You may find that your application requires a layout that differs slightly from your regular application layout to support one particular controller. Rather than repeating the main layout and editing it, you can accomplish this by using nested layouts (sometimes called sub-templates). Here's an example:
|
1156
1156
|
|
1157
|
-
Suppose you have the
|
1157
|
+
Suppose you have the following +ApplicationController+ layout:
|
1158
1158
|
|
1159
1159
|
* +app/views/layouts/application.html.erb+
|
1160
1160
|
|
@@ -82,9 +82,13 @@ On databases that support transactions with statements that change the schema (s
|
|
82
82
|
|
83
83
|
h4. What's in a Name
|
84
84
|
|
85
|
-
Migrations are stored in files in +db/migrate+, one for each migration class. The name of the file is of the form +YYYYMMDDHHMMSS_create_products.rb+, that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration. The
|
85
|
+
Migrations are stored in files in +db/migrate+, one for each migration class. The name of the file is of the form +YYYYMMDDHHMMSS_create_products.rb+, that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration. The name of the migration class (CamelCased version) should match the latter part of the file name. For example +20080906120000_create_products.rb+ should define +CreateProducts+ and +20080906120001_add_details_to_products.rb+ should define +AddDetailsToProducts+. If you do feel the need to change the file name then you <em>have to</em> update the name of the class inside or Rails will complain about a missing class.
|
86
86
|
|
87
|
-
Internally Rails only uses the migration's number (the timestamp) to identify them. Prior to Rails 2.1 the migration number started at 1 and was incremented each time a migration was generated. With multiple developers it was easy for these to clash requiring you to rollback migrations and renumber them. With Rails 2.1 this is largely avoided by using the creation time of the migration to identify them. You can revert to the old numbering scheme by
|
87
|
+
Internally Rails only uses the migration's number (the timestamp) to identify them. Prior to Rails 2.1 the migration number started at 1 and was incremented each time a migration was generated. With multiple developers it was easy for these to clash requiring you to rollback migrations and renumber them. With Rails 2.1 this is largely avoided by using the creation time of the migration to identify them. You can revert to the old numbering scheme by adding the following line to +config/application.rb+.
|
88
|
+
|
89
|
+
<ruby>
|
90
|
+
config.active_record.timestamped_migrations = false
|
91
|
+
</ruby>
|
88
92
|
|
89
93
|
The combination of timestamps and recording which migrations have been run allows Rails to handle common situations that occur with multiple developers.
|
90
94
|
|
@@ -568,7 +572,7 @@ In many ways this is exactly what it is. This file is created by inspecting the
|
|
568
572
|
|
569
573
|
There is however a trade-off: +db/schema.rb+ cannot express database specific items such as foreign key constraints, triggers or stored procedures. While in a migration you can execute custom SQL statements, the schema dumper cannot reconstitute those statements from the database. If you are using features like this then you should set the schema format to +:sql+.
|
570
574
|
|
571
|
-
Instead of using Active Record's schema dumper the database's structure will be dumped using a tool specific to that database (via the +db:structure:dump+ Rake task) into +db/#{
|
575
|
+
Instead of using Active Record's schema dumper the database's structure will be dumped using a tool specific to that database (via the +db:structure:dump+ Rake task) into +db/#{Rails.env}_structure.sql+. For example for PostgreSQL the +pg_dump+ utility is used and for MySQL this file will contain the output of +SHOW CREATE TABLE+ for the various tables. Loading this schema is simply a question of executing the SQL statements contained inside.
|
572
576
|
|
573
577
|
By definition this will be a perfect copy of the database's structure but this will usually prevent loading the schema into a database other than the one used to create it.
|
574
578
|
|
@@ -143,7 +143,7 @@ Now add a nested form for the +address+ association:
|
|
143
143
|
<%= form_for @person do |f| %>
|
144
144
|
<%= f.text_field :name %>
|
145
145
|
|
146
|
-
|
146
|
+
<%= f.fields_for :address do |af| %>
|
147
147
|
<%= f.text_field :street %>
|
148
148
|
<% end %>
|
149
149
|
<% end %>
|
@@ -184,7 +184,7 @@ The form code for an association collection is pretty similar to that of a singl
|
|
184
184
|
<%= form_for @person do |f| %>
|
185
185
|
<%= f.text_field :name %>
|
186
186
|
|
187
|
-
|
187
|
+
<%= f.fields_for :projects do |pf| %>
|
188
188
|
<%= f.text_field :name %>
|
189
189
|
<% end %>
|
190
190
|
<% end %>
|
@@ -246,16 +246,16 @@ Sample output of +BrowsingTest#test_homepage_wall_time.csv+:
|
|
246
246
|
|
247
247
|
<shell>
|
248
248
|
measurement,created_at,app,rails,ruby,platform
|
249
|
-
0.00738224999999992,2009-01-08T03:40:29Z,,
|
250
|
-
0.00755874999999984,2009-01-08T03:46:18Z,,
|
251
|
-
0.00762099999999993,2009-01-08T03:49:25Z,,
|
252
|
-
0.00603075000000008,2009-01-08T04:03:29Z,,
|
253
|
-
0.00619899999999995,2009-01-08T04:03:53Z,,
|
254
|
-
0.00755449999999991,2009-01-08T04:04:55Z,,
|
255
|
-
0.00595999999999997,2009-01-08T04:05:06Z,,
|
256
|
-
0.00740450000000004,2009-01-09T03:54:47Z,,
|
257
|
-
0.00603150000000008,2009-01-09T03:54:57Z,,
|
258
|
-
0.00771250000000012,2009-01-09T15:46:03Z,,
|
249
|
+
0.00738224999999992,2009-01-08T03:40:29Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
250
|
+
0.00755874999999984,2009-01-08T03:46:18Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
251
|
+
0.00762099999999993,2009-01-08T03:49:25Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
252
|
+
0.00603075000000008,2009-01-08T04:03:29Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
253
|
+
0.00619899999999995,2009-01-08T04:03:53Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
254
|
+
0.00755449999999991,2009-01-08T04:04:55Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
255
|
+
0.00595999999999997,2009-01-08T04:05:06Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
256
|
+
0.00740450000000004,2009-01-09T03:54:47Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
257
|
+
0.00603150000000008,2009-01-09T03:54:57Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
258
|
+
0.00771250000000012,2009-01-09T15:46:03Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
259
259
|
</shell>
|
260
260
|
|
261
261
|
h5(#output-profiling). Profiling
|
@@ -517,10 +517,9 @@ h4. Tutorials and Documentation
|
|
517
517
|
|
518
518
|
h3. Commercial Products
|
519
519
|
|
520
|
-
Rails has been lucky to have
|
520
|
+
Rails has been lucky to have two startups dedicated to Rails specific performance tools:
|
521
521
|
|
522
522
|
* "New Relic":http://www.newrelic.com
|
523
|
-
* "Fiveruns":http://www.fiveruns.com
|
524
523
|
* "Scout":http://scoutapp.com
|
525
524
|
|
526
525
|
h3. Changelog
|
@@ -53,7 +53,7 @@ h4. Generate the Plugin Skeleton
|
|
53
53
|
|
54
54
|
Rails ships with a plugin generator which creates a basic plugin skeleton. Pass the plugin name, either 'CamelCased' or 'under_scored', as an argument. Pass +--with-generator+ to add an example generator also.
|
55
55
|
|
56
|
-
This creates a plugin in
|
56
|
+
This creates a plugin in +vendor/plugins+ including an +init.rb+ and +README+ as well as standard +lib+, +task+, and +test+ directories.
|
57
57
|
|
58
58
|
Examples:
|
59
59
|
<shell>
|
@@ -104,27 +104,27 @@ To make it easy to organize your files and to make the plugin more compatible wi
|
|
104
104
|
`-- init.rb
|
105
105
|
</shell>
|
106
106
|
|
107
|
-
*vendor/plugins/yaffle/init.rb*
|
108
|
-
|
109
107
|
<ruby>
|
108
|
+
# vendor/plugins/yaffle/init.rb
|
109
|
+
|
110
110
|
require 'yaffle'
|
111
111
|
</ruby>
|
112
112
|
|
113
|
-
Now you can add any
|
113
|
+
Now you can add any +require+ statements to +lib/yaffle.rb+ and keep +init.rb+ clean.
|
114
114
|
|
115
115
|
h3. Tests
|
116
116
|
|
117
117
|
In this guide you will learn how to test your plugin against multiple different database adapters using Active Record. To setup your plugin to allow for easy testing you'll need to add 3 files:
|
118
118
|
|
119
|
-
* A
|
120
|
-
* A
|
119
|
+
* A +database.yml+ file with all of your connection strings
|
120
|
+
* A +schema.rb+ file with your table definitions
|
121
121
|
* A test helper method that sets up the database
|
122
122
|
|
123
123
|
h4. Test Setup
|
124
124
|
|
125
|
-
*vendor/plugins/yaffle/test/database.yml:*
|
126
|
-
|
127
125
|
<yaml>
|
126
|
+
# vendor/plugins/yaffle/test/database.yml
|
127
|
+
|
128
128
|
sqlite:
|
129
129
|
:adapter: sqlite
|
130
130
|
:dbfile: vendor/plugins/yaffle/test/yaffle_plugin.sqlite.db
|
@@ -150,9 +150,9 @@ mysql:
|
|
150
150
|
|
151
151
|
For this guide you'll need 2 tables/models, Hickwalls and Wickwalls, so add the following:
|
152
152
|
|
153
|
-
*vendor/plugins/yaffle/test/schema.rb:*
|
154
|
-
|
155
153
|
<ruby>
|
154
|
+
# vendor/plugins/yaffle/test/schema.rb
|
155
|
+
|
156
156
|
ActiveRecord::Schema.define(:version => 0) do
|
157
157
|
create_table :hickwalls, :force => true do |t|
|
158
158
|
t.string :name
|
@@ -170,9 +170,9 @@ ActiveRecord::Schema.define(:version => 0) do
|
|
170
170
|
end
|
171
171
|
</ruby>
|
172
172
|
|
173
|
-
*vendor/plugins/yaffle/test/test_helper.rb:*
|
174
|
-
|
175
173
|
<ruby>
|
174
|
+
# vendor/plugins/yaffle/test/test_helper.rb
|
175
|
+
|
176
176
|
ENV['RAILS_ENV'] = 'test'
|
177
177
|
ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
|
178
178
|
|
@@ -213,11 +213,11 @@ Now whenever you write a test that requires the database, you can call 'load_sch
|
|
213
213
|
|
214
214
|
h4. Run the Plugin Tests
|
215
215
|
|
216
|
-
Once you have these files in place, you can write your first test to ensure that your plugin-testing setup is correct.
|
217
|
-
|
218
|
-
*vendor/plugins/yaffle/test/yaffle_test.rb:*
|
216
|
+
Once you have these files in place, you can write your first test to ensure that your plugin-testing setup is correct. By default rails generates a file in +vendor/plugins/yaffle/test/yaffle_test.rb+ with a sample test. Replace the contents of that file with:
|
219
217
|
|
220
218
|
<ruby>
|
219
|
+
# vendor/plugins/yaffle/test/yaffle_test.rb
|
220
|
+
|
221
221
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
222
222
|
|
223
223
|
class YaffleTest < Test::Unit::TestCase
|
@@ -264,7 +264,7 @@ Finished in 0.002236 seconds.
|
|
264
264
|
1 test, 1 assertion, 0 failures, 0 errors
|
265
265
|
</shell>
|
266
266
|
|
267
|
-
By default the setup above runs your tests with sqlite or sqlite3. To run tests with one of the other connection strings specified in database.yml
|
267
|
+
By default the setup above runs your tests with sqlite or sqlite3. To run tests with one of the other connection strings specified in +database.yml+, pass the DB environment variable to rake:
|
268
268
|
|
269
269
|
<shell>
|
270
270
|
rake DB=sqlite
|
@@ -281,9 +281,9 @@ This section will explain how to add a method to String that will be available a
|
|
281
281
|
|
282
282
|
In this example you will add a method to String named +to_squawk+. To begin, create a new test file with a few assertions:
|
283
283
|
|
284
|
-
* *vendor/plugins/yaffle/test/core_ext_test.rb*
|
285
|
-
|
286
284
|
<ruby>
|
285
|
+
# vendor/plugins/yaffle/test/core_ext_test.rb
|
286
|
+
|
287
287
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
288
288
|
|
289
289
|
class CoreExtTest < Test::Unit::TestCase
|
@@ -311,19 +311,19 @@ NoMethodError: undefined method `to_squawk' for "Hello World":String
|
|
311
311
|
|
312
312
|
Great - now you are ready to start development.
|
313
313
|
|
314
|
-
Then in
|
315
|
-
|
316
|
-
* *vendor/plugins/yaffle/lib/yaffle.rb*
|
314
|
+
Then in +lib/yaffle.rb+ require +lib/core_ext.rb+:
|
317
315
|
|
318
316
|
<ruby>
|
317
|
+
# vendor/plugins/yaffle/lib/yaffle.rb
|
318
|
+
|
319
319
|
require "yaffle/core_ext"
|
320
320
|
</ruby>
|
321
321
|
|
322
|
-
Finally, create the
|
323
|
-
|
324
|
-
* *vendor/plugins/yaffle/lib/yaffle/core_ext.rb*
|
322
|
+
Finally, create the +core_ext.rb+ file and add the +to_squawk+ method:
|
325
323
|
|
326
324
|
<ruby>
|
325
|
+
# vendor/plugins/yaffle/lib/yaffle/core_ext.rb
|
326
|
+
|
327
327
|
String.class_eval do
|
328
328
|
def to_squawk
|
329
329
|
"squawk! #{self}".strip
|
@@ -331,7 +331,7 @@ String.class_eval do
|
|
331
331
|
end
|
332
332
|
</ruby>
|
333
333
|
|
334
|
-
To test that your method does what it says it does, run the unit tests with +rake+ from your plugin directory.
|
334
|
+
To test that your method does what it says it does, run the unit tests with +rake+ from your plugin directory. To see this in action, fire up a console and start squawking:
|
335
335
|
|
336
336
|
<shell>
|
337
337
|
$ rails console
|
@@ -345,13 +345,13 @@ When Rails loads plugins it looks for a file named +init.rb+. However, when the
|
|
345
345
|
|
346
346
|
NOTE: The plugins loader also looks for +rails/init.rb+, but that one is deprecated in favor of the top-level +init.rb+ aforementioned.
|
347
347
|
|
348
|
-
Under certain circumstances if you reopen classes or modules in +init.rb+ you may inadvertently create a new class, rather than reopening an existing class.
|
348
|
+
Under certain circumstances if you reopen classes or modules in +init.rb+ you may inadvertently create a new class, rather than reopening an existing class. A better alternative is to reopen the class in a different file, and require that file from +init.rb+, as shown above.
|
349
349
|
|
350
350
|
If you must reopen a class in +init.rb+ you can use +module_eval+ or +class_eval+ to avoid any issues:
|
351
351
|
|
352
|
-
* *vendor/plugins/yaffle/init.rb*
|
353
|
-
|
354
352
|
<ruby>
|
353
|
+
# vendor/plugins/yaffle/init.rb
|
354
|
+
|
355
355
|
Hash.class_eval do
|
356
356
|
def is_a_special_hash?
|
357
357
|
true
|
@@ -361,9 +361,9 @@ end
|
|
361
361
|
|
362
362
|
Another way is to explicitly define the top-level module space for all modules and classes, like +::Hash+:
|
363
363
|
|
364
|
-
* *vendor/plugins/yaffle/init.rb*
|
365
|
-
|
366
364
|
<ruby>
|
365
|
+
# vendor/plugins/yaffle/init.rb
|
366
|
+
|
367
367
|
class ::Hash
|
368
368
|
def is_a_special_hash?
|
369
369
|
true
|
@@ -14,13 +14,13 @@ h3. Usage
|
|
14
14
|
To apply a template, you need to provide the Rails generator with the location of the template you wish to apply, using -m option :
|
15
15
|
|
16
16
|
<shell>
|
17
|
-
$ rails blog -m ~/template.rb
|
17
|
+
$ rails new blog -m ~/template.rb
|
18
18
|
</shell>
|
19
19
|
|
20
20
|
It's also possible to apply a template using a URL :
|
21
21
|
|
22
22
|
<shell>
|
23
|
-
$ rails blog -m http://gist.github.com/31208.txt
|
23
|
+
$ rails new blog -m http://gist.github.com/31208.txt
|
24
24
|
</shell>
|
25
25
|
|
26
26
|
Alternatively, you can use the rake task +rails:template+ to apply a template to an existing Rails application :
|
@@ -113,7 +113,7 @@ CODE
|
|
113
113
|
|
114
114
|
Similarly +lib()+ creates a file in the +lib/+ directory and +vendor()+ creates a file in the +vendor/+ directory.
|
115
115
|
|
116
|
-
There is even +file()+, which accepts a relative path from +
|
116
|
+
There is even +file()+, which accepts a relative path from +Rails.root+ and creates all the directories/file needed :
|
117
117
|
|
118
118
|
<ruby>
|
119
119
|
file 'app/components/foo.rb', <<-CODE
|
@@ -49,7 +49,7 @@ Middlewares used in the code above are primarily useful only in the development
|
|
49
49
|
|
50
50
|
|_.Middleware|_.Purpose|
|
51
51
|
|+Rails::Rack::LogTailer+|Appends log file output to console|
|
52
|
-
|+ActionDispatch::Static+|Serves static files inside +
|
52
|
+
|+ActionDispatch::Static+|Serves static files inside +Rails.root/public+ directory|
|
53
53
|
|+Rails::Rack::Debugger+|Starts Debugger|
|
54
54
|
|
55
55
|
h4. +rackup+
|
@@ -57,7 +57,7 @@ h4. +rackup+
|
|
57
57
|
To use +rackup+ instead of Rails' +rails server+, you can put the following inside +config.ru+ of your Rails application's root directory:
|
58
58
|
|
59
59
|
<ruby>
|
60
|
-
#
|
60
|
+
# Rails.root/config.ru
|
61
61
|
require "config/environment"
|
62
62
|
|
63
63
|
use Rails::Rack::LogTailer
|
@@ -214,7 +214,7 @@ config.middleware.clear
|
|
214
214
|
</ruby>
|
215
215
|
|
216
216
|
<br />
|
217
|
-
<strong>Add a +config.ru+ file to +
|
217
|
+
<strong>Add a +config.ru+ file to +Rails.root+</strong>
|
218
218
|
|
219
219
|
<ruby>
|
220
220
|
# config.ru
|
@@ -222,69 +222,6 @@ use MyOwnStackFromStratch
|
|
222
222
|
run ActionController::Dispatcher.new
|
223
223
|
</ruby>
|
224
224
|
|
225
|
-
h3. Rails Metal Applications
|
226
|
-
|
227
|
-
Rails Metal applications are minimal Rack applications specially designed for integrating with a typical Rails application. As Rails Metal Applications skip all of the Action Controller stack, serving a request has no overhead from the Rails framework itself. This is especially useful for infrequent cases where the performance of the full stack Rails framework is an issue.
|
228
|
-
|
229
|
-
Ryan Bates' "Railscast on Rails Metal":http://railscasts.com/episodes/150-rails-metal provides a nice walkthrough generating and using Rails Metal.
|
230
|
-
|
231
|
-
h4. Generating a Metal Application
|
232
|
-
|
233
|
-
Rails provides a generator called +metal+ for creating a new Metal application:
|
234
|
-
|
235
|
-
<shell>
|
236
|
-
$ rails generate metal poller
|
237
|
-
</shell>
|
238
|
-
|
239
|
-
This generates +poller.rb+ in the +app/metal+ directory:
|
240
|
-
|
241
|
-
<ruby>
|
242
|
-
# Allow the metal piece to run in isolation
|
243
|
-
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
|
244
|
-
|
245
|
-
class Poller
|
246
|
-
def self.call(env)
|
247
|
-
if env["PATH_INFO"] =~ /^\/poller/
|
248
|
-
[200, {"Content-Type" => "text/html"}, ["Hello, World!"]]
|
249
|
-
else
|
250
|
-
[404, {"Content-Type" => "text/html"}, ["Not Found"]]
|
251
|
-
end
|
252
|
-
end
|
253
|
-
end
|
254
|
-
</ruby>
|
255
|
-
|
256
|
-
Metal applications within +app/metal+ folders in plugins will also be discovered and added to the list.
|
257
|
-
|
258
|
-
Metal applications are an optimization. You should make sure to "understand the related performance implications":http://weblog.rubyonrails.org/2008/12/20/performance-of-rails-metal before using it.
|
259
|
-
|
260
|
-
h4. Execution Order
|
261
|
-
|
262
|
-
All Metal Applications are executed by +Rails::Rack::Metal+ middleware, which is a part of the +ActionController::MiddlewareStack+ chain.
|
263
|
-
|
264
|
-
Here's the primary method responsible for running the Metal applications:
|
265
|
-
|
266
|
-
<ruby>
|
267
|
-
def call(env)
|
268
|
-
@metals.keys.each do |app|
|
269
|
-
result = app.call(env)
|
270
|
-
return result unless result[0].to_i == 404
|
271
|
-
end
|
272
|
-
@app.call(env)
|
273
|
-
end
|
274
|
-
</ruby>
|
275
|
-
|
276
|
-
In the code above, +@metals+ is an ordered hash of metal applications. Due to the default alphabetical ordering, +aaa.rb+ will come before +bbb.rb+ in the metal chain.
|
277
|
-
|
278
|
-
It is, however, possible to override the default ordering in your environment. Simply add a line like the following to +config/environment.rb+
|
279
|
-
|
280
|
-
<ruby>
|
281
|
-
config.metals = ["Bbb", "Aaa"]
|
282
|
-
</ruby>
|
283
|
-
|
284
|
-
Each string in the array should be the name of your metal class. If you do this then be warned that any metal applications not listed will not be loaded.
|
285
|
-
|
286
|
-
WARNING: Metal applications cannot return the HTTP Status +404+ to a client, as it is used for continuing the Metal chain execution. Please use normal Rails controllers or a custom middleware if returning +404+ is a requirement.
|
287
|
-
|
288
225
|
h3. Resources
|
289
226
|
|
290
227
|
h4. Learning Rack
|
@@ -118,6 +118,12 @@ h4. Singular Resources
|
|
118
118
|
|
119
119
|
Sometimes, you have a resource that clients always look up without referencing an ID. A common example, +/profile+ always shows the profile of the currently logged in user. In this case, you can use a singular resource to map +/profile+ (rather than +/profile/:id+) to the +show+ action.
|
120
120
|
|
121
|
+
<ruby>
|
122
|
+
match "profile" => "users#show"
|
123
|
+
</ruby>
|
124
|
+
|
125
|
+
This resourceful route
|
126
|
+
|
121
127
|
<ruby>
|
122
128
|
resource :geocoder
|
123
129
|
</ruby>
|
@@ -391,7 +397,7 @@ h4. The Query String
|
|
391
397
|
The +params+ will also include any parameters from the query string. For example, with this route:
|
392
398
|
|
393
399
|
<ruby>
|
394
|
-
match ':controller/:action/:id
|
400
|
+
match ':controller/:action/:id'
|
395
401
|
</ruby>
|
396
402
|
|
397
403
|
An incoming URL of +/photos/show/1?user_id=2+ will be dispatched to the +show+ action of the +Photos+ controller. +params+ will be <tt>{ :controller => "photos", :action => "show", :id => "1", :user_id => "2" }</tt>.
|
@@ -419,7 +425,7 @@ h4. Naming Routes
|
|
419
425
|
You can specify a name for any route using the +:as+ option.
|
420
426
|
|
421
427
|
<ruby>
|
422
|
-
match '
|
428
|
+
match 'exit' => 'sessions#destroy', :as => :logout
|
423
429
|
</ruby>
|
424
430
|
|
425
431
|
This will create +logout_path+ and +logout_url+ as named helpers in your application. Calling +logout_path+ will return +/logout+
|
@@ -636,7 +642,7 @@ end
|
|
636
642
|
resources :photos
|
637
643
|
</ruby>
|
638
644
|
|
639
|
-
This will provide route helpers such as +
|
645
|
+
This will provide route helpers such as +admin_photos_path+, +new_admin_photo_path+ etc.
|
640
646
|
|
641
647
|
You could specify a name prefix to use for a group of routes in the scope:
|
642
648
|
|
@@ -711,7 +717,7 @@ resources :magazines do
|
|
711
717
|
end
|
712
718
|
</ruby>
|
713
719
|
|
714
|
-
This will create routing helpers such as +
|
720
|
+
This will create routing helpers such as +magazine_periodical_ads_url+ and +edit_magazine_periodical_ad_path+.
|
715
721
|
|
716
722
|
h3. Inspecting and Testing Routes
|
717
723
|
|