mirror of https://github.com/rails/rails
Merge remote-tracking branch 'docrails/master'
Conflicts: activesupport/lib/active_support/core_ext/hash/deep_merge.rb activesupport/lib/active_support/core_ext/hash/keys.rb
This commit is contained in:
commit
17c29a0df0
|
@ -74,7 +74,7 @@ Or you can just chain the methods together like:
|
|||
|
||||
== Setting defaults
|
||||
|
||||
It is possible to set default values that will be used in every method in your Action Mailer class. To implement this functionality, you just call the public class method <tt>default</tt> which you get for free from ActionMailer::Base. This method accepts a Hash as the parameter. You can use any of the headers e-mail messages has, like <tt>:from</tt> as the key. You can also pass in a string as the key, like "Content-Type", but Action Mailer does this out of the box for you, so you won't need to worry about that. Finally, it is also possible to pass in a Proc that will get evaluated when it is needed.
|
||||
It is possible to set default values that will be used in every method in your Action Mailer class. To implement this functionality, you just call the public class method <tt>default</tt> which you get for free from <tt>ActionMailer::Base</tt>. This method accepts a Hash as the parameter. You can use any of the headers e-mail messages has, like <tt>:from</tt> as the key. You can also pass in a string as the key, like "Content-Type", but Action Mailer does this out of the box for you, so you won't need to worry about that. Finally, it is also possible to pass in a Proc that will get evaluated when it is needed.
|
||||
|
||||
Note that every value you set with this method will get overwritten if you use the same key in your mailer method.
|
||||
|
||||
|
|
|
@ -4,8 +4,8 @@ class Hash
|
|||
# h1 = { x: { y: [4, 5, 6] }, z: [7, 8, 9] }
|
||||
# h2 = { x: { y: [7, 8, 9] }, z: 'xyz' }
|
||||
#
|
||||
# h1.deep_merge(h2) # => {:x=>{:y=>[7, 8, 9]}, :z=>"xyz"}
|
||||
# h2.deep_merge(h1) # => {:x=>{:y=>[4, 5, 6]}, :z=>[7, 8, 9]}
|
||||
# h1.deep_merge(h2) # => {x: {y: [7, 8, 9]}, z: "xyz"}
|
||||
# h2.deep_merge(h1) # => {x: {y: [4, 5, 6]}, z: [7, 8, 9]}
|
||||
# h1.deep_merge(h2) { |key, old, new| Array.wrap(old) + Array.wrap(new) }
|
||||
# # => {:x=>{:y=>[4, 5, 6, 7, 8, 9]}, :z=>[7, 8, 9, "xyz"]}
|
||||
def deep_merge(other_hash, &block)
|
||||
|
|
|
@ -44,7 +44,7 @@ class Hash
|
|||
# hash = { 'name' => 'Rob', 'age' => '28' }
|
||||
#
|
||||
# hash.symbolize_keys
|
||||
# # => {"name"=>"Rob", "age"=>"28"}
|
||||
# # => { name: "Rob", age: "28" }
|
||||
def symbolize_keys
|
||||
transform_keys{ |key| key.to_sym rescue key }
|
||||
end
|
||||
|
|
|
@ -133,7 +133,7 @@ class BigDecimal
|
|||
# BigDecimal, it still has the chance to post-process the string and get the
|
||||
# real value.
|
||||
#
|
||||
# Use <tt>ActiveSupport.use_standard_json_big_decimal_format = true</tt> to
|
||||
# Use <tt>ActiveSupport.encode_big_decimal_as_string = true</tt> to
|
||||
# override this behavior.
|
||||
def as_json(options = nil) #:nodoc:
|
||||
if finite?
|
||||
|
|
|
@ -348,7 +348,7 @@ For most stores, this ID is used to look up the session data on the server, e.g.
|
|||
|
||||
The CookieStore can store around 4kB of data - much less than the others - but this is usually enough. Storing large amounts of data in the session is discouraged no matter which session store your application uses. You should especially avoid storing complex objects (anything other than basic Ruby objects, the most common example being model instances) in the session, as the server might not be able to reassemble them between requests, which will result in an error.
|
||||
|
||||
If your user sessions don't store critical data or don't need to be around for long periods (for instance if you just use the flash for messaging), you can consider using ActionDispatch::Session::CacheStore. This will store sessions using the cache implementation you have configured for your application. The advantage of this is that you can use your existing cache infrastructure for storing sessions without requiring any additional setup or administration. The downside, of course, is that the sessions will be ephemeral and could disappear at any time.
|
||||
If your user sessions don't store critical data or don't need to be around for long periods (for instance if you just use the flash for messaging), you can consider using `ActionDispatch::Session::CacheStore`. This will store sessions using the cache implementation you have configured for your application. The advantage of this is that you can use your existing cache infrastructure for storing sessions without requiring any additional setup or administration. The downside, of course, is that the sessions will be ephemeral and could disappear at any time.
|
||||
|
||||
Read more about session storage in the [Security Guide](security.html).
|
||||
|
||||
|
|
|
@ -1035,7 +1035,7 @@ If you found this guide useful, please consider recommending its authors on [wor
|
|||
Footnotes
|
||||
---------
|
||||
|
||||
[^1]: Or, to quote [Wikipedia](http://en.wikipedia.org/wiki/Internationalization_and_localization:) _"Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text."_
|
||||
[^1]: Or, to quote [Wikipedia](http://en.wikipedia.org/wiki/Internationalization_and_localization): _"Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text."_
|
||||
|
||||
[^2]: Other backends might allow or require to use other formats, e.g. a GetText backend might allow to read GetText files.
|
||||
|
||||
|
|
|
@ -126,7 +126,9 @@ A standard Rails application depends on several gems, specifically:
|
|||
|
||||
### `rails/commands.rb`
|
||||
|
||||
Once `config/boot.rb` has finished, the next file that is required is `rails/commands` which will execute a command based on the arguments passed in. In this case, the `ARGV` array simply contains `server` which is extracted into the `command` variable using these lines:
|
||||
Once `config/boot.rb` has finished, the next file that is required is
|
||||
`rails/commands`, which helps in expanding aliases. In the current case, the
|
||||
`ARGV` array simply contains `server` which will be passed over:
|
||||
|
||||
```ruby
|
||||
ARGV << '--help' if ARGV.empty?
|
||||
|
@ -142,31 +144,64 @@ aliases = {
|
|||
|
||||
command = ARGV.shift
|
||||
command = aliases[command] || command
|
||||
|
||||
require 'rails/commands/commands_tasks'
|
||||
|
||||
Rails::CommandsTasks.new(ARGV).run_command!(command)
|
||||
```
|
||||
|
||||
TIP: As you can see, an empty ARGV list will make Rails show the help
|
||||
snippet.
|
||||
|
||||
If we used `s` rather than `server`, Rails will use the `aliases` defined in the file and match them to their respective commands. With the `server` command, Rails will run this code:
|
||||
If we had used `s` rather than `server`, Rails would have used the `aliases`
|
||||
defined here to find the matching command.
|
||||
|
||||
### `rails/commands/command_tasks.rb`
|
||||
|
||||
When one types an incorrect rails command, the `run_command` is responsible for
|
||||
throwing an error message. If the command is valid, a method of the same name
|
||||
is called.
|
||||
|
||||
```ruby
|
||||
when 'server'
|
||||
# Change to the application's path if there is no config.ru file in current directory.
|
||||
# This allows us to run `rails server` from other directories, but still get
|
||||
# the main config.ru and properly set the tmp directory.
|
||||
Dir.chdir(File.expand_path('../../', APP_PATH)) unless File.exist?(File.expand_path("config.ru"))
|
||||
COMMAND_WHITELIST = %(plugin generate destroy console server dbconsole application runner new version help)
|
||||
|
||||
def run_command!(command)
|
||||
if COMMAND_WHITELIST.include?(command)
|
||||
send(command)
|
||||
else
|
||||
write_error_message(command)
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
With the `server` command, Rails will further run the following code:
|
||||
|
||||
```ruby
|
||||
def set_application_directory!
|
||||
Dir.chdir(File.expand_path('../../', APP_PATH)) unless
|
||||
File.exist?(File.expand_path("config.ru"))
|
||||
end
|
||||
|
||||
def server
|
||||
set_application_directory!
|
||||
require_command!("server")
|
||||
|
||||
require 'rails/commands/server'
|
||||
Rails::Server.new.tap do |server|
|
||||
# We need to require application after the server sets environment,
|
||||
# otherwise the --environment option given to the server won't propagate.
|
||||
require APP_PATH
|
||||
Dir.chdir(Rails.application.root)
|
||||
server.start
|
||||
end
|
||||
end
|
||||
|
||||
def require_command!(command)
|
||||
require "rails/commands/#{command}"
|
||||
end
|
||||
```
|
||||
|
||||
This file will change into the Rails root directory (a path two directories up from `APP_PATH` which points at `config/application.rb`), but only if the `config.ru` file isn't found. This then requires `rails/commands/server` which sets up the `Rails::Server` class.
|
||||
This file will change into the Rails root directory (a path two directories up
|
||||
from `APP_PATH` which points at `config/application.rb`), but only if the
|
||||
`config.ru` file isn't found. This then requires `rails/commands/server` which
|
||||
sets up the `Rails::Server` class.
|
||||
|
||||
```ruby
|
||||
require 'fileutils'
|
||||
|
@ -294,37 +329,43 @@ and it's free for you to change based on your needs.
|
|||
|
||||
### `Rails::Server#start`
|
||||
|
||||
After `config/application` is loaded, `server.start` is called. This method is defined like this:
|
||||
After `config/application` is loaded, `server.start` is called. This method is
|
||||
defined like this:
|
||||
|
||||
```ruby
|
||||
def start
|
||||
url = "#{options[:SSLEnable] ? 'https' : 'http'}://#{options[:Host]}:#{options[:Port]}"
|
||||
puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}"
|
||||
puts "=> Rails #{Rails.version} application starting in #{Rails.env} on #{url}"
|
||||
puts "=> Run `rails server -h` for more startup options"
|
||||
print_boot_information
|
||||
trap(:INT) { exit }
|
||||
puts "=> Ctrl-C to shutdown server" unless options[:daemonize]
|
||||
create_tmp_directories
|
||||
log_to_stdout if options[:log_stdout]
|
||||
|
||||
#Create required tmp directories if not found
|
||||
%w(cache pids sessions sockets).each do |dir_to_make|
|
||||
FileUtils.mkdir_p(Rails.root.join('tmp', dir_to_make))
|
||||
super
|
||||
...
|
||||
end
|
||||
|
||||
unless options[:daemonize]
|
||||
private
|
||||
|
||||
def print_boot_information
|
||||
...
|
||||
puts "=> Run `rails server -h` for more startup options"
|
||||
puts "=> Ctrl-C to shutdown server" unless options[:daemonize]
|
||||
end
|
||||
|
||||
def create_tmp_directories
|
||||
%w(cache pids sessions sockets).each do |dir_to_make|
|
||||
FileUtils.mkdir_p(File.join(Rails.root, 'tmp', dir_to_make))
|
||||
end
|
||||
end
|
||||
|
||||
def log_to_stdout
|
||||
wrapped_app # touch the app so the logger is set up
|
||||
|
||||
console = ActiveSupport::Logger.new($stdout)
|
||||
console.formatter = Rails.logger.formatter
|
||||
console.level = Rails.logger.level
|
||||
|
||||
Rails.logger.extend(ActiveSupport::Logger.broadcast(console))
|
||||
end
|
||||
|
||||
super
|
||||
ensure
|
||||
# The '-h' option calls exit before @options is set.
|
||||
# If we call 'options' with it unset, we get double help banners.
|
||||
puts 'Exiting' unless @options && options[:daemonize]
|
||||
end
|
||||
```
|
||||
|
||||
This is where the first output of the Rails initialization happens. This
|
||||
|
|
|
@ -182,18 +182,17 @@ You can swap an existing middleware in the middleware stack using `config.middle
|
|||
config.middleware.swap ActionDispatch::ShowExceptions, Lifo::ShowExceptions
|
||||
```
|
||||
|
||||
#### Middleware Stack is an Enumerable
|
||||
#### Deleting a Middleware
|
||||
|
||||
The middleware stack behaves just like a normal `Enumerable`. You can use any `Enumerable` methods to manipulate or interrogate the stack. The middleware stack also implements some `Array` methods including `[]`, `unshift` and `delete`. Methods described in the section above are just convenience methods.
|
||||
|
||||
Append following lines to your application configuration:
|
||||
Add the following lines to your application configuration:
|
||||
|
||||
```ruby
|
||||
# config/application.rb
|
||||
config.middleware.delete "Rack::Lock"
|
||||
```
|
||||
|
||||
And now if you inspect the middleware stack, you'll find that `Rack::Lock` will not be part of it.
|
||||
And now if you inspect the middleware stack, you'll find that `Rack::Lock` is
|
||||
not a part of it.
|
||||
|
||||
```bash
|
||||
$ rake middleware
|
||||
|
@ -319,26 +318,6 @@ Much of Action Controller's functionality is implemented as Middlewares. The fol
|
|||
|
||||
TIP: It's possible to use any of the above middlewares in your custom Rack stack.
|
||||
|
||||
### Using Rack Builder
|
||||
|
||||
The following shows how to replace use `Rack::Builder` instead of the Rails supplied `MiddlewareStack`.
|
||||
|
||||
<strong>Clear the existing Rails middleware stack</strong>
|
||||
|
||||
```ruby
|
||||
# config/application.rb
|
||||
config.middleware.clear
|
||||
```
|
||||
|
||||
<br>
|
||||
<strong>Add a `config.ru` file to `Rails.root`</strong>
|
||||
|
||||
```ruby
|
||||
# config.ru
|
||||
use MyOwnStackFromScratch
|
||||
run Rails.application
|
||||
```
|
||||
|
||||
Resources
|
||||
---------
|
||||
|
||||
|
|
|
@ -51,7 +51,7 @@ Use the same typography as in regular text:
|
|||
API Documentation Guidelines
|
||||
----------------------------
|
||||
|
||||
The guides and the API should be coherent and consistent where appropriate. Please have a look at these particular sections of the [API Documentation Guidelines](api_documentation_guidelines.html:)
|
||||
The guides and the API should be coherent and consistent where appropriate. Please have a look at these particular sections of the [API Documentation Guidelines](api_documentation_guidelines.html):
|
||||
|
||||
* [Wording](api_documentation_guidelines.html#wording)
|
||||
* [Example Code](api_documentation_guidelines.html#example-code)
|
||||
|
|
Loading…
Reference in New Issue