Skip to content

Commit f938019

Browse files
committed
Merge branch 'master' of github.com:lifo/docrails
Conflicts: activesupport/lib/active_support/core_ext/hash/slice.rb guides/source/active_support_core_extensions.md
2 parents cec66e5 + e842813 commit f938019

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+674
-482
lines changed

actionpack/lib/action_controller/metal/strong_parameters.rb

+42-17
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ def initialize(param) # :nodoc:
1919
end
2020
end
2121

22-
# == Action Controller Parameters
22+
# == Action Controller \Parameters
2323
#
2424
# Allows to choose which attributes should be whitelisted for mass updating
2525
# and thus prevent accidentally exposing that which shouldn’t be exposed.
2626
# Provides two methods for this purpose: #require and #permit. The former is
2727
# used to mark parameters as required. The latter is used to set the parameter
28-
# as permitted and limit which attributes should be allowed for mass updating.
28+
# as permitted and limit which attributes should be allowed for mass updating.
2929
#
3030
# params = ActionController::Parameters.new({
3131
# person: {
@@ -77,12 +77,12 @@ class Parameters < ActiveSupport::HashWithIndifferentAccess
7777
#
7878
# params = ActionController::Parameters.new(name: 'Francesco')
7979
# params.permitted? # => false
80-
# Person.new(params) # => ActiveModel::ForbiddenAttributesError
80+
# Person.new(params) # => ActiveModel::ForbiddenAttributesError
8181
#
8282
# ActionController::Parameters.permit_all_parameters = true
8383
#
8484
# params = ActionController::Parameters.new(name: 'Francesco')
85-
# params.permitted? # => true
85+
# params.permitted? # => true
8686
# Person.new(params) # => #<Person id: nil, name: "Francesco">
8787
def initialize(attributes = nil)
8888
super(attributes)
@@ -106,7 +106,7 @@ def permitted?
106106
# end
107107
#
108108
# params = ActionController::Parameters.new(name: 'Francesco')
109-
# params.permitted? # => false
109+
# params.permitted? # => false
110110
# Person.new(params) # => ActiveModel::ForbiddenAttributesError
111111
# params.permit!
112112
# params.permitted? # => true
@@ -125,7 +125,7 @@ def permit!
125125
# the parameter at the given +key+, otherwise raises an
126126
# <tt>ActionController::ParameterMissing</tt> error.
127127
#
128-
# ActionController::Parameters.new(person: { name: 'Francesco' }).require(:person)
128+
# ActionController::Parameters.new(person: { name: 'Francesco' }).require(:person)
129129
# # => {"name"=>"Francesco"}
130130
#
131131
# ActionController::Parameters.new(person: nil).require(:person)
@@ -141,21 +141,21 @@ def require(key)
141141
alias :required :require
142142

