rails 4.0.13 → 4.1.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +20 -15
- data/guides/CHANGELOG.md +5 -74
- 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/challenge.png +0 -0
- 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/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/template_is_missing_posts_new.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/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 +2 -0
- data/guides/bug_report_templates/action_controller_master.rb +2 -0
- data/guides/bug_report_templates/active_record_gem.rb +1 -1
- data/guides/bug_report_templates/active_record_master.rb +2 -1
- data/guides/code/getting_started/Gemfile +1 -1
- data/guides/code/getting_started/app/assets/javascripts/application.js +1 -2
- data/guides/code/getting_started/config/environments/development.rb +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/rails_guides/helpers.rb +1 -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 +1 -2
- data/guides/source/3_1_release_notes.md +1 -1
- 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 +601 -0
- data/guides/source/_welcome.html.erb +1 -1
- data/guides/source/action_controller_overview.md +117 -31
- data/guides/source/action_mailer_basics.md +19 -19
- data/guides/source/action_view_overview.md +131 -12
- 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 +67 -39
- data/guides/source/active_record_validations.md +31 -31
- data/guides/source/active_support_core_extensions.md +63 -74
- data/guides/source/active_support_instrumentation.md +13 -4
- data/guides/source/api_documentation_guidelines.md +19 -5
- data/guides/source/asset_pipeline.md +544 -249
- data/guides/source/association_basics.md +81 -22
- data/guides/source/caching_with_rails.md +15 -6
- data/guides/source/command_line.md +28 -19
- data/guides/source/configuring.md +98 -50
- data/guides/source/contributing_to_ruby_on_rails.md +11 -11
- data/guides/source/credits.html.erb +2 -2
- data/guides/source/debugging_rails_applications.md +36 -5
- data/guides/source/development_dependencies_install.md +89 -8
- data/guides/source/documents.yaml +7 -1
- data/guides/source/engines.md +648 -224
- data/guides/source/form_helpers.md +56 -45
- data/guides/source/generators.md +7 -3
- data/guides/source/getting_started.md +379 -164
- data/guides/source/i18n.md +59 -23
- data/guides/source/index.html.erb +1 -1
- data/guides/source/initialization.md +153 -56
- data/guides/source/kindle/toc.html.erb +1 -1
- data/guides/source/layout.html.erb +3 -3
- data/guides/source/layouts_and_rendering.md +12 -11
- data/guides/source/maintenance_policy.md +4 -23
- data/guides/source/migrations.md +41 -37
- data/guides/source/nested_model_forms.md +3 -3
- data/guides/source/plugins.md +27 -23
- data/guides/source/rails_application_templates.md +25 -6
- data/guides/source/rails_on_rack.md +35 -51
- data/guides/source/routing.md +108 -99
- data/guides/source/ruby_on_rails_guides_guidelines.md +2 -2
- data/guides/source/security.md +33 -31
- data/guides/source/testing.md +37 -34
- data/guides/source/upgrading_ruby_on_rails.md +335 -16
- data/guides/source/working_with_javascript_in_rails.md +18 -10
- metadata +66 -39
- data/guides/assets/images/jaimeiniesta.jpg +0 -0
- data/guides/source/kindle/KINDLE.md +0 -26
@@ -162,8 +162,8 @@ Person.create(name: nil).valid? # => false
|
|
162
162
|
```
|
163
163
|
|
164
164
|
After Active Record has performed validations, any errors found can be accessed
|
165
|
-
through the `errors` instance method, which returns a collection of errors.
|
166
|
-
definition, an object is valid if this collection is empty after running
|
165
|
+
through the `errors.messages` instance method, which returns a collection of errors.
|
166
|
+
By definition, an object is valid if this collection is empty after running
|
167
167
|
validations.
|
168
168
|
|
169
169
|
Note that an object instantiated with `new` will not report errors even if it's
|
@@ -175,28 +175,28 @@ class Person < ActiveRecord::Base
|
|
175
175
|
end
|
176
176
|
|
177
177
|
>> p = Person.new
|
178
|
-
|
179
|
-
>> p.errors
|
180
|
-
|
178
|
+
# => #<Person id: nil, name: nil>
|
179
|
+
>> p.errors.messages
|
180
|
+
# => {}
|
181
181
|
|
182
182
|
>> p.valid?
|
183
|
-
|
184
|
-
>> p.errors
|
185
|
-
|
183
|
+
# => false
|
184
|
+
>> p.errors.messages
|
185
|
+
# => {name:["can't be blank"]}
|
186
186
|
|
187
187
|
>> p = Person.create
|
188
|
-
|
189
|
-
>> p.errors
|
190
|
-
|
188
|
+
# => #<Person id: nil, name: nil>
|
189
|
+
>> p.errors.messages
|
190
|
+
# => {name:["can't be blank"]}
|
191
191
|
|
192
192
|
>> p.save
|
193
|
-
|
193
|
+
# => false
|
194
194
|
|
195
195
|
>> p.save!
|
196
|
-
|
196
|
+
# => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
|
197
197
|
|
198
198
|
>> Person.create!
|
199
|
-
|
199
|
+
# => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
|
200
200
|
```
|
201
201
|
|
202
202
|
`invalid?` is simply the inverse of `valid?`. It triggers your validations,
|
@@ -357,7 +357,7 @@ given regular expression, which is specified using the `:with` option.
|
|
357
357
|
```ruby
|
358
358
|
class Product < ActiveRecord::Base
|
359
359
|
validates :legacy_code, format: { with: /\A[a-zA-Z]+\z/,
|
360
|
-
message: "
|
360
|
+
message: "only allows letters" }
|
361
361
|
end
|
362
362
|
```
|
363
363
|
|
@@ -526,7 +526,7 @@ If you validate the presence of an object associated via a `has_one` or
|
|
526
526
|
Since `false.blank?` is true, if you want to validate the presence of a boolean
|
527
527
|
field you should use `validates :field_name, inclusion: { in: [true, false] }`.
|
528
528
|
|
529
|
-
The default error message is _"can't be
|
529
|
+
The default error message is _"can't be blank"_.
|
530
530
|
|
531
531
|
### `absence`
|
532
532
|
|
@@ -616,6 +616,10 @@ The default error message is _"has already been taken"_.
|
|
616
616
|
This helper passes the record to a separate class for validation.
|
617
617
|
|
618
618
|
```ruby
|
619
|
+
class Person < ActiveRecord::Base
|
620
|
+
validates_with GoodnessValidator
|
621
|
+
end
|
622
|
+
|
619
623
|
class GoodnessValidator < ActiveModel::Validator
|
620
624
|
def validate(record)
|
621
625
|
if record.first_name == "Evil"
|
@@ -623,10 +627,6 @@ class GoodnessValidator < ActiveModel::Validator
|
|
623
627
|
end
|
624
628
|
end
|
625
629
|
end
|
626
|
-
|
627
|
-
class Person < ActiveRecord::Base
|
628
|
-
validates_with GoodnessValidator
|
629
|
-
end
|
630
630
|
```
|
631
631
|
|
632
632
|
NOTE: Errors added to `record.errors[:base]` relate to the state of the record
|
@@ -644,6 +644,10 @@ Like all other validations, `validates_with` takes the `:if`, `:unless` and
|
|
644
644
|
validator class as `options`:
|
645
645
|
|
646
646
|
```ruby
|
647
|
+
class Person < ActiveRecord::Base
|
648
|
+
validates_with GoodnessValidator, fields: [:first_name, :last_name]
|
649
|
+
end
|
650
|
+
|
647
651
|
class GoodnessValidator < ActiveModel::Validator
|
648
652
|
def validate(record)
|
649
653
|
if options[:fields].any?{|field| record.send(field) == "Evil" }
|
@@ -651,10 +655,6 @@ class GoodnessValidator < ActiveModel::Validator
|
|
651
655
|
end
|
652
656
|
end
|
653
657
|
end
|
654
|
-
|
655
|
-
class Person < ActiveRecord::Base
|
656
|
-
validates_with GoodnessValidator, fields: [:first_name, :last_name]
|
657
|
-
end
|
658
658
|
```
|
659
659
|
|
660
660
|
Note that the validator will be initialized *only once* for the whole application
|
@@ -682,7 +682,7 @@ class GoodnessValidator
|
|
682
682
|
end
|
683
683
|
end
|
684
684
|
|
685
|
-
#
|
685
|
+
# ...
|
686
686
|
end
|
687
687
|
```
|
688
688
|
|
@@ -734,8 +734,8 @@ class Topic < ActiveRecord::Base
|
|
734
734
|
validates :title, length: { is: 5 }, allow_blank: true
|
735
735
|
end
|
736
736
|
|
737
|
-
Topic.create(
|
738
|
-
Topic.create(
|
737
|
+
Topic.create(title: "").valid? # => true
|
738
|
+
Topic.create(title: nil).valid? # => true
|
739
739
|
```
|
740
740
|
|
741
741
|
### `:message`
|
@@ -781,7 +781,7 @@ end
|
|
781
781
|
Person.new.valid? # => ActiveModel::StrictValidationFailed: Name can't be blank
|
782
782
|
```
|
783
783
|
|
784
|
-
There is also an ability to pass custom exception to `:strict` option
|
784
|
+
There is also an ability to pass custom exception to `:strict` option.
|
785
785
|
|
786
786
|
```ruby
|
787
787
|
class Person < ActiveRecord::Base
|
@@ -869,7 +869,7 @@ should happen, an `Array` can be used. Moreover, you can apply both `:if` and
|
|
869
869
|
```ruby
|
870
870
|
class Computer < ActiveRecord::Base
|
871
871
|
validates :mouse, presence: true,
|
872
|
-
if: ["market.retail?", :desktop?]
|
872
|
+
if: ["market.retail?", :desktop?]
|
873
873
|
unless: Proc.new { |c| c.trackpad.present? }
|
874
874
|
end
|
875
875
|
```
|
@@ -990,12 +990,12 @@ end
|
|
990
990
|
|
991
991
|
person = Person.new
|
992
992
|
person.valid? # => false
|
993
|
-
person.errors
|
993
|
+
person.errors.messages
|
994
994
|
# => {:name=>["can't be blank", "is too short (minimum is 3 characters)"]}
|
995
995
|
|
996
996
|
person = Person.new(name: "John Doe")
|
997
997
|
person.valid? # => true
|
998
|
-
person.errors # =>
|
998
|
+
person.errors.messages # => {}
|
999
999
|
```
|
1000
1000
|
|
1001
1001
|
### `errors[]`
|
@@ -37,9 +37,10 @@ For every single method defined as a core extension this guide has a note that s
|
|
37
37
|
|
38
38
|
NOTE: Defined in `active_support/core_ext/object/blank.rb`.
|
39
39
|
|
40
|
-
That means that
|
40
|
+
That means that you can require it like this:
|
41
41
|
|
42
42
|
```ruby
|
43
|
+
require 'active_support'
|
43
44
|
require 'active_support/core_ext/object/blank'
|
44
45
|
```
|
45
46
|
|
@@ -52,6 +53,7 @@ The next level is to simply load all extensions to `Object`. As a rule of thumb,
|
|
52
53
|
Thus, to load all extensions to `Object` (including `blank?`):
|
53
54
|
|
54
55
|
```ruby
|
56
|
+
require 'active_support'
|
55
57
|
require 'active_support/core_ext/object'
|
56
58
|
```
|
57
59
|
|
@@ -60,6 +62,7 @@ require 'active_support/core_ext/object'
|
|
60
62
|
You may prefer just to load all core extensions, there is a file for that:
|
61
63
|
|
62
64
|
```ruby
|
65
|
+
require 'active_support'
|
63
66
|
require 'active_support/core_ext'
|
64
67
|
```
|
65
68
|
|
@@ -96,12 +99,13 @@ INFO: The predicate for strings uses the Unicode-aware character class `[:space:
|
|
96
99
|
|
97
100
|
WARNING: Note that numbers are not mentioned. In particular, 0 and 0.0 are **not** blank.
|
98
101
|
|
99
|
-
For example, this method from `
|
102
|
+
For example, this method from `ActionController::HttpAuthentication::Token::ControllerMethods` uses `blank?` for checking whether a token is present:
|
100
103
|
|
101
104
|
```ruby
|
102
|
-
def
|
103
|
-
|
104
|
-
|
105
|
+
def authenticate(controller, &login_procedure)
|
106
|
+
token, options = token_and_options(controller.request)
|
107
|
+
unless token.blank?
|
108
|
+
login_procedure.call(token, options)
|
105
109
|
end
|
106
110
|
end
|
107
111
|
```
|
@@ -166,7 +170,7 @@ NOTE: Defined in `active_support/core_ext/object/duplicable.rb`.
|
|
166
170
|
|
167
171
|
### `deep_dup`
|
168
172
|
|
169
|
-
The `deep_dup` method returns deep copy of a given object. Normally, when you `dup` an object that contains other objects,
|
173
|
+
The `deep_dup` method returns deep copy of a given object. Normally, when you `dup` an object that contains other objects, Ruby does not `dup` them, so it creates a shallow copy of the object. If you have an array with a string, for example, it will look like this:
|
170
174
|
|
171
175
|
```ruby
|
172
176
|
array = ['string']
|
@@ -175,14 +179,14 @@ duplicate = array.dup
|
|
175
179
|
duplicate.push 'another-string'
|
176
180
|
|
177
181
|
# the object was duplicated, so the element was added only to the duplicate
|
178
|
-
array
|
179
|
-
duplicate
|
182
|
+
array # => ['string']
|
183
|
+
duplicate # => ['string', 'another-string']
|
180
184
|
|
181
185
|
duplicate.first.gsub!('string', 'foo')
|
182
186
|
|
183
187
|
# first element was not duplicated, it will be changed in both arrays
|
184
|
-
array
|
185
|
-
duplicate
|
188
|
+
array # => ['foo']
|
189
|
+
duplicate # => ['foo', 'another-string']
|
186
190
|
```
|
187
191
|
|
188
192
|
As you can see, after duplicating the `Array` instance, we got another object, therefore we can modify it and the original object will stay unchanged. This is not true for array's elements, however. Since `dup` does not make deep copy, the string inside the array is still the same object.
|
@@ -195,8 +199,8 @@ duplicate = array.deep_dup
|
|
195
199
|
|
196
200
|
duplicate.first.gsub!('string', 'foo')
|
197
201
|
|
198
|
-
array
|
199
|
-
duplicate
|
202
|
+
array # => ['string']
|
203
|
+
duplicate # => ['foo']
|
200
204
|
```
|
201
205
|
|
202
206
|
If the object is not duplicable, `deep_dup` will just return it:
|
@@ -420,11 +424,9 @@ NOTE: Defined in `active_support/core_ext/object/with_options.rb`.
|
|
420
424
|
|
421
425
|
### JSON support
|
422
426
|
|
423
|
-
Active Support provides a better
|
427
|
+
Active Support provides a better implementation of `to_json` than the +json+ gem ordinarily provides for Ruby objects. This is because some classes, like +Hash+, +OrderedHash+, and +Process::Status+ need special handling in order to provide a proper JSON representation.
|
424
428
|
|
425
|
-
|
426
|
-
|
427
|
-
NOTE: Defined in `active_support/core_ext/object/to_json.rb`.
|
429
|
+
NOTE: Defined in `active_support/core_ext/object/json.rb`.
|
428
430
|
|
429
431
|
### Instance Variables
|
430
432
|
|
@@ -622,7 +624,7 @@ NOTE: Defined in `active_support/core_ext/module/attr_internal.rb`.
|
|
622
624
|
|
623
625
|
#### Module Attributes
|
624
626
|
|
625
|
-
The macros `mattr_reader`, `mattr_writer`, and `mattr_accessor` are
|
627
|
+
The macros `mattr_reader`, `mattr_writer`, and `mattr_accessor` are the same as the `cattr_*` macros defined for class. In fact, the `cattr_*` macros are just aliases for the `mattr_*` macros. Check [Class Attributes](#class-attributes).
|
626
628
|
|
627
629
|
For example, the dependencies mechanism uses them:
|
628
630
|
|
@@ -733,7 +735,7 @@ X.local_constants # => [:X1, :X2, :Y]
|
|
733
735
|
X::Y.local_constants # => [:Y1, :X1]
|
734
736
|
```
|
735
737
|
|
736
|
-
The names are returned as symbols.
|
738
|
+
The names are returned as symbols.
|
737
739
|
|
738
740
|
NOTE: Defined in `active_support/core_ext/module/introspection.rb`.
|
739
741
|
|
@@ -1062,7 +1064,7 @@ For convenience `class_attribute` also defines an instance predicate which is th
|
|
1062
1064
|
|
1063
1065
|
When `:instance_reader` is `false`, the instance predicate returns a `NoMethodError` just like the reader method.
|
1064
1066
|
|
1065
|
-
If you do not want the instance predicate,
|
1067
|
+
If you do not want the instance predicate, pass `instance_predicate: false` and it will not be defined.
|
1066
1068
|
|
1067
1069
|
NOTE: Defined in `active_support/core_ext/class/attribute.rb`
|
1068
1070
|
|
@@ -1091,6 +1093,15 @@ end
|
|
1091
1093
|
|
1092
1094
|
we can access `field_error_proc` in views.
|
1093
1095
|
|
1096
|
+
Also, you can pass a block to `cattr_*` to set up the attribute with a default value:
|
1097
|
+
|
1098
|
+
```ruby
|
1099
|
+
class MysqlAdapter < AbstractAdapter
|
1100
|
+
# Generates class methods to access @@emulate_booleans with default value of true.
|
1101
|
+
cattr_accessor(:emulate_booleans) { true }
|
1102
|
+
end
|
1103
|
+
```
|
1104
|
+
|
1094
1105
|
The generation of the reader instance method can be prevented by setting `:instance_reader` to `false` and the generation of the writer instance method can be prevented by setting `:instance_writer` to `false`. Generation of both methods can be prevented by setting `:instance_accessor` to `false`. In all cases, the value must be exactly `false` and not any false value.
|
1095
1106
|
|
1096
1107
|
```ruby
|
@@ -1108,7 +1119,7 @@ end
|
|
1108
1119
|
|
1109
1120
|
A model may find it useful to set `:instance_accessor` to `false` as a way to prevent mass-assignment from setting the attribute.
|
1110
1121
|
|
1111
|
-
NOTE: Defined in `active_support/core_ext/class/attribute_accessors.rb
|
1122
|
+
NOTE: Defined in `active_support/core_ext/module/attribute_accessors.rb`. `active_support/core_ext/class/attribute_accessors.rb` is deprecated and will be removed in Ruby on Rails 4.2.
|
1112
1123
|
|
1113
1124
|
### Subclasses & Descendants
|
1114
1125
|
|
@@ -1248,6 +1259,18 @@ Calling `to_s` on a safe string returns a safe string, but coercion with `to_str
|
|
1248
1259
|
|
1249
1260
|
Calling `dup` or `clone` on safe strings yields safe strings.
|
1250
1261
|
|
1262
|
+
### `remove`
|
1263
|
+
|
1264
|
+
The method `remove` will remove all occurrences of the pattern:
|
1265
|
+
|
1266
|
+
```ruby
|
1267
|
+
"Hello World".remove(/Hello /) => "World"
|
1268
|
+
```
|
1269
|
+
|
1270
|
+
There's also the destructive version `String#remove!`.
|
1271
|
+
|
1272
|
+
NOTE: Defined in `active_support/core_ext/string/filters.rb`.
|
1273
|
+
|
1251
1274
|
### `squish`
|
1252
1275
|
|
1253
1276
|
The method `squish` strips leading and trailing whitespace, and substitutes runs of whitespace with a single space each:
|
@@ -1258,7 +1281,7 @@ The method `squish` strips leading and trailing whitespace, and substitutes runs
|
|
1258
1281
|
|
1259
1282
|
There's also the destructive version `String#squish!`.
|
1260
1283
|
|
1261
|
-
Note that it handles both ASCII and Unicode whitespace.
|
1284
|
+
Note that it handles both ASCII and Unicode whitespace like mongolian vowel separator (U+180E).
|
1262
1285
|
|
1263
1286
|
NOTE: Defined in `active_support/core_ext/string/filters.rb`.
|
1264
1287
|
|
@@ -1380,8 +1403,6 @@ The third argument, `indent_empty_lines`, is a flag that says whether empty line
|
|
1380
1403
|
|
1381
1404
|
The `indent!` method performs indentation in-place.
|
1382
1405
|
|
1383
|
-
NOTE: Defined in `active_support/core_ext/string/indent.rb`.
|
1384
|
-
|
1385
1406
|
### Access
|
1386
1407
|
|
1387
1408
|
#### `at(position)`
|
@@ -1449,7 +1470,7 @@ The method `pluralize` returns the plural of its receiver:
|
|
1449
1470
|
|
1450
1471
|
As the previous example shows, Active Support knows some irregular plurals and uncountable nouns. Built-in rules can be extended in `config/initializers/inflections.rb`. That file is generated by the `rails` command and has instructions in comments.
|
1451
1472
|
|
1452
|
-
`pluralize` can also take an optional `count` parameter.
|
1473
|
+
`pluralize` can also take an optional `count` parameter. If `count == 1` the singular form will be returned. For any other value of `count` the plural form will be returned:
|
1453
1474
|
|
1454
1475
|
```ruby
|
1455
1476
|
"dude".pluralize(0) # => "dudes"
|
@@ -1533,7 +1554,7 @@ ActiveSupport::Inflector.inflections do |inflect|
|
|
1533
1554
|
inflect.acronym 'SSL'
|
1534
1555
|
end
|
1535
1556
|
|
1536
|
-
"SSLError".underscore.camelize
|
1557
|
+
"SSLError".underscore.camelize # => "SSLError"
|
1537
1558
|
```
|
1538
1559
|
|
1539
1560
|
`camelize` is aliased to `camelcase`.
|
@@ -1763,6 +1784,12 @@ The method `humanize` gives you a sensible name for display out of an attribute
|
|
1763
1784
|
"comments_count".humanize # => "Comments count"
|
1764
1785
|
```
|
1765
1786
|
|
1787
|
+
The capitalization of the first word can be turned off by setting the optional parameter `capitalize` to false:
|
1788
|
+
|
1789
|
+
```ruby
|
1790
|
+
"author_id".humanize(capitalize: false) # => "author"
|
1791
|
+
```
|
1792
|
+
|
1766
1793
|
The helper method `full_messages` uses `humanize` as a fallback to include attribute names:
|
1767
1794
|
|
1768
1795
|
```ruby
|
@@ -1989,7 +2016,7 @@ Produce a string representation of a number in human-readable words:
|
|
1989
2016
|
1234567890123456.to_s(:human) # => "1.23 Quadrillion"
|
1990
2017
|
```
|
1991
2018
|
|
1992
|
-
NOTE: Defined in `active_support/core_ext/numeric/
|
2019
|
+
NOTE: Defined in `active_support/core_ext/numeric/conversions.rb`.
|
1993
2020
|
|
1994
2021
|
Extensions to `Integer`
|
1995
2022
|
-----------------------
|
@@ -2432,7 +2459,7 @@ dup[1][2] = 4
|
|
2432
2459
|
array[1][2] == nil # => true
|
2433
2460
|
```
|
2434
2461
|
|
2435
|
-
NOTE: Defined in `active_support/core_ext/
|
2462
|
+
NOTE: Defined in `active_support/core_ext/object/deep_dup.rb`.
|
2436
2463
|
|
2437
2464
|
### Grouping
|
2438
2465
|
|
@@ -2658,45 +2685,7 @@ hash[:b][:e] == nil # => true
|
|
2658
2685
|
hash[:b][:d] == [3, 4] # => true
|
2659
2686
|
```
|
2660
2687
|
|
2661
|
-
NOTE: Defined in `active_support/core_ext/
|
2662
|
-
|
2663
|
-
### Diffing
|
2664
|
-
|
2665
|
-
The method `diff` returns a hash that represents a diff of the receiver and the argument with the following logic:
|
2666
|
-
|
2667
|
-
* Pairs `key`, `value` that exist in both hashes do not belong to the diff hash.
|
2668
|
-
|
2669
|
-
* If both hashes have `key`, but with different values, the pair in the receiver wins.
|
2670
|
-
|
2671
|
-
* The rest is just merged.
|
2672
|
-
|
2673
|
-
```ruby
|
2674
|
-
{a: 1}.diff(a: 1)
|
2675
|
-
# => {}, first rule
|
2676
|
-
|
2677
|
-
{a: 1}.diff(a: 2)
|
2678
|
-
# => {:a=>1}, second rule
|
2679
|
-
|
2680
|
-
{a: 1}.diff(b: 2)
|
2681
|
-
# => {:a=>1, :b=>2}, third rule
|
2682
|
-
|
2683
|
-
{a: 1, b: 2, c: 3}.diff(b: 1, c: 3, d: 4)
|
2684
|
-
# => {:a=>1, :b=>2, :d=>4}, all rules
|
2685
|
-
|
2686
|
-
{}.diff({}) # => {}
|
2687
|
-
{a: 1}.diff({}) # => {:a=>1}
|
2688
|
-
{}.diff(a: 1) # => {:a=>1}
|
2689
|
-
```
|
2690
|
-
|
2691
|
-
An important property of this diff hash is that you can retrieve the original hash by applying `diff` twice:
|
2692
|
-
|
2693
|
-
```ruby
|
2694
|
-
hash.diff(hash2).diff(hash2) == hash
|
2695
|
-
```
|
2696
|
-
|
2697
|
-
Diffing hashes may be useful for error messages related to expected option hashes for example.
|
2698
|
-
|
2699
|
-
NOTE: Defined in `active_support/core_ext/hash/diff.rb`.
|
2688
|
+
NOTE: Defined in `active_support/core_ext/object/deep_dup.rb`.
|
2700
2689
|
|
2701
2690
|
### Working with Keys
|
2702
2691
|
|
@@ -2724,14 +2713,14 @@ NOTE: Defined in `active_support/core_ext/hash/except.rb`.
|
|
2724
2713
|
The method `transform_keys` accepts a block and returns a hash that has applied the block operations to each of the keys in the receiver:
|
2725
2714
|
|
2726
2715
|
```ruby
|
2727
|
-
{nil => nil, 1 => 1, a: :a}.transform_keys{ |key| key.to_s.upcase }
|
2716
|
+
{nil => nil, 1 => 1, a: :a}.transform_keys { |key| key.to_s.upcase }
|
2728
2717
|
# => {"" => nil, "A" => :a, "1" => 1}
|
2729
2718
|
```
|
2730
2719
|
|
2731
2720
|
The result in case of collision is undefined:
|
2732
2721
|
|
2733
2722
|
```ruby
|
2734
|
-
{"a" => 1, a: 2}.transform_keys{ |key| key.to_s.upcase }
|
2723
|
+
{"a" => 1, a: 2}.transform_keys { |key| key.to_s.upcase }
|
2735
2724
|
# => {"A" => 2}, in my test, can't rely on this result though
|
2736
2725
|
```
|
2737
2726
|
|
@@ -2739,11 +2728,11 @@ This method may be useful for example to build specialized conversions. For inst
|
|
2739
2728
|
|
2740
2729
|
```ruby
|
2741
2730
|
def stringify_keys
|
2742
|
-
transform_keys{ |key| key.to_s }
|
2731
|
+
transform_keys { |key| key.to_s }
|
2743
2732
|
end
|
2744
2733
|
...
|
2745
2734
|
def symbolize_keys
|
2746
|
-
transform_keys{ |key| key.to_sym rescue key }
|
2735
|
+
transform_keys { |key| key.to_sym rescue key }
|
2747
2736
|
end
|
2748
2737
|
```
|
2749
2738
|
|
@@ -2752,7 +2741,7 @@ There's also the bang variant `transform_keys!` that applies the block operation
|
|
2752
2741
|
Besides that, one can use `deep_transform_keys` and `deep_transform_keys!` to perform the block operation on all the keys in the given hash and all the hashes nested into it. An example of the result is:
|
2753
2742
|
|
2754
2743
|
```ruby
|
2755
|
-
{nil => nil, 1 => 1, nested: {a: 3, 5 => 5}}.deep_transform_keys{ |key| key.to_s.upcase }
|
2744
|
+
{nil => nil, 1 => 1, nested: {a: 3, 5 => 5}}.deep_transform_keys { |key| key.to_s.upcase }
|
2756
2745
|
# => {""=>nil, "1"=>1, "NESTED"=>{"A"=>3, "5"=>5}}
|
2757
2746
|
```
|
2758
2747
|
|
@@ -3831,13 +3820,13 @@ def default_helper_module!
|
|
3831
3820
|
module_path = module_name.underscore
|
3832
3821
|
helper module_path
|
3833
3822
|
rescue MissingSourceFile => e
|
3834
|
-
raise e unless e.is_missing? "
|
3823
|
+
raise e unless e.is_missing? "helpers/#{module_path}_helper"
|
3835
3824
|
rescue NameError => e
|
3836
3825
|
raise e unless e.missing_name? "#{module_name}Helper"
|
3837
3826
|
end
|
3838
3827
|
```
|
3839
3828
|
|
3840
|
-
NOTE: Defined in `
|
3829
|
+
NOTE: Defined in `actionpack/lib/abstract_controller/helpers.rb`.
|
3841
3830
|
|
3842
3831
|
Extensions to `LoadError`
|
3843
3832
|
-------------------------
|
@@ -3860,4 +3849,4 @@ rescue NameError => e
|
|
3860
3849
|
end
|
3861
3850
|
```
|
3862
3851
|
|
3863
|
-
NOTE: Defined in `
|
3852
|
+
NOTE: Defined in `actionpack/lib/abstract_controller/helpers.rb`.
|