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
@@ -4,7 +4,7 @@
|
|
4
4
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
5
|
<head>
|
6
6
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
7
|
-
<meta name="viewport" content="width=device-width, initial-scale=1"
|
7
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
8
8
|
|
9
9
|
<title><%= yield(:page_title) || 'Ruby on Rails Guides' %></title>
|
10
10
|
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
|
@@ -48,7 +48,7 @@
|
|
48
48
|
<ul class="nav">
|
49
49
|
<li><a class="nav-item" href="index.html">Home</a></li>
|
50
50
|
<li class="guides-index guides-index-large">
|
51
|
-
<a href="index.html"
|
51
|
+
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Guides Index</a>
|
52
52
|
<div id="guides" class="clearfix" style="display: none;">
|
53
53
|
<hr />
|
54
54
|
<% ['L', 'R'].each do |position| %>
|
@@ -139,7 +139,7 @@
|
|
139
139
|
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
|
140
140
|
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
|
141
141
|
<script type="text/javascript">
|
142
|
-
SyntaxHighlighter.all()
|
142
|
+
SyntaxHighlighter.all();
|
143
143
|
$(guidesIndex.bind);
|
144
144
|
</script>
|
145
145
|
</body>
|
@@ -88,7 +88,7 @@ If we want to display the properties of all the books in our view, we can do so
|
|
88
88
|
<% end %>
|
89
89
|
</table>
|
90
90
|
|
91
|
-
<br
|
91
|
+
<br>
|
92
92
|
|
93
93
|
<%= link_to "New book", new_book_path %>
|
94
94
|
```
|
@@ -122,8 +122,7 @@ X-Runtime: 0.014297
|
|
122
122
|
Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
|
123
123
|
Cache-Control: no-cache
|
124
124
|
|
125
|
-
|
126
|
-
$
|
125
|
+
$
|
127
126
|
```
|
128
127
|
|
129
128
|
We see there is an empty response (no data after the `Cache-Control` line), but the request was successful because Rails has set the response to 200 OK. You can set the `:status` option on render to change this response. Rendering nothing can be useful for Ajax requests where all you want to send back to the browser is an acknowledgment that the request was completed.
|
@@ -137,7 +136,7 @@ If you want to render the view that corresponds to a different template within t
|
|
137
136
|
```ruby
|
138
137
|
def update
|
139
138
|
@book = Book.find(params[:id])
|
140
|
-
if @book.update(
|
139
|
+
if @book.update(book_params)
|
141
140
|
redirect_to(@book)
|
142
141
|
else
|
143
142
|
render "edit"
|
@@ -152,7 +151,7 @@ If you prefer, you can use a symbol instead of a string to specify the action to
|
|
152
151
|
```ruby
|
153
152
|
def update
|
154
153
|
@book = Book.find(params[:id])
|
155
|
-
if @book.update(
|
154
|
+
if @book.update(book_params)
|
156
155
|
redirect_to(@book)
|
157
156
|
else
|
158
157
|
render :edit
|
@@ -375,9 +374,9 @@ Rails understands both numeric status codes and the corresponding symbols shown
|
|
375
374
|
| | 423 | :locked |
|
376
375
|
| | 424 | :failed_dependency |
|
377
376
|
| | 426 | :upgrade_required |
|
378
|
-
| |
|
379
|
-
| |
|
380
|
-
| |
|
377
|
+
| | 428 | :precondition_required |
|
378
|
+
| | 429 | :too_many_requests |
|
379
|
+
| | 431 | :request_header_fields_too_large |
|
381
380
|
| **Server Error** | 500 | :internal_server_error |
|
382
381
|
| | 501 | :not_implemented |
|
383
382
|
| | 502 | :bad_gateway |
|
@@ -405,7 +404,7 @@ class ProductsController < ApplicationController
|
|
405
404
|
end
|
406
405
|
```
|
407
406
|
|
408
|
-
With this declaration, all of the views rendered by the
|
407
|
+
With this declaration, all of the views rendered by the `ProductsController` will use `app/views/layouts/inventory.html.erb` as their layout.
|
409
408
|
|
410
409
|
To assign a specific layout for the entire application, use a `layout` declaration in your `ApplicationController` class:
|
411
410
|
|
@@ -704,7 +703,7 @@ WARNING: The asset tag helpers do _not_ verify the existence of the assets at th
|
|
704
703
|
|
705
704
|
#### Linking to Feeds with the `auto_discovery_link_tag`
|
706
705
|
|
707
|
-
The `auto_discovery_link_tag` helper builds HTML that most browsers and
|
706
|
+
The `auto_discovery_link_tag` helper builds HTML that most browsers and feed readers can use to detect the presence of RSS or Atom feeds. It takes the type of the link (`:rss` or `:atom`), a hash of options that are passed through to url_for, and a hash of options for the tag:
|
708
707
|
|
709
708
|
```erb
|
710
709
|
<%= auto_discovery_link_tag(:rss, {action: "feed"},
|
@@ -1009,6 +1008,7 @@ You can also pass local variables into partials, making them even more powerful
|
|
1009
1008
|
|
1010
1009
|
```html+erb
|
1011
1010
|
<h1>New zone</h1>
|
1011
|
+
<%= error_messages_for :zone %>
|
1012
1012
|
<%= render partial: "form", locals: {zone: @zone} %>
|
1013
1013
|
```
|
1014
1014
|
|
@@ -1016,6 +1016,7 @@ You can also pass local variables into partials, making them even more powerful
|
|
1016
1016
|
|
1017
1017
|
```html+erb
|
1018
1018
|
<h1>Editing zone</h1>
|
1019
|
+
<%= error_messages_for :zone %>
|
1019
1020
|
<%= render partial: "form", locals: {zone: @zone} %>
|
1020
1021
|
```
|
1021
1022
|
|
@@ -1024,7 +1025,7 @@ You can also pass local variables into partials, making them even more powerful
|
|
1024
1025
|
```html+erb
|
1025
1026
|
<%= form_for(zone) do |f| %>
|
1026
1027
|
<p>
|
1027
|
-
<b>Zone name</b><br
|
1028
|
+
<b>Zone name</b><br>
|
1028
1029
|
<%= f.text_field :name %>
|
1029
1030
|
</p>
|
1030
1031
|
<p>
|
@@ -3,29 +3,10 @@ Maintenance Policy for Ruby on Rails
|
|
3
3
|
|
4
4
|
Support of the Rails framework is divided into four groups: New features, bug
|
5
5
|
fixes, security issues, and severe security issues. They are handled as
|
6
|
-
follows, all versions in
|
6
|
+
follows, all versions in x.y.z format
|
7
7
|
|
8
8
|
--------------------------------------------------------------------------------
|
9
9
|
|
10
|
-
Rails follows a shifted version of [semver](http://semver.org/):
|
11
|
-
|
12
|
-
**Patch `Z`**
|
13
|
-
|
14
|
-
Only bug fixes, no API changes, no new features.
|
15
|
-
Except as necessary for security fixes.
|
16
|
-
|
17
|
-
**Minor `Y`**
|
18
|
-
|
19
|
-
New features, may contain API changes (Serve as major versions of Semver).
|
20
|
-
Breaking changes are paired with deprecation notices in the previous minor
|
21
|
-
or major release.
|
22
|
-
|
23
|
-
**Major `X`**
|
24
|
-
|
25
|
-
New features, will likely contain API changes. The difference between Rails'
|
26
|
-
minor and major releases is the magnitude of breaking changes, and usually
|
27
|
-
reserved for special occasions.
|
28
|
-
|
29
10
|
New Features
|
30
11
|
------------
|
31
12
|
|
@@ -39,7 +20,7 @@ Only the latest release series will receive bug fixes. When enough bugs are
|
|
39
20
|
fixed and its deemed worthy to release a new gem, this is the branch it happens
|
40
21
|
from.
|
41
22
|
|
42
|
-
**Currently included series:**
|
23
|
+
**Currently included series:** 4.0.z
|
43
24
|
|
44
25
|
Security Issues
|
45
26
|
---------------
|
@@ -54,7 +35,7 @@ be built from 1.2.2, and then added to the end of 1-2-stable. This means that
|
|
54
35
|
security releases are easy to upgrade to if you're running the latest version
|
55
36
|
of Rails.
|
56
37
|
|
57
|
-
**Currently included series:**
|
38
|
+
**Currently included series:** 4.0.z, 3.2.z
|
58
39
|
|
59
40
|
Severe Security Issues
|
60
41
|
----------------------
|
@@ -63,7 +44,7 @@ For severe security issues we will provide new versions as above, and also the
|
|
63
44
|
last major release series will receive patches and new versions. The
|
64
45
|
classification of the security issue is judged by the core team.
|
65
46
|
|
66
|
-
**Currently included series:**
|
47
|
+
**Currently included series:** 4.0.z, 3.2.z
|
67
48
|
|
68
49
|
Unsupported Release Series
|
69
50
|
--------------------------
|
data/guides/source/migrations.md
CHANGED
@@ -184,7 +184,7 @@ class RemovePartNumberFromProducts < ActiveRecord::Migration
|
|
184
184
|
end
|
185
185
|
```
|
186
186
|
|
187
|
-
You are not limited to one magically generated column. For example
|
187
|
+
You are not limited to one magically generated column. For example:
|
188
188
|
|
189
189
|
```bash
|
190
190
|
$ rails generate migration AddDetailsToProducts part_number:string price:decimal
|
@@ -227,7 +227,7 @@ or remove from it as you see fit by editing the
|
|
227
227
|
`db/migrate/YYYYMMDDHHMMSS_add_details_to_products.rb` file.
|
228
228
|
|
229
229
|
Also, the generator accepts column type as `references`(also available as
|
230
|
-
`belongs_to`). For instance
|
230
|
+
`belongs_to`). For instance:
|
231
231
|
|
232
232
|
```bash
|
233
233
|
$ rails generate migration AddUserRefToProducts user:references
|
@@ -269,7 +269,7 @@ end
|
|
269
269
|
The model and scaffold generators will create migrations appropriate for adding
|
270
270
|
a new model. This migration will already contain instructions for creating the
|
271
271
|
relevant table. If you tell Rails what columns you want, then statements for
|
272
|
-
adding these columns will also be created. For example, running
|
272
|
+
adding these columns will also be created. For example, running:
|
273
273
|
|
274
274
|
```bash
|
275
275
|
$ rails generate model Product name:string description:text
|
@@ -303,10 +303,10 @@ braces. You can use the following modifiers:
|
|
303
303
|
* `polymorphic` Adds a `type` column for `belongs_to` associations
|
304
304
|
* `null` Allows or disallows `NULL` values in the column.
|
305
305
|
|
306
|
-
For instance, running
|
306
|
+
For instance, running:
|
307
307
|
|
308
308
|
```bash
|
309
|
-
$ rails generate migration AddDetailsToProducts price:decimal{5,2} supplier:references{polymorphic}
|
309
|
+
$ rails generate migration AddDetailsToProducts 'price:decimal{5,2}' supplier:references{polymorphic}
|
310
310
|
```
|
311
311
|
|
312
312
|
will produce a migration that looks like this
|
@@ -345,7 +345,7 @@ By default, `create_table` will create a primary key called `id`. You can change
|
|
345
345
|
the name of the primary key with the `:primary_key` option (don't forget to
|
346
346
|
update the corresponding model) or, if you don't want a primary key at all, you
|
347
347
|
can pass the option `id: false`. If you need to pass database specific options
|
348
|
-
you can place an SQL fragment in the `:options` option. For example
|
348
|
+
you can place an SQL fragment in the `:options` option. For example:
|
349
349
|
|
350
350
|
```ruby
|
351
351
|
create_table :products, options: "ENGINE=BLACKHOLE" do |t|
|
@@ -359,7 +359,7 @@ will append `ENGINE=BLACKHOLE` to the SQL statement used to create the table
|
|
359
359
|
### Creating a Join Table
|
360
360
|
|
361
361
|
Migration method `create_join_table` creates a HABTM join table. A typical use
|
362
|
-
would be
|
362
|
+
would be:
|
363
363
|
|
364
364
|
```ruby
|
365
365
|
create_join_table :products, :categories
|
@@ -367,26 +367,24 @@ create_join_table :products, :categories
|
|
367
367
|
|
368
368
|
which creates a `categories_products` table with two columns called
|
369
369
|
`category_id` and `product_id`. These columns have the option `:null` set to
|
370
|
-
`false` by default.
|
371
|
-
|
372
|
-
You can pass the option `:table_name` when you want to customize the table
|
373
|
-
name. For example,
|
370
|
+
`false` by default. This can be overridden by specifying the `:column_options`
|
371
|
+
option.
|
374
372
|
|
375
373
|
```ruby
|
376
|
-
create_join_table :products, :categories,
|
374
|
+
create_join_table :products, :categories, column_options: {null: true}
|
377
375
|
```
|
378
376
|
|
379
|
-
will create
|
377
|
+
will create the `product_id` and `category_id` with the `:null` option as
|
378
|
+
`true`.
|
380
379
|
|
381
|
-
|
382
|
-
|
380
|
+
You can pass the option `:table_name` when you want to customize the table
|
381
|
+
name. For example:
|
383
382
|
|
384
383
|
```ruby
|
385
|
-
create_join_table :products, :categories,
|
384
|
+
create_join_table :products, :categories, table_name: :categorization
|
386
385
|
```
|
387
386
|
|
388
|
-
will create
|
389
|
-
`true`.
|
387
|
+
will create a `categorization` table.
|
390
388
|
|
391
389
|
`create_join_table` also accepts a block, which you can use to add indices
|
392
390
|
(which are not created by default) or additional columns:
|
@@ -402,7 +400,7 @@ end
|
|
402
400
|
|
403
401
|
A close cousin of `create_table` is `change_table`, used for changing existing
|
404
402
|
tables. It is used in a similar fashion to `create_table` but the object
|
405
|
-
yielded to the block knows more tricks. For example
|
403
|
+
yielded to the block knows more tricks. For example:
|
406
404
|
|
407
405
|
```ruby
|
408
406
|
change_table :products do |t|
|
@@ -449,7 +447,7 @@ definitions:
|
|
449
447
|
* `create_table`
|
450
448
|
* `create_join_table`
|
451
449
|
* `drop_table` (must supply a block)
|
452
|
-
* `drop_join_table`
|
450
|
+
* `drop_join_table` (must supply a block)
|
453
451
|
* `remove_timestamps`
|
454
452
|
* `rename_column`
|
455
453
|
* `rename_index`
|
@@ -466,7 +464,7 @@ or write the `up` and `down` methods instead of using the `change` method.
|
|
466
464
|
|
467
465
|
Complex migrations may require processing that Active Record doesn't know how
|
468
466
|
to reverse. You can use `reversible` to specify what to do when running a
|
469
|
-
migration what else to do when reverting it. For example
|
467
|
+
migration what else to do when reverting it. For example:
|
470
468
|
|
471
469
|
```ruby
|
472
470
|
class ExampleMigration < ActiveRecord::Migration
|
@@ -650,7 +648,7 @@ will update your `db/schema.rb` file to match the structure of your database.
|
|
650
648
|
If you specify a target version, Active Record will run the required migrations
|
651
649
|
(change, up, down) until it has reached the specified version. The version
|
652
650
|
is the numerical prefix on the migration's filename. For example, to migrate
|
653
|
-
to version 20080906120000 run
|
651
|
+
to version 20080906120000 run:
|
654
652
|
|
655
653
|
```bash
|
656
654
|
$ rake db:migrate VERSION=20080906120000
|
@@ -667,7 +665,7 @@ down to, but not including, 20080906120000.
|
|
667
665
|
|
668
666
|
A common task is to rollback the last migration. For example, if you made a
|
669
667
|
mistake in it and wish to correct it. Rather than tracking down the version
|
670
|
-
number associated with the previous migration you can run
|
668
|
+
number associated with the previous migration you can run:
|
671
669
|
|
672
670
|
```bash
|
673
671
|
$ rake db:rollback
|
@@ -685,7 +683,7 @@ will revert the last 3 migrations.
|
|
685
683
|
|
686
684
|
The `db:migrate:redo` task is a shortcut for doing a rollback and then migrating
|
687
685
|
back up again. As with the `db:rollback` task, you can use the `STEP` parameter
|
688
|
-
if you need to go more than one version back, for example
|
686
|
+
if you need to go more than one version back, for example:
|
689
687
|
|
690
688
|
```bash
|
691
689
|
$ rake db:migrate:redo STEP=3
|
@@ -695,22 +693,27 @@ Neither of these Rake tasks do anything you could not do with `db:migrate`. They
|
|
695
693
|
are simply more convenient, since you do not need to explicitly specify the
|
696
694
|
version to migrate to.
|
697
695
|
|
696
|
+
### Setup the Database
|
697
|
+
|
698
|
+
The `rake db:setup` task will create the database, load the schema and initialize
|
699
|
+
it with the seed data.
|
700
|
+
|
698
701
|
### Resetting the Database
|
699
702
|
|
700
|
-
The `rake db:reset` task will drop the database
|
701
|
-
|
703
|
+
The `rake db:reset` task will drop the database and set it up again. This is
|
704
|
+
functionally equivalent to `rake db:drop db:setup`.
|
702
705
|
|
703
706
|
NOTE: This is not the same as running all the migrations. It will only use the
|
704
|
-
contents of the current schema.rb file. If a migration can't be rolled back,
|
705
|
-
|
706
|
-
|
707
|
+
contents of the current `schema.rb` file. If a migration can't be rolled back,
|
708
|
+
`rake db:reset` may not help you. To find out more about dumping the schema see
|
709
|
+
[Schema Dumping and You](#schema-dumping-and-you) section.
|
707
710
|
|
708
711
|
### Running Specific Migrations
|
709
712
|
|
710
713
|
If you need to run a specific migration up or down, the `db:migrate:up` and
|
711
714
|
`db:migrate:down` tasks will do that. Just specify the appropriate version and
|
712
715
|
the corresponding migration will have its `change`, `up` or `down` method
|
713
|
-
invoked, for example
|
716
|
+
invoked, for example:
|
714
717
|
|
715
718
|
```bash
|
716
719
|
$ rake db:migrate:up VERSION=20080906120000
|
@@ -752,7 +755,7 @@ Several methods are provided in migrations that allow you to control all this:
|
|
752
755
|
| say | Takes a message argument and outputs it as is. A second boolean argument can be passed to specify whether to indent or not.
|
753
756
|
| say_with_time | Outputs text along with how long it took to run its block. If the block returns an integer it assumes it is the number of rows affected.
|
754
757
|
|
755
|
-
For example, this migration
|
758
|
+
For example, this migration:
|
756
759
|
|
757
760
|
```ruby
|
758
761
|
class CreateProducts < ActiveRecord::Migration
|
@@ -853,7 +856,7 @@ She also adds a validation to the `Product` model for the new column:
|
|
853
856
|
# app/models/product.rb
|
854
857
|
|
855
858
|
class Product < ActiveRecord::Base
|
856
|
-
validates :flag,
|
859
|
+
validates :flag, inclusion: { in: [true, false] }
|
857
860
|
end
|
858
861
|
```
|
859
862
|
|
@@ -879,7 +882,8 @@ She also adds a validation to the `Product` model for the new column:
|
|
879
882
|
# app/models/product.rb
|
880
883
|
|
881
884
|
class Product < ActiveRecord::Base
|
882
|
-
validates :flag, :
|
885
|
+
validates :flag, inclusion: { in: [true, false] }
|
886
|
+
validates :fuzz, presence: true
|
883
887
|
end
|
884
888
|
```
|
885
889
|
|
@@ -1036,8 +1040,8 @@ this, then you should set the schema format to `:sql`.
|
|
1036
1040
|
Instead of using Active Record's schema dumper, the database's structure will
|
1037
1041
|
be dumped using a tool specific to the database (via the `db:structure:dump`
|
1038
1042
|
Rake task) into `db/structure.sql`. For example, for PostgreSQL, the `pg_dump`
|
1039
|
-
utility is used. For MySQL, this file will contain the output of
|
1040
|
-
TABLE` for the various tables.
|
1043
|
+
utility is used. For MySQL, this file will contain the output of
|
1044
|
+
`SHOW CREATE TABLE` for the various tables.
|
1041
1045
|
|
1042
1046
|
Loading these schemas is simply a question of executing the SQL statements they
|
1043
1047
|
contain. By definition, this will create a perfect copy of the database's
|
@@ -1066,8 +1070,8 @@ with foreign key constraints in the database.
|
|
1066
1070
|
|
1067
1071
|
Although Active Record does not provide any tools for working directly with
|
1068
1072
|
such features, the `execute` method can be used to execute arbitrary SQL. You
|
1069
|
-
|
1070
|
-
[foreigner](https://github.com/matthuhiggins/foreigner) which
|
1073
|
+
can also use a gem like
|
1074
|
+
[foreigner](https://github.com/matthuhiggins/foreigner) which adds foreign key
|
1071
1075
|
support to Active Record (including support for dumping foreign keys in
|
1072
1076
|
`db/schema.rb`).
|
1073
1077
|
|
@@ -9,7 +9,7 @@ After reading this guide, you will know:
|
|
9
9
|
|
10
10
|
--------------------------------------------------------------------------------
|
11
11
|
|
12
|
-
NOTE: This guide assumes the user knows how to use the [Rails form helpers](form_helpers.html) in general. Also, it
|
12
|
+
NOTE: This guide assumes the user knows how to use the [Rails form helpers](form_helpers.html) in general. Also, it's **not** an API reference. For a complete reference please visit [the Rails API documentation](http://api.rubyonrails.org/).
|
13
13
|
|
14
14
|
|
15
15
|
Model setup
|
@@ -56,7 +56,7 @@ end
|
|
56
56
|
|
57
57
|
### Custom model
|
58
58
|
|
59
|
-
As you might have inflected from this explanation, you _don
|
59
|
+
As you might have inflected from this explanation, you _don't_ necessarily need an ActiveRecord::Base model to use this functionality. The following examples are sufficient to enable the nested model form behavior:
|
60
60
|
|
61
61
|
#### Single associated object
|
62
62
|
|
@@ -177,7 +177,7 @@ When this form is posted the Rails parameter parser will construct a hash like t
|
|
177
177
|
}
|
178
178
|
```
|
179
179
|
|
180
|
-
That
|
180
|
+
That's it. The controller will simply pass this hash on to the model from the `create` action. The model will then handle building the `address` association for you and automatically save it when the parent (`person`) is saved.
|
181
181
|
|
182
182
|
#### Nested form for a collection of associated objects
|
183
183
|
|
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.
|
@@ -48,7 +48,7 @@ See usage and options by asking for help:
|
|
48
48
|
$ 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
|
@@ -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
|
@@ -219,7 +219,7 @@ $ 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
|
@@ -245,7 +245,7 @@ end
|
|
245
245
|
|
246
246
|
```
|
247
247
|
|
248
|
-
We will also add code to define the acts_as_yaffle method.
|
248
|
+
We will also add code to define the `acts_as_yaffle` method.
|
249
249
|
|
250
250
|
```ruby
|
251
251
|
# yaffle/lib/yaffle/acts_as_yaffle.rb
|
@@ -286,7 +286,7 @@ You can then return to the root directory (`cd ../..`) of your plugin and rerun
|
|
286
286
|
|
287
287
|
```
|
288
288
|
|
289
|
-
Getting closer... Now we will implement the code of the acts_as_yaffle method to make the tests pass.
|
289
|
+
Getting closer... Now we will implement the code of the `acts_as_yaffle` method to make the tests pass.
|
290
290
|
|
291
291
|
```ruby
|
292
292
|
# yaffle/lib/yaffle/acts_as_yaffle.rb
|
@@ -310,7 +310,7 @@ end
|
|
310
310
|
ActiveRecord::Base.send :include, Yaffle::ActsAsYaffle
|
311
311
|
```
|
312
312
|
|
313
|
-
When you run `rake
|
313
|
+
When you run `rake`, you should see the tests all pass:
|
314
314
|
|
315
315
|
```bash
|
316
316
|
5 tests, 5 assertions, 0 failures, 0 errors, 0 skips
|
@@ -327,7 +327,7 @@ To start out, write a failing test that shows the behavior you'd like:
|
|
327
327
|
# yaffle/test/acts_as_yaffle_test.rb
|
328
328
|
require 'test_helper'
|
329
329
|
|
330
|
-
class ActsAsYaffleTest <
|
330
|
+
class ActsAsYaffleTest < ActiveSupport::TestCase
|
331
331
|
|
332
332
|
def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
|
333
333
|
assert_equal "last_squawk", Hickwall.yaffle_text_field
|
@@ -390,7 +390,11 @@ Run `rake` one final time and you should see:
|
|
390
390
|
7 tests, 7 assertions, 0 failures, 0 errors, 0 skips
|
391
391
|
```
|
392
392
|
|
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
|
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:
|
394
|
+
|
395
|
+
```ruby
|
396
|
+
send("#{self.class.yaffle_text_field}=", string.to_squawk)
|
397
|
+
```
|
394
398
|
|
395
399
|
Generators
|
396
400
|
----------
|
@@ -398,7 +402,7 @@ Generators
|
|
398
402
|
Generators can be included in your gem simply by creating them in a lib/generators directory of your plugin. More information about
|
399
403
|
the creation of generators can be found in the [Generators Guide](generators.html)
|
400
404
|
|
401
|
-
Publishing
|
405
|
+
Publishing Your Gem
|
402
406
|
-------------------
|
403
407
|
|
404
408
|
Gem plugins currently in development can easily be shared from any Git repository. To share the Yaffle gem with others, simply
|
@@ -411,12 +415,12 @@ gem 'yaffle', git: 'git://github.com/yaffle_watcher/yaffle.git'
|
|
411
415
|
After running `bundle install`, your gem functionality will be available to the application.
|
412
416
|
|
413
417
|
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)
|
418
|
+
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
419
|
|
416
420
|
RDoc Documentation
|
417
421
|
------------------
|
418
422
|
|
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.
|
423
|
+
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
424
|
|
421
425
|
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
426
|
|