143143
# Returns a new <tt>ActionController::Parameters</tt> instance that
144-
# includes only the given +filters+ and sets the +permitted+ for the
145-
# object to +true+. This is useful for limiting which attributes
144+
# includes only the given +filters+ and sets the +permitted+ attribute
145+
# for the object to +true+. This is useful for limiting which attributes
146146
# should be allowed for mass updating.
147147
#
148148
# params = ActionController::Parameters.new(user: { name: 'Francesco', age: 22, role: 'admin' })
149149
# permitted = params.require(:user).permit(:name, :age)
150-
# permitted.permitted? # => true
150+
# permitted.permitted? # => true
151151
# permitted.has_key?(:name) # => true
152152
# permitted.has_key?(:age) # => true
153153
# permitted.has_key?(:role) # => false
154154
#
155155
# You can also use +permit+ on nested parameters, like:
156156
#
157157
# params = ActionController::Parameters.new({
158-
# person: {
158+
# person: {
159159
# name: 'Francesco',
160160
# age: 22,
161161
# pets: [{
@@ -168,7 +168,7 @@ def require(key)
168168
# permitted = params.permit(person: [ :name, { pets: :name } ])
169169
# permitted.permitted? # => true
170170
# permitted[:person][:name] # => "Francesco"
171-
# permitted[:person][:age] # => nil
171+
# permitted[:person][:age] # => nil
172172
# permitted[:person][:pets][0][:name] # => "Purplish"
173173
# permitted[:person][:pets][0][:category] # => nil
174174
#
@@ -229,7 +229,7 @@ def permit(*filters)
229229
# returns +nil+.
230230
#
231231
# params = ActionController::Parameters.new(person: { name: 'Francesco' })
232-
# params[:person] # => {"name"=>"Francesco"}
232+
# params[:person] # => {"name"=>"Francesco"}
233233
# params[:none] # => nil
234234
def [](key)
235235
convert_hashes_to_parameters(key, super)
@@ -242,10 +242,10 @@ def [](key)
242242
# is given, then that will be run and its result returned.
243243
#
244244
# params = ActionController::Parameters.new(person: { name: 'Francesco' })
245-
# params.fetch(:person) # => {"name"=>"Francesco"}
246-
# params.fetch(:none) => ActionController::ParameterMissing: param not found: none
245+
# params.fetch(:person) # => {"name"=>"Francesco"}
246+
# params.fetch(:none) # => ActionController::ParameterMissing: param not found: none
247247
# params.fetch(:none, 'Francesco') # => "Francesco"
248-
# params.fetch(:none) { 'Francesco' } # => "Francesco"
248+
# params.fetch(:none) { 'Francesco' } # => "Francesco"
249249
def fetch(key, *args)
250250
convert_hashes_to_parameters(key, super)
251251
rescue KeyError
@@ -303,7 +303,7 @@ def each_element(object)
303303
# == Strong \Parameters
304304
#
305305
# It provides an interface for protecting attributes from end-user
306-
# assignment. This makes Action Controller parameters forbidden
306+
# assignment. This makes Action Controller parameters forbidden
307307
# to be used in Active Model mass assignment until they have been
308308
# whitelisted.
309309
#
@@ -332,14 +332,39 @@ def each_element(object)
332332
#
333333
# private
334334
# # Using a private method to encapsulate the permissible parameters is
335-
# # just a good pattern since you'll be able to reuse the same permit
335+
# # just a good pattern since you'll be able to reuse the same permit
336336
# # list between create and update. Also, you can specialize this method
337337
# # with per-user checking of permissible attributes.
338338
# def person_params
339339
# params.require(:person).permit(:name, :age)
340340
# end
341341
# end
342342
#
343+
# In order to use <tt>accepts_nested_attribute_for</tt> with Strong \Parameters, you
344+
# will need to specify which nested attributes should be whitelisted.
345+
#
346+
# class Person
347+
# has_many :pets
348+
# accepts_nested_attributes_for :pets
349+
# end
350+
#
351+
# class PeopleController < ActionController::Base
352+
# def create
353+
# Person.create(person_params)
354+
# end
355+
#
356+
# ...
357+
#
358+
# private
359+
#
360+
# def person_params
361+
# # It's mandatory to specify the nested attributes that should be whitelisted.
362+
# # If you use `permit` with just the key that points to the nested attributes hash,
363+
# # it will return an empty hash.
364+
# params.require(:person).permit(:name, :age, pets_attributes: { :name, :category })
365+
# end
366+
# end
367+
#
343368
# See ActionController::Parameters.require and ActionController::Parameters.permit
344369
# for more information.
345370
module StrongParameters

activemodel/README.rdoc

+27-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ behavior out of the box:
7575

7676
* Tracking value changes
7777

78-
The ActiveModel::Dirty module allows for tracking attribute changes:
78+
class Person
79+
include ActiveModel::Dirty
80+
81+
attr_accessor :name
82+
end
7983

8084
person = Person.new
8185
person.name # => nil
@@ -152,9 +156,31 @@ behavior out of the box:
152156
ActiveModel::Serialization provides a standard interface for your object
153157
to provide +to_json+ or +to_xml+ serialization.
154158

