mirror of https://github.com/rails/rails
Merge branch 'master' of github.com:lifo/docrails
Conflicts: guides/source/upgrading_ruby_on_rails.md
This commit is contained in:
commit
0c145448c4
|
@ -2,7 +2,7 @@ module ActionDispatch
|
|||
# This middleware calculates the IP address of the remote client that is
|
||||
# making the request. It does this by checking various headers that could
|
||||
# contain the address, and then picking the last-set address that is not
|
||||
# on the list of trusted IPs. This follows the precendent set by e.g.
|
||||
# on the list of trusted IPs. This follows the precedent set by e.g.
|
||||
# {the Tomcat server}[https://issues.apache.org/bugzilla/show_bug.cgi?id=50453],
|
||||
# with {reasoning explained at length}[http://blog.gingerlime.com/2012/rails-ip-spoofing-vulnerabilities-and-protection]
|
||||
# by @gingerlime. A more detailed explanation of the algorithm is given
|
||||
|
|
|
@ -13,7 +13,7 @@ module ActionView
|
|||
# elements. All of the select-type methods share a number of common options that are as follows:
|
||||
#
|
||||
# * <tt>:prefix</tt> - overwrites the default prefix of "date" used for the select names. So specifying "birthday"
|
||||
# would give \birthday[month] instead of \date[month] if passed to the <tt>select_month</tt> method.
|
||||
# would give \birthday[month] instead of \date[month] if passed to the <tt>select_month</tt> method.
|
||||
# * <tt>:include_blank</tt> - set to true if it should be possible to set an empty date.
|
||||
# * <tt>:discard_type</tt> - set to true if you want to discard the type part of the select name. If set to true,
|
||||
# the <tt>select_month</tt> method would use simply "date" (which can be overwritten using <tt>:prefix</tt>) instead
|
||||
|
|
|
@ -241,13 +241,13 @@ module ActionView
|
|||
# # </div>
|
||||
# # </form>"
|
||||
#
|
||||
# <%= button_to "New", action: "new", form_class: "new-thing" %>
|
||||
# <%= button_to "New", { action: "new" }, form_class: "new-thing" %>
|
||||
# # => "<form method="post" action="/controller/new" class="new-thing">
|
||||
# # <div><input value="New" type="submit" /></div>
|
||||
# # </form>"
|
||||
#
|
||||
#
|
||||
# <%= button_to "Create", action: "create", remote: true, form: { "data-type" => "json" } %>
|
||||
# <%= button_to "Create", { action: "create" }, remote: true, form: { "data-type" => "json" } %>
|
||||
# # => "<form method="post" action="/images/create" class="button_to" data-remote="true" data-type="json">
|
||||
# # <div>
|
||||
# # <input value="Create" type="submit" />
|
||||
|
|
|
@ -60,6 +60,9 @@ module ActiveModel
|
|||
# end
|
||||
# end
|
||||
#
|
||||
# Note that the validator is initialized only once for the whole application
|
||||
# lifecycle, and not on each validation run.
|
||||
#
|
||||
# The easiest way to add custom validators for validating individual attributes
|
||||
# is with the convenient <tt>ActiveModel::EachValidator</tt>.
|
||||
#
|
||||
|
|
|
@ -13,6 +13,12 @@ module ActiveRecord
|
|||
#
|
||||
# Person.count(:age, distinct: true)
|
||||
# # => counts the number of different age values
|
||||
#
|
||||
# If +count+ is used with +group+, it returns a Hash whose keys represent the aggregated column,
|
||||
# and the values are the respective amounts:
|
||||
#
|
||||
# Person.group(:city).count
|
||||
# # => { 'Rome' => 5, 'Paris' => 3 }
|
||||
def count(column_name = nil, options = {})
|
||||
column_name, options = nil, column_name if column_name.is_a?(Hash)
|
||||
calculate(:count, column_name, options)
|
||||
|
|
|
@ -189,6 +189,16 @@ module ActiveRecord
|
|||
# Model.select(:field, :other_field, :and_one_more)
|
||||
# # => [#<Model field: "value", other_field: "value", and_one_more: "value">]
|
||||
#
|
||||
# You can also use one or more strings, which will be used unchanged as SELECT fields.
|
||||
#
|
||||
# Model.select('field AS field_one', 'other_field AS field_two')
|
||||
# # => [#<Model field: "value", other_field: "value">]
|
||||
#
|
||||
# If an alias was specified, it will be accessible from the resulting objects:
|
||||
#
|
||||
# Model.select('field AS field_one').first.field_one
|
||||
# # => "value"
|
||||
#
|
||||
# Accessing attributes of an object that do not have fields retrieved by a select
|
||||
# will throw <tt>ActiveModel::MissingAttributeError</tt>:
|
||||
#
|
||||
|
@ -220,6 +230,9 @@ module ActiveRecord
|
|||
#
|
||||
# User.group(:name)
|
||||
# => [#<User id: 3, name: "Foo", ...>, #<User id: 2, name: "Oscar", ...>]
|
||||
#
|
||||
# User.group('name AS grouped_name, age')
|
||||
# => [#<User id: 3, name: "Foo", age: 21, ...>, #<User id: 2, name: "Oscar", age: 21, ...>, #<User id: 5, name: "Foo", age: 23, ...>]
|
||||
def group(*args)
|
||||
args.blank? ? self : spawn.group!(*args)
|
||||
end
|
||||
|
@ -292,6 +305,11 @@ module ActiveRecord
|
|||
#
|
||||
# User.joins(:posts)
|
||||
# => SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."user_id" = "users"."id"
|
||||
#
|
||||
# You can use strings in order to customize your joins:
|
||||
#
|
||||
# User.joins("LEFT JOIN bookmarks ON bookmarks.bookmarkable_type = 'Post' AND bookmarks.user_id = users.id")
|
||||
# => SELECT "users".* FROM "users" LEFT JOIN bookmarks ON bookmarks.bookmarkable_type = 'Post' AND bookmarks.user_id = users.id
|
||||
def joins(*args)
|
||||
args.compact.blank? ? self : spawn.joins!(*args.flatten)
|
||||
end
|
||||
|
|
|
@ -29,9 +29,9 @@ class Class
|
|||
#
|
||||
# class Foo; end
|
||||
# class Bar < Foo; end
|
||||
# class Baz < Foo; end
|
||||
# class Baz < Bar; end
|
||||
#
|
||||
# Foo.subclasses # => [Baz, Bar]
|
||||
# Foo.subclasses # => [Bar]
|
||||
def subclasses
|
||||
subclasses, chain = [], descendants
|
||||
chain.each do |k|
|
||||
|
|
|
@ -618,6 +618,35 @@ class GoodnessValidator < ActiveModel::Validator
|
|||
end
|
||||
```
|
||||
|
||||
Note that the validator will be initialized *only once* for the whole application
|
||||
life cycle, and not on each validation run, so be careful about using instance
|
||||
variables inside it.
|
||||
|
||||
If your validator is complex enough that you want instance variables, you can
|
||||
easily use a plain old Ruby object instead:
|
||||
|
||||
```ruby
|
||||
class Person < ActiveRecord::Base
|
||||
validate do |person|
|
||||
GoodnessValidator.new(person).validate
|
||||
end
|
||||
end
|
||||
|
||||
class GoodnessValidator
|
||||
def initialize(person)
|
||||
@person = person
|
||||
end
|
||||
|
||||
def validate
|
||||
if some_complex_condition_involving_ivars_and_private_methods?
|
||||
@person.errors[:base] << "This person is evil"
|
||||
end
|
||||
end
|
||||
|
||||
# …
|
||||
end
|
||||
```
|
||||
|
||||
### `validates_each`
|
||||
|
||||
This helper validates attributes against a block. It doesn't have a predefined
|
||||
|
|
|
@ -85,7 +85,7 @@ You can also run any single test separately:
|
|||
$ ARCONN=sqlite3 ruby -Itest test/cases/associations/has_many_associations_test.rb
|
||||
```
|
||||
|
||||
You can invoke `test_jdbcmysql`, `test_jdbcsqlite3` or `test_jdbcpostgresql` also. See the file `activerecord/RUNNING_UNIT_TESTS` for information on running more targeted database tests, or the file `ci/travis.rb` for the test suite run by the continuous integration server.
|
||||
You can invoke `test_jdbcmysql`, `test_jdbcsqlite3` or `test_jdbcpostgresql` also. See the file `activerecord/RUNNING_UNIT_TESTS.rdoc` for information on running more targeted database tests, or the file `ci/travis.rb` for the test suite run by the continuous integration server.
|
||||
|
||||
### Warnings
|
||||
|
||||
|
|
|
@ -774,10 +774,13 @@ You don't need to set up and run your tests by hand on a test-by-test basis. Rai
|
|||
Brief Note About `Test::Unit`
|
||||
-----------------------------
|
||||
|
||||
Ruby ships with a boat load of libraries. One little gem of a library is `Test::Unit`, a framework for unit testing in Ruby. All the basic assertions discussed above are actually defined in `Test::Unit::Assertions`. The class `ActiveSupport::TestCase` which we have been using in our unit and functional tests extends `Test::Unit::TestCase`, allowing
|
||||
Ruby ships with a boat load of libraries. Ruby 1.8 provides `Test::Unit`, a framework for unit testing in Ruby. All the basic assertions discussed above are actually defined in `Test::Unit::Assertions`. The class `ActiveSupport::TestCase` which we have been using in our unit and functional tests extends `Test::Unit::TestCase`, allowing
|
||||
us to use all of the basic assertions in our tests.
|
||||
|
||||
Ruby 1.9 introduced `MiniTest`, an updated version of `Test::Unit` which provides a backwards compatible API for `Test::Unit`. You could also use `MiniTest` in Ruby 1.8 by installing the `minitest` gem.
|
||||
|
||||
NOTE: For more information on `Test::Unit`, refer to [test/unit Documentation](http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/)
|
||||
For more information on `MiniTest`, refer to [Minitest](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/minitest/unit/rdoc/)
|
||||
|
||||
Setup and Teardown
|
||||
------------------
|
||||
|
|
|
@ -111,7 +111,7 @@ paintIt = (element, backgroundColor, textColor) ->
|
|||
element.style.color = textColor
|
||||
|
||||
$ ->
|
||||
$("a[data-color]").click ->
|
||||
$("a[data-background-color]").click ->
|
||||
backgroundColor = $(this).data("background-color")
|
||||
textColor = $(this).data("text-color")
|
||||
paintIt(this, backgroundColor, textColor)
|
||||
|
|
Loading…
Reference in New Issue