rails 4.0.13 → 4.1.16
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.
- checksums.yaml +4 -4
- data/README.md +22 -17
- data/guides/CHANGELOG.md +68 -34
- data/guides/assets/images/edge_badge.png +0 -0
- data/guides/assets/images/feature_tile.gif +0 -0
- data/guides/assets/images/footer_tile.gif +0 -0
- data/guides/assets/images/fxn.png +0 -0
- data/guides/assets/images/getting_started/article_with_comments.png +0 -0
- data/guides/assets/images/getting_started/challenge.png +0 -0
- data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
- data/guides/assets/images/getting_started/form_with_errors.png +0 -0
- data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
- data/guides/assets/images/getting_started/new_article.png +0 -0
- data/guides/assets/images/getting_started/rails_welcome.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
- data/guides/assets/images/getting_started/show_action_for_articles.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_articles_new.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_articles.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_articles.png +0 -0
- data/guides/assets/images/header_tile.gif +0 -0
- data/guides/assets/images/icons/README +1 -1
- data/guides/assets/images/icons/callouts/11.png +0 -0
- data/guides/assets/images/icons/callouts/12.png +0 -0
- data/guides/assets/images/icons/callouts/13.png +0 -0
- data/guides/assets/images/icons/callouts/15.png +0 -0
- data/guides/assets/images/icons/caution.png +0 -0
- data/guides/assets/images/icons/example.png +0 -0
- data/guides/assets/images/radar.png +0 -0
- data/guides/assets/images/rails4_features.png +0 -0
- data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/assets/images/vijaydev.jpg +0 -0
- data/guides/assets/javascripts/guides.js +30 -34
- data/guides/assets/stylesheets/main.css +2 -1
- data/guides/assets/stylesheets/print.css +1 -1
- data/guides/bug_report_templates/action_controller_gem.rb +9 -4
- data/guides/bug_report_templates/action_controller_master.rb +4 -2
- data/guides/bug_report_templates/active_record_gem.rb +5 -2
- data/guides/bug_report_templates/active_record_master.rb +2 -1
- data/guides/bug_report_templates/generic_gem.rb +15 -0
- data/guides/bug_report_templates/generic_master.rb +26 -0
- data/guides/code/getting_started/Gemfile +21 -24
- data/guides/code/getting_started/Gemfile.lock +78 -73
- data/guides/code/getting_started/Rakefile +1 -1
- data/guides/code/getting_started/app/assets/javascripts/application.js +1 -2
- data/guides/code/getting_started/app/views/layouts/application.html.erb +2 -2
- data/guides/code/getting_started/config/environment.rb +1 -1
- data/guides/code/getting_started/config/environments/development.rb +2 -2
- data/guides/code/getting_started/config/environments/production.rb +3 -3
- data/guides/code/getting_started/config/environments/test.rb +2 -2
- data/guides/code/getting_started/config/initializers/secret_token.rb +1 -1
- data/guides/code/getting_started/config/initializers/session_store.rb +1 -1
- data/guides/code/getting_started/config/routes.rb +1 -1
- data/guides/code/getting_started/config.ru +1 -1
- data/guides/code/getting_started/public/404.html +2 -0
- data/guides/code/getting_started/public/422.html +2 -0
- data/guides/code/getting_started/public/500.html +2 -0
- data/guides/code/getting_started/test/test_helper.rb +0 -3
- data/guides/rails_guides/helpers.rb +3 -1
- data/guides/source/2_2_release_notes.md +2 -2
- data/guides/source/2_3_release_notes.md +8 -8
- data/guides/source/3_0_release_notes.md +2 -3
- data/guides/source/3_1_release_notes.md +2 -2
- data/guides/source/3_2_release_notes.md +12 -12
- data/guides/source/4_0_release_notes.md +79 -46
- data/guides/source/4_1_release_notes.md +731 -0
- data/guides/source/_welcome.html.erb +5 -2
- data/guides/source/action_controller_overview.md +189 -40
- data/guides/source/action_mailer_basics.md +27 -27
- data/guides/source/action_view_overview.md +131 -20
- data/guides/source/active_model_basics.md +6 -6
- data/guides/source/active_record_basics.md +15 -15
- data/guides/source/active_record_callbacks.md +18 -16
- data/guides/source/active_record_querying.md +93 -51
- data/guides/source/active_record_validations.md +26 -24
- data/guides/source/active_support_core_extensions.md +72 -118
- data/guides/source/active_support_instrumentation.md +13 -4
- data/guides/source/api_documentation_guidelines.md +104 -6
- data/guides/source/asset_pipeline.md +573 -244
- data/guides/source/association_basics.md +94 -22
- data/guides/source/caching_with_rails.md +15 -6
- data/guides/source/command_line.md +55 -46
- data/guides/source/configuring.md +248 -52
- data/guides/source/contributing_to_ruby_on_rails.md +18 -17
- data/guides/source/credits.html.erb +2 -2
- data/guides/source/debugging_rails_applications.md +39 -8
- data/guides/source/development_dependencies_install.md +91 -8
- data/guides/source/documents.yaml +4 -0
- data/guides/source/engines.md +678 -232
- data/guides/source/form_helpers.md +53 -35
- data/guides/source/generators.md +19 -15
- data/guides/source/getting_started.md +758 -497
- data/guides/source/i18n.md +64 -28
- data/guides/source/index.html.erb +1 -1
- data/guides/source/initialization.md +155 -58
- data/guides/source/kindle/toc.html.erb +1 -1
- data/guides/source/layout.html.erb +2 -2
- data/guides/source/layouts_and_rendering.md +59 -26
- data/guides/source/maintenance_policy.md +3 -3
- data/guides/source/migrations.md +101 -62
- data/guides/source/nested_model_forms.md +3 -3
- data/guides/source/plugins.md +34 -31
- data/guides/source/rails_application_templates.md +27 -8
- data/guides/source/rails_on_rack.md +41 -58
- data/guides/source/routing.md +115 -104
- data/guides/source/ruby_on_rails_guides_guidelines.md +2 -2
- data/guides/source/security.md +81 -36
- data/guides/source/testing.md +56 -79
- data/guides/source/upgrading_ruby_on_rails.md +531 -21
- data/guides/source/working_with_javascript_in_rails.md +19 -11
- metadata +51 -23
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
- data/guides/assets/images/getting_started/new_post.png +0 -0
- data/guides/assets/images/getting_started/post_with_comments.png +0 -0
- data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
- data/guides/assets/images/getting_started/undefined_method_post_path.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
- data/guides/assets/images/jaimeiniesta.jpg +0 -0
- data/guides/source/kindle/KINDLE.md +0 -26
data/guides/source/plugins.md
CHANGED
|
@@ -3,9 +3,9 @@ The Basics of Creating Rails Plugins
|
|
|
3
3
|
|
|
4
4
|
A Rails plugin is either an extension or a modification of the core framework. Plugins provide:
|
|
5
5
|
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* A way for developers to share bleeding-edge ideas without hurting the stable code base.
|
|
7
|
+
* A segmented architecture so that units of code can be fixed or updated on their own release schedule.
|
|
8
|
+
* An outlet for the core developers so that they don't have to include every cool new feature under the sun.
|
|
9
9
|
|
|
10
10
|
After reading this guide, you will know:
|
|
11
11
|
|
|
@@ -15,7 +15,7 @@ After reading this guide, you will know:
|
|
|
15
15
|
This guide describes how to build a test-driven plugin that will:
|
|
16
16
|
|
|
17
17
|
* Extend core Ruby classes like Hash and String.
|
|
18
|
-
* Add methods to ActiveRecord::Base in the tradition of the
|
|
18
|
+
* Add methods to `ActiveRecord::Base` in the tradition of the `acts_as` plugins.
|
|
19
19
|
* Give you information about where to put generators in your plugin.
|
|
20
20
|
|
|
21
21
|
For the purpose of this guide pretend for a moment that you are an avid bird watcher.
|
|
@@ -39,16 +39,16 @@ to run integration tests using a dummy Rails application. Create your
|
|
|
39
39
|
plugin with the command:
|
|
40
40
|
|
|
41
41
|
```bash
|
|
42
|
-
$ rails plugin new yaffle
|
|
42
|
+
$ bin/rails plugin new yaffle
|
|
43
43
|
```
|
|
44
44
|
|
|
45
45
|
See usage and options by asking for help:
|
|
46
46
|
|
|
47
47
|
```bash
|
|
48
|
-
$ rails plugin --help
|
|
48
|
+
$ bin/rails plugin --help
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
-
Testing
|
|
51
|
+
Testing Your Newly Generated Plugin
|
|
52
52
|
-----------------------------------
|
|
53
53
|
|
|
54
54
|
You can navigate to the directory that contains the plugin, run the `bundle install` command
|
|
@@ -74,7 +74,7 @@ In this example you will add a method to String named `to_squawk`. To begin, cre
|
|
|
74
74
|
|
|
75
75
|
require 'test_helper'
|
|
76
76
|
|
|
77
|
-
class CoreExtTest <
|
|
77
|
+
class CoreExtTest < ActiveSupport::TestCase
|
|
78
78
|
def test_to_squawk_prepends_the_word_squawk
|
|
79
79
|
assert_equal "squawk! Hello World", "Hello World".to_squawk
|
|
80
80
|
end
|
|
@@ -92,7 +92,7 @@ Run `rake` to run the test. This test should fail because we haven't implemented
|
|
|
92
92
|
|
|
93
93
|
Great - now you are ready to start development.
|
|
94
94
|
|
|
95
|
-
|
|
95
|
+
In `lib/yaffle.rb`, add `require "yaffle/core_ext"`:
|
|
96
96
|
|
|
97
97
|
```ruby
|
|
98
98
|
# yaffle/lib/yaffle.rb
|
|
@@ -124,7 +124,7 @@ To test that your method does what it says it does, run the unit tests with `rak
|
|
|
124
124
|
To see this in action, change to the test/dummy directory, fire up a console and start squawking:
|
|
125
125
|
|
|
126
126
|
```bash
|
|
127
|
-
$ rails console
|
|
127
|
+
$ bin/rails console
|
|
128
128
|
>> "Hello World".to_squawk
|
|
129
129
|
=> "squawk! Hello World"
|
|
130
130
|
```
|
|
@@ -132,8 +132,8 @@ $ rails console
|
|
|
132
132
|
Add an "acts_as" Method to Active Record
|
|
133
133
|
----------------------------------------
|
|
134
134
|
|
|
135
|
-
A common pattern in plugins is to add a method called
|
|
136
|
-
want to write a method called
|
|
135
|
+
A common pattern in plugins is to add a method called `acts_as_something` to models. In this case, you
|
|
136
|
+
want to write a method called `acts_as_yaffle` that adds a `squawk` method to your Active Record models.
|
|
137
137
|
|
|
138
138
|
To begin, set up your files so that you have:
|
|
139
139
|
|
|
@@ -142,7 +142,7 @@ To begin, set up your files so that you have:
|
|
|
142
142
|
|
|
143
143
|
require 'test_helper'
|
|
144
144
|
|
|
145
|
-
class ActsAsYaffleTest <
|
|
145
|
+
class ActsAsYaffleTest < ActiveSupport::TestCase
|
|
146
146
|
end
|
|
147
147
|
```
|
|
148
148
|
|
|
@@ -168,9 +168,9 @@ end
|
|
|
168
168
|
|
|
169
169
|
### Add a Class Method
|
|
170
170
|
|
|
171
|
-
This plugin will expect that you've added a method to your model named
|
|
172
|
-
plugin users might have already defined a method on their model named
|
|
173
|
-
for something else. This plugin will allow the name to be changed by adding a class method called
|
|
171
|
+
This plugin will expect that you've added a method to your model named `last_squawk`. However, the
|
|
172
|
+
plugin users might have already defined a method on their model named `last_squawk` that they use
|
|
173
|
+
for something else. This plugin will allow the name to be changed by adding a class method called `yaffle_text_field`.
|
|
174
174
|
|
|
175
175
|
To start out, write a failing test that shows the behavior you'd like:
|
|
176
176
|
|
|
@@ -179,7 +179,7 @@ To start out, write a failing test that shows the behavior you'd like:
|
|
|
179
179
|
|
|
180
180
|
require 'test_helper'
|
|
181
181
|
|
|
182
|
-
class ActsAsYaffleTest <
|
|
182
|
+
class ActsAsYaffleTest < ActiveSupport::TestCase
|
|
183
183
|
|
|
184
184
|
def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
|
|
185
185
|
assert_equal "last_squawk", Hickwall.yaffle_text_field
|
|
@@ -214,17 +214,16 @@ test/dummy directory:
|
|
|
214
214
|
|
|
215
215
|
```bash
|
|
216
216
|
$ cd test/dummy
|
|
217
|
-
$ rails generate model Hickwall last_squawk:string
|
|
218
|
-
$ rails generate model Wickwall last_squawk:string last_tweet:string
|
|
217
|
+
$ bin/rails generate model Hickwall last_squawk:string
|
|
218
|
+
$ bin/rails generate model Wickwall last_squawk:string last_tweet:string
|
|
219
219
|
```
|
|
220
220
|
|
|
221
221
|
Now you can create the necessary database tables in your testing database by navigating to your dummy app
|
|
222
|
-
and migrating the database. First
|
|
222
|
+
and migrating the database. First, run:
|
|
223
223
|
|
|
224
224
|
```bash
|
|
225
225
|
$ cd test/dummy
|
|
226
|
-
$ rake db:migrate
|
|
227
|
-
$ rake db:test:prepare
|
|
226
|
+
$ bin/rake db:migrate
|
|
228
227
|
```
|
|
229
228
|
|
|
230
229
|
While you are here, change the Hickwall and Wickwall models so that they know that they are supposed to act
|
|
@@ -245,7 +244,7 @@ end
|
|
|
245
244
|
|
|
246
245
|
```
|
|
247
246
|
|
|
248
|
-
We will also add code to define the acts_as_yaffle method.
|
|
247
|
+
We will also add code to define the `acts_as_yaffle` method.
|
|
249
248
|
|
|
250
249
|
```ruby
|
|
251
250
|
# yaffle/lib/yaffle/acts_as_yaffle.rb
|
|
@@ -286,7 +285,7 @@ You can then return to the root directory (`cd ../..`) of your plugin and rerun
|
|
|
286
285
|
|
|
287
286
|
```
|
|
288
287
|
|
|
289
|
-
Getting closer... Now we will implement the code of the acts_as_yaffle method to make the tests pass.
|
|
288
|
+
Getting closer... Now we will implement the code of the `acts_as_yaffle` method to make the tests pass.
|
|
290
289
|
|
|
291
290
|
```ruby
|
|
292
291
|
# yaffle/lib/yaffle/acts_as_yaffle.rb
|
|
@@ -310,7 +309,7 @@ end
|
|
|
310
309
|
ActiveRecord::Base.send :include, Yaffle::ActsAsYaffle
|
|
311
310
|
```
|
|
312
311
|
|
|
313
|
-
When you run `rake
|
|
312
|
+
When you run `rake`, you should see the tests all pass:
|
|
314
313
|
|
|
315
314
|
```bash
|
|
316
315
|
5 tests, 5 assertions, 0 failures, 0 errors, 0 skips
|
|
@@ -327,7 +326,7 @@ To start out, write a failing test that shows the behavior you'd like:
|
|
|
327
326
|
# yaffle/test/acts_as_yaffle_test.rb
|
|
328
327
|
require 'test_helper'
|
|
329
328
|
|
|
330
|
-
class ActsAsYaffleTest <
|
|
329
|
+
class ActsAsYaffleTest < ActiveSupport::TestCase
|
|
331
330
|
|
|
332
331
|
def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
|
|
333
332
|
assert_equal "last_squawk", Hickwall.yaffle_text_field
|
|
@@ -390,7 +389,11 @@ Run `rake` one final time and you should see:
|
|
|
390
389
|
7 tests, 7 assertions, 0 failures, 0 errors, 0 skips
|
|
391
390
|
```
|
|
392
391
|
|
|
393
|
-
NOTE: The use of `write_attribute` to write to the field in model is just one example of how a plugin can interact with the model, and will not always be the right method to use. For example, you could also use
|
|
392
|
+
NOTE: The use of `write_attribute` to write to the field in model is just one example of how a plugin can interact with the model, and will not always be the right method to use. For example, you could also use:
|
|
393
|
+
|
|
394
|
+
```ruby
|
|
395
|
+
send("#{self.class.yaffle_text_field}=", string.to_squawk)
|
|
396
|
+
```
|
|
394
397
|
|
|
395
398
|
Generators
|
|
396
399
|
----------
|
|
@@ -398,7 +401,7 @@ Generators
|
|
|
398
401
|
Generators can be included in your gem simply by creating them in a lib/generators directory of your plugin. More information about
|
|
399
402
|
the creation of generators can be found in the [Generators Guide](generators.html)
|
|
400
403
|
|
|
401
|
-
Publishing
|
|
404
|
+
Publishing Your Gem
|
|
402
405
|
-------------------
|
|
403
406
|
|
|
404
407
|
Gem plugins currently in development can easily be shared from any Git repository. To share the Yaffle gem with others, simply
|
|
@@ -411,12 +414,12 @@ gem 'yaffle', git: 'git://github.com/yaffle_watcher/yaffle.git'
|
|
|
411
414
|
After running `bundle install`, your gem functionality will be available to the application.
|
|
412
415
|
|
|
413
416
|
When the gem is ready to be shared as a formal release, it can be published to [RubyGems](http://www.rubygems.org).
|
|
414
|
-
For more information about publishing gems to RubyGems, see: [Creating and Publishing Your First Ruby Gem](http://blog.thepete.net/2010/11/creating-and-publishing-your-first-ruby.html)
|
|
417
|
+
For more information about publishing gems to RubyGems, see: [Creating and Publishing Your First Ruby Gem](http://blog.thepete.net/2010/11/creating-and-publishing-your-first-ruby.html).
|
|
415
418
|
|
|
416
419
|
RDoc Documentation
|
|
417
420
|
------------------
|
|
418
421
|
|
|
419
|
-
Once your plugin is stable and you are ready to deploy do everyone else a favor and document it! Luckily, writing documentation for your plugin is easy.
|
|
422
|
+
Once your plugin is stable and you are ready to deploy, do everyone else a favor and document it! Luckily, writing documentation for your plugin is easy.
|
|
420
423
|
|
|
421
424
|
The first step is to update the README file with detailed information about how to use your plugin. A few key things to include are:
|
|
422
425
|
|
|
@@ -430,7 +433,7 @@ Once your README is solid, go through and add rdoc comments to all of the method
|
|
|
430
433
|
Once your comments are good to go, navigate to your plugin directory and run:
|
|
431
434
|
|
|
432
435
|
```bash
|
|
433
|
-
$ rake rdoc
|
|
436
|
+
$ bin/rake rdoc
|
|
434
437
|
```
|
|
435
438
|
|
|
436
439
|
### References
|
|
@@ -23,8 +23,8 @@ $ rails new blog -m http://example.com/template.rb
|
|
|
23
23
|
You can use the rake task `rails:template` to apply templates to an existing Rails application. The location of the template needs to be passed in to an environment variable named LOCATION. Again, this can either be path to a file or a URL.
|
|
24
24
|
|
|
25
25
|
```bash
|
|
26
|
-
$ rake rails:template LOCATION=~/template.rb
|
|
27
|
-
$ rake rails:template LOCATION=http://example.com/template.rb
|
|
26
|
+
$ bin/rake rails:template LOCATION=~/template.rb
|
|
27
|
+
$ bin/rake rails:template LOCATION=http://example.com/template.rb
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
Template API
|
|
@@ -47,7 +47,7 @@ The following sections outline the primary methods provided by the API:
|
|
|
47
47
|
|
|
48
48
|
### gem(*args)
|
|
49
49
|
|
|
50
|
-
Adds a `gem` entry for the supplied gem to the generated application
|
|
50
|
+
Adds a `gem` entry for the supplied gem to the generated application's `Gemfile`.
|
|
51
51
|
|
|
52
52
|
For example, if your application depends on the gems `bj` and `nokogiri`:
|
|
53
53
|
|
|
@@ -91,14 +91,14 @@ Adds a line inside the `Application` class for `config/application.rb`.
|
|
|
91
91
|
If `options[:env]` is specified, the line is appended to the corresponding file in `config/environments`.
|
|
92
92
|
|
|
93
93
|
```ruby
|
|
94
|
-
environment 'config.action_mailer.default_url_options = {host:
|
|
94
|
+
environment 'config.action_mailer.default_url_options = {host: "http://yourwebsite.example.com"}', env: 'production'
|
|
95
95
|
```
|
|
96
96
|
|
|
97
97
|
A block can be used in place of the `data` argument.
|
|
98
98
|
|
|
99
99
|
### vendor/lib/file/initializer(filename, data = nil, &block)
|
|
100
100
|
|
|
101
|
-
Adds an initializer to the generated application
|
|
101
|
+
Adds an initializer to the generated application's `config/initializers` directory.
|
|
102
102
|
|
|
103
103
|
Let's say you like using `Object#not_nil?` and `Object#not_blank?`:
|
|
104
104
|
|
|
@@ -127,7 +127,7 @@ file 'app/components/foo.rb', <<-CODE
|
|
|
127
127
|
CODE
|
|
128
128
|
```
|
|
129
129
|
|
|
130
|
-
That
|
|
130
|
+
That'll create the `app/components` directory and put `foo.rb` in there.
|
|
131
131
|
|
|
132
132
|
### rakefile(filename, data = nil, &block)
|
|
133
133
|
|
|
@@ -197,7 +197,7 @@ end
|
|
|
197
197
|
|
|
198
198
|
### ask(question)
|
|
199
199
|
|
|
200
|
-
`ask()` gives you a chance to get some feedback from the user and use it in your templates. Let's say you want your user to name the new shiny library you
|
|
200
|
+
`ask()` gives you a chance to get some feedback from the user and use it in your templates. Let's say you want your user to name the new shiny library you're adding:
|
|
201
201
|
|
|
202
202
|
```ruby
|
|
203
203
|
lib_name = ask("What do you want to call the shiny library ?")
|
|
@@ -211,7 +211,7 @@ CODE
|
|
|
211
211
|
|
|
212
212
|
### yes?(question) or no?(question)
|
|
213
213
|
|
|
214
|
-
These methods let you ask questions from templates and decide the flow based on the user
|
|
214
|
+
These methods let you ask questions from templates and decide the flow based on the user's answer. Let's say you want to freeze rails only if the user wants to:
|
|
215
215
|
|
|
216
216
|
```ruby
|
|
217
217
|
rake("rails:freeze:gems") if yes?("Freeze rails gems?")
|
|
@@ -227,3 +227,22 @@ git :init
|
|
|
227
227
|
git add: "."
|
|
228
228
|
git commit: "-a -m 'Initial commit'"
|
|
229
229
|
```
|
|
230
|
+
|
|
231
|
+
Advanced Usage
|
|
232
|
+
--------------
|
|
233
|
+
|
|
234
|
+
The application template is evaluated in the context of a
|
|
235
|
+
`Rails::Generators::AppGenerator` instance. It uses the `apply` action
|
|
236
|
+
provided by
|
|
237
|
+
[Thor](https://github.com/erikhuda/thor/blob/master/lib/thor/actions.rb#L207).
|
|
238
|
+
This means you can extend and change the instance to match your needs.
|
|
239
|
+
|
|
240
|
+
For example by overwriting the `source_paths` method to contain the
|
|
241
|
+
location of your template. Now methods like `copy_file` will accept
|
|
242
|
+
relative paths to your template's location.
|
|
243
|
+
|
|
244
|
+
```ruby
|
|
245
|
+
def source_paths
|
|
246
|
+
[File.expand_path(File.dirname(__FILE__))]
|
|
247
|
+
end
|
|
248
|
+
```
|
|
@@ -5,7 +5,6 @@ This guide covers Rails integration with Rack and interfacing with other Rack co
|
|
|
5
5
|
|
|
6
6
|
After reading this guide, you will know:
|
|
7
7
|
|
|
8
|
-
* How to create Rails Metal applications.
|
|
9
8
|
* How to use Rack Middlewares in your Rails applications.
|
|
10
9
|
* Action Pack's internal Middleware stack.
|
|
11
10
|
* How to define a custom Middleware stack.
|
|
@@ -28,10 +27,9 @@ Rails on Rack
|
|
|
28
27
|
|
|
29
28
|
### Rails Application's Rack Object
|
|
30
29
|
|
|
31
|
-
`
|
|
30
|
+
`Rails.application` is the primary Rack application object of a Rails
|
|
32
31
|
application. Any Rack compliant web server should be using
|
|
33
|
-
`
|
|
34
|
-
application. `Rails.application` refers to the same application object.
|
|
32
|
+
`Rails.application` object to serve a Rails application.
|
|
35
33
|
|
|
36
34
|
### `rails server`
|
|
37
35
|
|
|
@@ -82,9 +80,9 @@ To use `rackup` instead of Rails' `rails server`, you can put the following insi
|
|
|
82
80
|
|
|
83
81
|
```ruby
|
|
84
82
|
# Rails.root/config.ru
|
|
85
|
-
require ::File.expand_path('../config/environment',
|
|
83
|
+
require ::File.expand_path('../config/environment', __FILE__)
|
|
86
84
|
|
|
87
|
-
use Rack::Debugger
|
|
85
|
+
use Rails::Rack::Debugger
|
|
88
86
|
use Rack::ContentLength
|
|
89
87
|
run Rails.application
|
|
90
88
|
```
|
|
@@ -113,12 +111,13 @@ NOTE: `ActionDispatch::MiddlewareStack` is Rails equivalent of `Rack::Builder`,
|
|
|
113
111
|
Rails has a handy rake task for inspecting the middleware stack in use:
|
|
114
112
|
|
|
115
113
|
```bash
|
|
116
|
-
$ rake middleware
|
|
114
|
+
$ bin/rake middleware
|
|
117
115
|
```
|
|
118
116
|
|
|
119
117
|
For a freshly generated Rails application, this might produce something like:
|
|
120
118
|
|
|
121
119
|
```ruby
|
|
120
|
+
use Rack::Sendfile
|
|
122
121
|
use ActionDispatch::Static
|
|
123
122
|
use Rack::Lock
|
|
124
123
|
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x000000029a0838>
|
|
@@ -131,6 +130,7 @@ use ActionDispatch::DebugExceptions
|
|
|
131
130
|
use ActionDispatch::RemoteIp
|
|
132
131
|
use ActionDispatch::Reloader
|
|
133
132
|
use ActionDispatch::Callbacks
|
|
133
|
+
use ActiveRecord::Migration::CheckPending
|
|
134
134
|
use ActiveRecord::ConnectionAdapters::ConnectionManagement
|
|
135
135
|
use ActiveRecord::QueryCache
|
|
136
136
|
use ActionDispatch::Cookies
|
|
@@ -140,10 +140,10 @@ use ActionDispatch::ParamsParser
|
|
|
140
140
|
use Rack::Head
|
|
141
141
|
use Rack::ConditionalGet
|
|
142
142
|
use Rack::ETag
|
|
143
|
-
run
|
|
143
|
+
run Rails.application.routes
|
|
144
144
|
```
|
|
145
145
|
|
|
146
|
-
|
|
146
|
+
The default middlewares shown here (and some others) are each summarized in the [Internal Middlewares](#internal-middleware-stack) section, below.
|
|
147
147
|
|
|
148
148
|
### Configuring Middleware Stack
|
|
149
149
|
|
|
@@ -181,27 +181,26 @@ You can swap an existing middleware in the middleware stack using `config.middle
|
|
|
181
181
|
config.middleware.swap ActionDispatch::ShowExceptions, Lifo::ShowExceptions
|
|
182
182
|
```
|
|
183
183
|
|
|
184
|
-
####
|
|
184
|
+
#### Deleting a Middleware
|
|
185
185
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
Append following lines to your application configuration:
|
|
186
|
+
Add the following lines to your application configuration:
|
|
189
187
|
|
|
190
188
|
```ruby
|
|
191
189
|
# config/application.rb
|
|
192
190
|
config.middleware.delete "Rack::Lock"
|
|
193
191
|
```
|
|
194
192
|
|
|
195
|
-
And now if you inspect the middleware stack, you'll find that `Rack::Lock`
|
|
193
|
+
And now if you inspect the middleware stack, you'll find that `Rack::Lock` is
|
|
194
|
+
not a part of it.
|
|
196
195
|
|
|
197
196
|
```bash
|
|
198
|
-
$ rake middleware
|
|
197
|
+
$ bin/rake middleware
|
|
199
198
|
(in /Users/lifo/Rails/blog)
|
|
200
199
|
use ActionDispatch::Static
|
|
201
200
|
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x00000001c304c8>
|
|
202
201
|
use Rack::Runtime
|
|
203
202
|
...
|
|
204
|
-
run
|
|
203
|
+
run Rails.application.routes
|
|
205
204
|
```
|
|
206
205
|
|
|
207
206
|
If you want to remove session related middleware, do the following:
|
|
@@ -224,116 +223,100 @@ config.middleware.delete "Rack::MethodOverride"
|
|
|
224
223
|
|
|
225
224
|
Much of Action Controller's functionality is implemented as Middlewares. The following list explains the purpose of each of them:
|
|
226
225
|
|
|
227
|
-
|
|
226
|
+
**`Rack::Sendfile`**
|
|
228
227
|
|
|
229
228
|
* Sets server specific X-Sendfile header. Configure this via `config.action_dispatch.x_sendfile_header` option.
|
|
230
229
|
|
|
231
|
-
|
|
230
|
+
**`ActionDispatch::Static`**
|
|
232
231
|
|
|
233
232
|
* Used to serve static assets. Disabled if `config.serve_static_assets` is `false`.
|
|
234
233
|
|
|
235
|
-
|
|
234
|
+
**`Rack::Lock`**
|
|
236
235
|
|
|
237
236
|
* Sets `env["rack.multithread"]` flag to `false` and wraps the application within a Mutex.
|
|
238
237
|
|
|
239
|
-
|
|
238
|
+
**`ActiveSupport::Cache::Strategy::LocalCache::Middleware`**
|
|
240
239
|
|
|
241
240
|
* Used for memory caching. This cache is not thread safe.
|
|
242
241
|
|
|
243
|
-
|
|
242
|
+
**`Rack::Runtime`**
|
|
244
243
|
|
|
245
244
|
* Sets an X-Runtime header, containing the time (in seconds) taken to execute the request.
|
|
246
245
|
|
|
247
|
-
|
|
246
|
+
**`Rack::MethodOverride`**
|
|
248
247
|
|
|
249
248
|
* Allows the method to be overridden if `params[:_method]` is set. This is the middleware which supports the PUT and DELETE HTTP method types.
|
|
250
249
|
|
|
251
|
-
|
|
250
|
+
**`ActionDispatch::RequestId`**
|
|
252
251
|
|
|
253
252
|
* Makes a unique `X-Request-Id` header available to the response and enables the `ActionDispatch::Request#uuid` method.
|
|
254
253
|
|
|
255
|
-
|
|
254
|
+
**`Rails::Rack::Logger`**
|
|
256
255
|
|
|
257
256
|
* Notifies the logs that the request has began. After request is complete, flushes all the logs.
|
|
258
257
|
|
|
259
|
-
|
|
258
|
+
**`ActionDispatch::ShowExceptions`**
|
|
260
259
|
|
|
261
260
|
* Rescues any exception returned by the application and calls an exceptions app that will wrap it in a format for the end user.
|
|
262
261
|
|
|
263
|
-
|
|
262
|
+
**`ActionDispatch::DebugExceptions`**
|
|
264
263
|
|
|
265
264
|
* Responsible for logging exceptions and showing a debugging page in case the request is local.
|
|
266
265
|
|
|
267
|
-
|
|
266
|
+
**`ActionDispatch::RemoteIp`**
|
|
268
267
|
|
|
269
268
|
* Checks for IP spoofing attacks.
|
|
270
269
|
|
|
271
|
-
|
|
270
|
+
**`ActionDispatch::Reloader`**
|
|
272
271
|
|
|
273
272
|
* Provides prepare and cleanup callbacks, intended to assist with code reloading during development.
|
|
274
273
|
|
|
275
|
-
|
|
274
|
+
**`ActionDispatch::Callbacks`**
|
|
276
275
|
|
|
277
276
|
* Runs the prepare callbacks before serving the request.
|
|
278
277
|
|
|
279
|
-
|
|
278
|
+
**`ActiveRecord::Migration::CheckPending`**
|
|
279
|
+
|
|
280
|
+
* Checks pending migrations and raises `ActiveRecord::PendingMigrationError` if any migrations are pending.
|
|
281
|
+
|
|
282
|
+
**`ActiveRecord::ConnectionAdapters::ConnectionManagement`**
|
|
280
283
|
|
|
281
284
|
* Cleans active connections after each request, unless the `rack.test` key in the request environment is set to `true`.
|
|
282
285
|
|
|
283
|
-
|
|
286
|
+
**`ActiveRecord::QueryCache`**
|
|
284
287
|
|
|
285
288
|
* Enables the Active Record query cache.
|
|
286
289
|
|
|
287
|
-
|
|
290
|
+
**`ActionDispatch::Cookies`**
|
|
288
291
|
|
|
289
292
|
* Sets cookies for the request.
|
|
290
293
|
|
|
291
|
-
|
|
294
|
+
**`ActionDispatch::Session::CookieStore`**
|
|
292
295
|
|
|
293
296
|
* Responsible for storing the session in cookies.
|
|
294
297
|
|
|
295
|
-
|
|
298
|
+
**`ActionDispatch::Flash`**
|
|
296
299
|
|
|
297
300
|
* Sets up the flash keys. Only available if `config.action_controller.session_store` is set to a value.
|
|
298
301
|
|
|
299
|
-
|
|
302
|
+
**`ActionDispatch::ParamsParser`**
|
|
300
303
|
|
|
301
304
|
* Parses out parameters from the request into `params`.
|
|
302
305
|
|
|
303
|
-
|
|
306
|
+
**`ActionDispatch::Head`**
|
|
304
307
|
|
|
305
308
|
* Converts HEAD requests to `GET` requests and serves them as so.
|
|
306
309
|
|
|
307
|
-
|
|
310
|
+
**`Rack::ConditionalGet`**
|
|
308
311
|
|
|
309
312
|
* Adds support for "Conditional `GET`" so that server responds with nothing if page wasn't changed.
|
|
310
313
|
|
|
311
|
-
|
|
314
|
+
**`Rack::ETag`**
|
|
312
315
|
|
|
313
316
|
* Adds ETag header on all String bodies. ETags are used to validate cache.
|
|
314
317
|
|
|
315
318
|
TIP: It's possible to use any of the above middlewares in your custom Rack stack.
|
|
316
319
|
|
|
317
|
-
### Using Rack Builder
|
|
318
|
-
|
|
319
|
-
The following shows how to replace use `Rack::Builder` instead of the Rails supplied `MiddlewareStack`.
|
|
320
|
-
|
|
321
|
-
<strong>Clear the existing Rails middleware stack</strong>
|
|
322
|
-
|
|
323
|
-
```ruby
|
|
324
|
-
# config/application.rb
|
|
325
|
-
config.middleware.clear
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
<br />
|
|
329
|
-
<strong>Add a `config.ru` file to `Rails.root`</strong>
|
|
330
|
-
|
|
331
|
-
```ruby
|
|
332
|
-
# config.ru
|
|
333
|
-
use MyOwnStackFromScratch
|
|
334
|
-
run Rails.application
|
|
335
|
-
```
|
|
336
|
-
|
|
337
320
|
Resources
|
|
338
321
|
---------
|
|
339
322
|
|