159+
class SerialPerson
160+
include ActiveModel::Serialization
161+
162+
attr_accessor :name
163+
164+
def attributes
165+
{'name' => name}
166+
end
167+
end
168+
155169
s = SerialPerson.new
156170
s.serializable_hash # => {"name"=>nil}
171+
172+
class SerialPerson
173+
include ActiveModel::Serializers::JSON
174+
end
175+
176+
s = SerialPerson.new
157177
s.to_json # => "{\"name\":null}"
178+
179+
class SerialPerson
180+
include ActiveModel::Serializers::Xml
181+
end
182+
183+
s = SerialPerson.new
158184
s.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<serial-person...
159185

160186
{Learn more}[link:classes/ActiveModel/Serialization.html]

activemodel/lib/active_model/attribute_methods.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module ActiveModel
1111
# # => ActiveModel::MissingAttributeError: missing attribute: user_id
1212
class MissingAttributeError < NoMethodError
1313
end
14-
# == Active Model Attribute Methods
14+
# == Active \Model Attribute Methods
1515
#
1616
# <tt>ActiveModel::AttributeMethods</tt> provides a way to add prefixes and
1717
# suffixes to your methods as well as handling the creation of Active Record

activemodel/lib/active_model/callbacks.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'active_support/callbacks'
22

33
module ActiveModel
4-
# == Active Model Callbacks
4+
# == Active \Model \Callbacks
55
#
66
# Provides an interface for any class to have Active Record like callbacks.
77
#

activemodel/lib/active_model/conversion.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'active_support/inflector'
22

33
module ActiveModel
4-
# == Active Model Conversions
4+
# == Active \Model Conversions
55
#
66
# Handles default conversions: to_model, to_key, to_param, and to_partial_path.
77
#

activemodel/lib/active_model/dirty.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'active_support/core_ext/object/duplicable'
44

55
module ActiveModel
6-
# == Active Model Dirty
6+
# == Active \Model \Dirty
77
#
88
# Provides a way to track changes in your object in the same way as
99
# Active Record does.

activemodel/lib/active_model/errors.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require 'active_support/core_ext/string/inflections'
55

66
module ActiveModel
7-
# == Active Model Errors
7+
# == Active \Model \Errors
88
#
99
# Provides a modified +Hash+ that you can include in your object
1010
# for handling error messages and interacting with Action Pack helpers.

activemodel/lib/active_model/lint.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module ActiveModel
22
module Lint
3-
# == Active Model Lint Tests
3+
# == Active \Model \Lint \Tests
44
#
5-
# You can test whether an object is compliant with the Active Model API by
5+
# You can test whether an object is compliant with the Active \Model API by
66
# including <tt>ActiveModel::Lint::Tests</tt> in your TestCase. It will
77
# include tests that tell you whether your object is fully compliant,
88
# or if not, which aspects of the API are not implemented.
@@ -71,7 +71,7 @@ def test_persisted?
7171
assert_boolean model.persisted?, "persisted?"
7272
end
7373

74-
# == Naming
74+
# == \Naming
7575
#
7676
# Model.model_name must return a string with some convenience methods:
7777
# <tt>:human</tt>, <tt>:singular</tt> and <tt>:plural</tt>. Check
@@ -85,7 +85,7 @@ def test_model_naming
8585
assert model_name.plural.respond_to?(:to_str)
8686
end
8787

88-
# == Errors Testing
88+
# == \Errors Testing
8989
#
9090
# Returns an object that implements [](attribute) defined which returns an
9191
# Array of Strings that are the errors for the attribute in question.

activemodel/lib/active_model/model.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module ActiveModel
22

3-
# == Active Model Basic Model
3+
# == Active \Model Basic \Model
44
#
55
# Includes the required interface for an object to interact with
66
# <tt>ActionPack</tt>, using different <tt>ActiveModel</tt> modules.

activemodel/lib/active_model/naming.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def _singularize(string, replacement='_')
195195
end
196196
end
197197

198-
# == Active Model Naming
198+
# == Active \Model \Naming
199199
#
200200
# Creates a +model_name+ method on your object.
201201
#

activemodel/lib/active_model/observing.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
require 'active_support/descendants_tracker'
99

1010
module ActiveModel
11-
# == Active Model Observers Activation
11+
# == Active \Model Observers Activation
1212
module Observing
1313
extend ActiveSupport::Concern
1414

@@ -229,7 +229,7 @@ def notify_observers(method, *extra_args)
229229
end
230230
end
231231

232-
# == Active Model Observers
232+
# == Active \Model Observers
233233
#
234234
# Observer classes respond to life cycle callbacks to implement trigger-like
235235
# behavior outside the original class. This is a great way to reduce the
@@ -257,7 +257,7 @@ def notify_observers(method, *extra_args)
257257
#
258258
# This Observer uses logger to log when specific callbacks are triggered.
259259
#
260-
# == Observing a class that can't be inferred
260+
# == \Observing a class that can't be inferred
261261
#
262262
# Observers will by default be mapped to the class with which they share a
263263
# name. So <tt>CommentObserver</tt> will be tied to observing <tt>Comment</tt>,

activemodel/lib/active_model/railtie.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require "rails"
33

44
module ActiveModel
5-
class Railtie < Rails::Railtie
5+
class Railtie < Rails::Railtie # :nodoc:
66
config.eager_load_namespaces << ActiveModel
77
end
8-
end
8+
end

activemodel/lib/active_model/secure_password.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def has_secure_password(options = {})
4444
if options.fetch(:validations, true)
4545
validates_confirmation_of :password
4646
validates_presence_of :password, :on => :create
47-
47+
4848
before_create { raise "Password digest missing on new record" if password_digest.blank? }
4949
end
5050

@@ -68,7 +68,7 @@ module InstanceMethodsOnActivation
6868
# user = User.new(name: 'david', password: 'mUc3m00RsqyRe')
6969
# user.save
7070
# user.authenticate('notright') # => false
71-
#  user.authenticate('mUc3m00RsqyRe') # => user
71+
# user.authenticate('mUc3m00RsqyRe') # => user
7272
def authenticate(unencrypted_password)
7373
BCrypt::Password.new(password_digest) == unencrypted_password && self
7474
end
@@ -84,7 +84,7 @@ def authenticate(unencrypted_password)
8484
# user.password = nil
8585
# user.password_digest # => nil
8686
# user.password = 'mUc3m00RsqyRe'
87-
# user.password_digest # => "$2a$10$4LEA7r4YmNHtvlAvHhsYAeZmk/xeUVtMTYqwIvYY76EW5GUqDiP4."
87+
# user.password_digest # => "$2a$10$4LEA7r4YmNHtvlAvHhsYAeZmk/xeUVtMTYqwIvYY76EW5GUqDiP4."
8888
def password=(unencrypted_password)
8989
unless unencrypted_password.blank?
9090
@password = unencrypted_password

activemodel/lib/active_model/serialization.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require 'active_support/core_ext/hash/slice'
33

44
module ActiveModel
5-
# == Active Model Serialization
5+
# == Active \Model \Serialization
66
#
77
# Provides a basic serialization to a serializable_hash for your object.
88
#

activemodel/lib/active_model/translation.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module ActiveModel
22

3-
# == Active Model Translation
3+
# == Active \Model \Translation
44
#
55
# Provides integration between your object and the Rails internationalization
66
# (i18n) framework.

activemodel/lib/active_model/validations.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
module ActiveModel
99

10-
# == Active Model Validations
10+
# == Active \Model Validations
1111
#
1212
# Provides a full validation framework to your objects.
1313
#

activemodel/lib/active_model/validations/acceptance.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module ActiveModel
22

3-
# == Active Model Acceptance Validator
3+
# == Active \Model Acceptance \Validator
44
module Validations
55
class AcceptanceValidator < EachValidator #:nodoc:
66
def initialize(options)

0 commit comments

Comments
 (0)