rails 4.2.0.beta3 → 4.2.0.beta4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -7
- data/guides/output/2_2_release_notes.html +724 -0
- data/guides/output/2_3_release_notes.html +870 -0
- data/guides/output/3_0_release_notes.html +773 -0
- data/guides/output/3_1_release_notes.html +740 -0
- data/guides/output/3_2_release_notes.html +797 -0
- data/guides/output/4_0_release_notes.html +523 -0
- data/guides/output/4_1_release_notes.html +806 -0
- data/guides/output/4_2_release_notes.html +728 -0
- data/guides/output/Gemfile +6 -0
- data/guides/output/_license.html +226 -0
- data/guides/output/_welcome.html +233 -0
- data/guides/output/action_controller_overview.html +1335 -0
- data/guides/output/action_mailer_basics.html +928 -0
- data/guides/output/action_view_overview.html +1509 -0
- data/guides/output/active_job_basics.html +546 -0
- data/guides/output/active_model_basics.html +438 -0
- data/guides/output/active_record_basics.html +594 -0
- data/guides/output/active_record_callbacks.html +592 -0
- data/guides/output/active_record_migrations.html +1123 -0
- data/guides/output/active_record_postgresql.html +675 -0
- data/guides/output/active_record_querying.html +1796 -0
- data/guides/output/active_record_validations.html +1301 -0
- data/guides/output/active_support_core_extensions.html +3450 -0
- data/guides/output/active_support_instrumentation.html +1121 -0
- data/guides/output/api_documentation_guidelines.html +498 -0
- data/guides/output/asset_pipeline.html +1167 -0
- data/guides/output/association_basics.html +2086 -0
- data/guides/output/caching_with_rails.html +553 -0
- data/guides/output/command_line.html +791 -0
- data/guides/output/configuring.html +1055 -0
- data/guides/output/contributing_to_ruby_on_rails.html +657 -0
- data/guides/output/credits.html +284 -0
- data/guides/output/debugging_rails_applications.html +1014 -0
- data/guides/output/development_dependencies_install.html +478 -0
- data/guides/output/engines.html +1438 -0
- data/guides/output/form_helpers.html +1074 -0
- data/guides/output/generators.html +838 -0
- data/guides/output/getting_started.html +2092 -0
- data/guides/output/i18n.html +1198 -0
- data/guides/output/images/akshaysurve.jpg +0 -0
- data/guides/output/images/belongs_to.png +0 -0
- data/guides/output/images/book_icon.gif +0 -0
- data/guides/output/images/bullet.gif +0 -0
- data/guides/output/images/chapters_icon.gif +0 -0
- data/guides/output/images/check_bullet.gif +0 -0
- data/guides/output/images/credits_pic_blank.gif +0 -0
- data/guides/output/images/csrf.png +0 -0
- data/guides/output/images/edge_badge.png +0 -0
- data/guides/output/images/favicon.ico +0 -0
- data/guides/output/images/feature_tile.gif +0 -0
- data/guides/output/images/footer_tile.gif +0 -0
- data/guides/output/images/fxn.png +0 -0
- data/guides/output/images/getting_started/article_with_comments.png +0 -0
- data/guides/output/images/getting_started/challenge.png +0 -0
- data/guides/output/images/getting_started/confirm_dialog.png +0 -0
- data/guides/output/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
- data/guides/output/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
- data/guides/output/images/getting_started/form_with_errors.png +0 -0
- data/guides/output/images/getting_started/index_action_with_edit_link.png +0 -0
- data/guides/output/images/getting_started/new_article.png +0 -0
- data/guides/output/images/getting_started/new_post.png +0 -0
- data/guides/output/images/getting_started/post_with_comments.png +0 -0
- data/guides/output/images/getting_started/rails_welcome.png +0 -0
- data/guides/output/images/getting_started/routing_error_no_controller.png +0 -0
- data/guides/output/images/getting_started/routing_error_no_route_matches.png +0 -0
- data/guides/output/images/getting_started/show_action_for_articles.png +0 -0
- data/guides/output/images/getting_started/show_action_for_posts.png +0 -0
- data/guides/output/images/getting_started/template_is_missing_articles_new.png +0 -0
- data/guides/output/images/getting_started/template_is_missing_posts_new.png +0 -0
- data/guides/output/images/getting_started/undefined_method_post_path.png +0 -0
- data/guides/output/images/getting_started/unknown_action_create_for_articles.png +0 -0
- data/guides/output/images/getting_started/unknown_action_create_for_posts.png +0 -0
- data/guides/output/images/getting_started/unknown_action_new_for_articles.png +0 -0
- data/guides/output/images/getting_started/unknown_action_new_for_posts.png +0 -0
- data/guides/output/images/grey_bullet.gif +0 -0
- data/guides/output/images/habtm.png +0 -0
- data/guides/output/images/has_many.png +0 -0
- data/guides/output/images/has_many_through.png +0 -0
- data/guides/output/images/has_one.png +0 -0
- data/guides/output/images/has_one_through.png +0 -0
- data/guides/output/images/header_backdrop.png +0 -0
- data/guides/output/images/header_tile.gif +0 -0
- data/guides/output/images/i18n/demo_html_safe.png +0 -0
- data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translated_en.png +0 -0
- data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translation_missing.png +0 -0
- data/guides/output/images/i18n/demo_untranslated.png +0 -0
- data/guides/output/images/icons/README +5 -0
- data/guides/output/images/icons/callouts/1.png +0 -0
- data/guides/output/images/icons/callouts/10.png +0 -0
- data/guides/output/images/icons/callouts/11.png +0 -0
- data/guides/output/images/icons/callouts/12.png +0 -0
- data/guides/output/images/icons/callouts/13.png +0 -0
- data/guides/output/images/icons/callouts/14.png +0 -0
- data/guides/output/images/icons/callouts/15.png +0 -0
- data/guides/output/images/icons/callouts/2.png +0 -0
- data/guides/output/images/icons/callouts/3.png +0 -0
- data/guides/output/images/icons/callouts/4.png +0 -0
- data/guides/output/images/icons/callouts/5.png +0 -0
- data/guides/output/images/icons/callouts/6.png +0 -0
- data/guides/output/images/icons/callouts/7.png +0 -0
- data/guides/output/images/icons/callouts/8.png +0 -0
- data/guides/output/images/icons/callouts/9.png +0 -0
- data/guides/output/images/icons/caution.png +0 -0
- data/guides/output/images/icons/example.png +0 -0
- data/guides/output/images/icons/home.png +0 -0
- data/guides/output/images/icons/important.png +0 -0
- data/guides/output/images/icons/next.png +0 -0
- data/guides/output/images/icons/note.png +0 -0
- data/guides/output/images/icons/prev.png +0 -0
- data/guides/output/images/icons/tip.png +0 -0
- data/guides/output/images/icons/up.png +0 -0
- data/guides/output/images/icons/warning.png +0 -0
- data/guides/output/images/nav_arrow.gif +0 -0
- data/guides/output/images/oscardelben.jpg +0 -0
- data/guides/output/images/polymorphic.png +0 -0
- data/guides/output/images/radar.png +0 -0
- data/guides/output/images/rails4_features.png +0 -0
- data/guides/output/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/output/images/rails_guides_logo.gif +0 -0
- data/guides/output/images/rails_logo_remix.gif +0 -0
- data/guides/output/images/session_fixation.png +0 -0
- data/guides/output/images/tab_grey.gif +0 -0
- data/guides/output/images/tab_info.gif +0 -0
- data/guides/output/images/tab_note.gif +0 -0
- data/guides/output/images/tab_red.gif +0 -0
- data/guides/output/images/tab_yellow.gif +0 -0
- data/guides/output/images/tab_yellow.png +0 -0
- data/guides/output/images/vijaydev.jpg +0 -0
- data/guides/output/index.html +354 -0
- data/guides/output/initialization.html +876 -0
- data/guides/output/javascripts/guides.js +59 -0
- data/guides/output/javascripts/jquery.min.js +4 -0
- data/guides/output/javascripts/responsive-tables.js +43 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
- data/guides/output/javascripts/syntaxhighlighter/shCore.js +17 -0
- data/guides/output/layout.html +448 -0
- data/guides/output/layouts_and_rendering.html +1541 -0
- data/guides/output/maintenance_policy.html +257 -0
- data/guides/output/migrations.html +1360 -0
- data/guides/output/nested_model_forms.html +412 -0
- data/guides/output/plugins.html +644 -0
- data/guides/output/rails_application_templates.html +450 -0
- data/guides/output/rails_on_rack.html +547 -0
- data/guides/output/routing.html +1631 -0
- data/guides/output/ruby_on_rails_guides_guidelines.html +329 -0
- data/guides/output/security.html +935 -0
- data/guides/output/stylesheets/fixes.css +16 -0
- data/guides/output/stylesheets/kindle.css +11 -0
- data/guides/output/stylesheets/main.css +713 -0
- data/guides/output/stylesheets/print.css +52 -0
- data/guides/output/stylesheets/reset.css +43 -0
- data/guides/output/stylesheets/responsive-tables.css +50 -0
- data/guides/output/stylesheets/style.css +13 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCore.css +226 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
- data/guides/output/testing.html +1350 -0
- data/guides/output/upgrading_ruby_on_rails.html +1135 -0
- data/guides/output/working_with_javascript_in_rails.html +587 -0
- data/guides/source/4_0_release_notes.md +2 -2
- data/guides/source/4_2_release_notes.md +9 -2
- data/guides/source/action_controller_overview.md +3 -1
- data/guides/source/action_mailer_basics.md +1 -2
- data/guides/source/active_job_basics.md +25 -2
- data/guides/source/active_model_basics.md +350 -19
- data/guides/source/active_record_basics.md +2 -2
- data/guides/source/active_record_migrations.md +1 -1
- data/guides/source/active_record_validations.md +12 -4
- data/guides/source/active_support_core_extensions.md +7 -9
- data/guides/source/active_support_instrumentation.md +2 -0
- data/guides/source/association_basics.md +7 -7
- data/guides/source/configuring.md +24 -0
- data/guides/source/contributing_to_ruby_on_rails.md +1 -1
- data/guides/source/credits.html.erb +1 -1
- data/guides/source/documents.yaml +5 -0
- data/guides/source/engines.md +2 -2
- data/guides/source/form_helpers.md +6 -0
- data/guides/source/i18n.md +6 -9
- data/guides/source/layouts_and_rendering.md +1 -1
- data/guides/source/plugins.md +2 -2
- data/guides/source/rails_on_rack.md +4 -0
- data/guides/source/testing.md +1 -0
- data/guides/source/upgrading_ruby_on_rails.md +5 -5
- metadata +227 -27
@@ -229,11 +229,11 @@ Please refer to the [Changelog](https://github.com/rails/rails/blob/4-0-stable/a
|
|
229
229
|
The method `change_table` is also reversible, as long as its block doesn't call `remove`, `change` or `change_default`
|
230
230
|
|
231
231
|
* New method `reversible` makes it possible to specify code to be run when migrating up or down.
|
232
|
-
See the [Guide on Migration](https://github.com/rails/rails/blob/master/guides/source/
|
232
|
+
See the [Guide on Migration](https://github.com/rails/rails/blob/master/guides/source/active_record_migrations.md#using-reversible)
|
233
233
|
|
234
234
|
* New method `revert` will revert a whole migration or the given block.
|
235
235
|
If migrating down, the given migration / block is run normally.
|
236
|
-
See the [Guide on Migration](https://github.com/rails/rails/blob/master/guides/source/
|
236
|
+
See the [Guide on Migration](https://github.com/rails/rails/blob/master/guides/source/active_record_migrations.md#reverting-previous-migrations)
|
237
237
|
|
238
238
|
* Adds PostgreSQL array type support. Any datatype can be used to create an array column, with full migration and schema dumper support.
|
239
239
|
|
@@ -79,7 +79,7 @@ post.comments(true) # uses cached pattern
|
|
79
79
|
The caching is not used in the following scenarios:
|
80
80
|
|
81
81
|
- The model has a default scope
|
82
|
-
- The model uses single table
|
82
|
+
- The model uses single table inheritance to inherit from another model
|
83
83
|
- `find` with a list of ids. eg:
|
84
84
|
|
85
85
|
```ruby
|
@@ -395,6 +395,9 @@ Please refer to the [Changelog][action-pack] for detailed changes.
|
|
395
395
|
|
396
396
|
### Deprecations
|
397
397
|
|
398
|
+
* Deprecated the `only_path` option on `*_path` helpers.
|
399
|
+
([Commit](https://github.com/rails/rails/commit/aa1fadd48fb40dd9396a383696134a259aa59db9))
|
400
|
+
|
398
401
|
* Deprecated `assert_tag`, `assert_no_tag`, `find_tag` and `find_all_tag` in
|
399
402
|
favor of `assert_select`.
|
400
403
|
([Commit](https://github.com/rails/rails-dom-testing/commit/b12850bc5ff23ba4b599bf2770874dd4f11bf750))
|
@@ -750,10 +753,14 @@ Please refer to the [Changelog][active-support] for detailed changes.
|
|
750
753
|
|
751
754
|
### Notable changes
|
752
755
|
|
756
|
+
* `Object#try` and `Object#try!` can now be used without an explicit receiver.
|
757
|
+
([Commit](https://github.com/rails/rails/commit/5e51bdda59c9ba8e5faf86294e3e431bd45f1830),
|
758
|
+
[Pull Request](https://github.com/rails/rails/pull/17361))
|
759
|
+
|
753
760
|
* Introduced new configuration option `active_support.test_order` for
|
754
761
|
specifying the order test cases are executed. This option currently defaults
|
755
762
|
to `:sorted` but will be changed to `:random` in Rails 5.0.
|
756
|
-
([Commit](
|
763
|
+
([Commit](https://github.com/rails/rails/commit/53e877f7d9291b2bf0b8c425f9e32ef35829f35b))
|
757
764
|
|
758
765
|
* The `travel_to` test helper now truncates the `usec` component to 0.
|
759
766
|
([Commit](https://github.com/rails/rails/commit/9f6e82ee4783e491c20f5244a613fdeb4024beb5))
|
@@ -1221,7 +1221,9 @@ Create the controller and views.
|
|
1221
1221
|
error.html.erb
|
1222
1222
|
```
|
1223
1223
|
|
1224
|
-
Do not forget to set the correct status code on the controller as shown before.
|
1224
|
+
Do not forget to set the correct status code on the controller as shown before.
|
1225
|
+
|
1226
|
+
WARNING: You should avoid using the database or any complex operations because the user is already on the error page. Generating another error while on an error page could cause issues like presenting an empty page for the users.
|
1225
1227
|
|
1226
1228
|
Force HTTPS protocol
|
1227
1229
|
--------------------
|
@@ -298,8 +298,7 @@ Action Mailer 3.0 makes inline attachments, which involved a lot of hacking in p
|
|
298
298
|
```html+erb
|
299
299
|
<p>Hello there, this is our image</p>
|
300
300
|
|
301
|
-
<%= image_tag attachments['image.jpg'].url, alt: 'My Photo',
|
302
|
-
class: 'photos' %>
|
301
|
+
<%= image_tag attachments['image.jpg'].url, alt: 'My Photo', class: 'photos' %>
|
303
302
|
```
|
304
303
|
|
305
304
|
#### Sending Email To Multiple Recipients
|
@@ -99,7 +99,7 @@ If no adapter is set, the job is immediately executed.
|
|
99
99
|
|
100
100
|
### Backends
|
101
101
|
|
102
|
-
Active Job has
|
102
|
+
Active Job has built-in adapters for multiple queueing backends (Sidekiq,
|
103
103
|
Resque, Delayed Job and others). To get an up-to-date list of the adapters
|
104
104
|
see the API Documentation for [ActiveJob::QueueAdapters](http://api.rubyonrails.org/classes/ActiveJob/QueueAdapters.html).
|
105
105
|
|
@@ -149,6 +149,29 @@ end
|
|
149
149
|
# environment
|
150
150
|
```
|
151
151
|
|
152
|
+
The default queue name prefix delimiter is '_'. This can be changed by setting
|
153
|
+
`config.active_job.queue_name_delimiter` in `application.rb`:
|
154
|
+
|
155
|
+
```ruby
|
156
|
+
# config/application.rb
|
157
|
+
module YourApp
|
158
|
+
class Application < Rails::Application
|
159
|
+
config.active_job.queue_name_prefix = Rails.env
|
160
|
+
config.active_job.queue_name_delimiter = '.'
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
# app/jobs/guests_cleanup.rb
|
165
|
+
class GuestsCleanupJob < ActiveJob::Base
|
166
|
+
queue_as :low_priority
|
167
|
+
#....
|
168
|
+
end
|
169
|
+
|
170
|
+
# Now your job will run on queue production.low_priority on your
|
171
|
+
# production environment and on staging.low_priority on your staging
|
172
|
+
# environment
|
173
|
+
```
|
174
|
+
|
152
175
|
If you want more control on what queue a job will be run you can pass a :queue
|
153
176
|
option to #set:
|
154
177
|
|
@@ -263,7 +286,7 @@ class TrashableCleanupJob
|
|
263
286
|
end
|
264
287
|
```
|
265
288
|
|
266
|
-
This works with any class that mixes in `
|
289
|
+
This works with any class that mixes in `GlobalID::Identification`, which
|
267
290
|
by default has been mixed into Active Model classes.
|
268
291
|
|
269
292
|
|
@@ -1,20 +1,32 @@
|
|
1
1
|
Active Model Basics
|
2
2
|
===================
|
3
3
|
|
4
|
-
This guide should provide you with all you need to get started using model
|
4
|
+
This guide should provide you with all you need to get started using model
|
5
|
+
classes. Active Model allows for Action Pack helpers to interact with
|
6
|
+
plain Ruby objects. Active Model also helps build custom ORMs for use
|
7
|
+
outside of the Rails framework.
|
5
8
|
|
6
|
-
After reading this guide, you will
|
9
|
+
After reading this guide, you will be able to add to plain Ruby objects:
|
10
|
+
|
11
|
+
* The ability to behave like an Active Record model.
|
12
|
+
* Callbacks and validations like Active Record.
|
13
|
+
* Serializers.
|
14
|
+
* Integration with the Rails internationalization (i18n) framework.
|
7
15
|
|
8
16
|
--------------------------------------------------------------------------------
|
9
17
|
|
10
18
|
Introduction
|
11
19
|
------------
|
12
20
|
|
13
|
-
Active Model is a library containing various modules used in developing
|
21
|
+
Active Model is a library containing various modules used in developing
|
22
|
+
classes that need some features present on Active Record.
|
23
|
+
Some of these modules are explained below.
|
14
24
|
|
15
|
-
###
|
25
|
+
### Attribute Methods
|
16
26
|
|
17
|
-
The AttributeMethods module can add custom prefixes and suffixes
|
27
|
+
The `ActiveModel::AttributeMethods` module can add custom prefixes and suffixes
|
28
|
+
on methods of a class. It is used by defining the prefixes and suffixes and
|
29
|
+
which methods on the object will use them.
|
18
30
|
|
19
31
|
```ruby
|
20
32
|
class Person
|
@@ -38,14 +50,17 @@ end
|
|
38
50
|
|
39
51
|
person = Person.new
|
40
52
|
person.age = 110
|
41
|
-
person.age_highest? # true
|
42
|
-
person.reset_age # 0
|
43
|
-
person.age_highest? # false
|
53
|
+
person.age_highest? # => true
|
54
|
+
person.reset_age # => 0
|
55
|
+
person.age_highest? # => false
|
44
56
|
```
|
45
57
|
|
46
58
|
### Callbacks
|
47
59
|
|
48
|
-
Callbacks gives Active Record style callbacks. This provides an
|
60
|
+
`ActiveModel::Callbacks` gives Active Record style callbacks. This provides an
|
61
|
+
ability to define callbacks which run at appropriate times.
|
62
|
+
After defining callbacks, you can wrap them with before, after and around
|
63
|
+
custom methods.
|
49
64
|
|
50
65
|
```ruby
|
51
66
|
class Person
|
@@ -69,7 +84,9 @@ end
|
|
69
84
|
|
70
85
|
### Conversion
|
71
86
|
|
72
|
-
If a class defines `persisted?` and `id` methods, then you can include the
|
87
|
+
If a class defines `persisted?` and `id` methods, then you can include the
|
88
|
+
`ActiveModel::Conversion` module in that class and call the Rails conversion
|
89
|
+
methods on objects of that class.
|
73
90
|
|
74
91
|
```ruby
|
75
92
|
class Person
|
@@ -92,11 +109,13 @@ person.to_param # => nil
|
|
92
109
|
|
93
110
|
### Dirty
|
94
111
|
|
95
|
-
An object becomes dirty when it has gone through one or more changes to its
|
112
|
+
An object becomes dirty when it has gone through one or more changes to its
|
113
|
+
attributes and has not been saved. `ActiveModel::Dirty` gives the ability to
|
114
|
+
check whether an object has been changed or not. It also has attribute based
|
115
|
+
accessor methods. Let's consider a Person class with attributes `first_name`
|
116
|
+
and `last_name`:
|
96
117
|
|
97
118
|
```ruby
|
98
|
-
require 'active_model'
|
99
|
-
|
100
119
|
class Person
|
101
120
|
include ActiveModel::Dirty
|
102
121
|
define_attribute_methods :first_name, :last_name
|
@@ -162,10 +181,11 @@ Track what was the previous value of the attribute.
|
|
162
181
|
|
163
182
|
```ruby
|
164
183
|
# attr_name_was accessor
|
165
|
-
person.first_name_was # =>
|
184
|
+
person.first_name_was # => nil
|
166
185
|
```
|
167
186
|
|
168
|
-
Track both previous and current value of the changed attribute. Returns an array
|
187
|
+
Track both previous and current value of the changed attribute. Returns an array
|
188
|
+
if changed, else returns nil.
|
169
189
|
|
170
190
|
```ruby
|
171
191
|
# attr_name_change
|
@@ -175,7 +195,8 @@ person.last_name_change # => nil
|
|
175
195
|
|
176
196
|
### Validations
|
177
197
|
|
178
|
-
Validations module adds the ability to class objects
|
198
|
+
`ActiveModel::Validations` module adds the ability to validate class objects
|
199
|
+
like in Active Record.
|
179
200
|
|
180
201
|
```ruby
|
181
202
|
class Person
|
@@ -188,7 +209,8 @@ class Person
|
|
188
209
|
validates! :token, presence: true
|
189
210
|
end
|
190
211
|
|
191
|
-
person = Person.new
|
212
|
+
person = Person.new
|
213
|
+
person.token = "2b1f325"
|
192
214
|
person.valid? # => false
|
193
215
|
person.name = 'vishnu'
|
194
216
|
person.email = 'me'
|
@@ -199,9 +221,9 @@ person.token = nil
|
|
199
221
|
person.valid? # => raises ActiveModel::StrictValidationFailed
|
200
222
|
```
|
201
223
|
|
202
|
-
###
|
224
|
+
### Naming
|
203
225
|
|
204
|
-
Naming adds a number of class methods which make the naming and routing
|
226
|
+
`ActiveModel::Naming` adds a number of class methods which make the naming and routing
|
205
227
|
easier to manage. The module defines the `model_name` class method which
|
206
228
|
will define a number of accessors using some `ActiveSupport::Inflector` methods.
|
207
229
|
|
@@ -221,3 +243,312 @@ Person.model_name.i18n_key # => :person
|
|
221
243
|
Person.model_name.route_key # => "people"
|
222
244
|
Person.model_name.singular_route_key # => "person"
|
223
245
|
```
|
246
|
+
|
247
|
+
### Model
|
248
|
+
|
249
|
+
`ActiveModel::Model` adds the ability to a class to work with Action Pack and
|
250
|
+
Action View right out of the box.
|
251
|
+
|
252
|
+
```ruby
|
253
|
+
class EmailContact
|
254
|
+
include ActiveModel::Model
|
255
|
+
|
256
|
+
attr_accessor :name, :email, :message
|
257
|
+
validates :name, :email, :message, presence: true
|
258
|
+
|
259
|
+
def deliver
|
260
|
+
if valid?
|
261
|
+
# deliver email
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
```
|
266
|
+
|
267
|
+
When including `ActiveModel::Model` you get some features like:
|
268
|
+
|
269
|
+
- model name introspection
|
270
|
+
- conversions
|
271
|
+
- translations
|
272
|
+
- validations
|
273
|
+
|
274
|
+
It also gives you the ability to initialize an object with a hash of attributes,
|
275
|
+
much like any Active Record object.
|
276
|
+
|
277
|
+
```ruby
|
278
|
+
email_contact = EmailContact.new(name: 'David',
|
279
|
+
email: 'david@example.com',
|
280
|
+
message: 'Hello World')
|
281
|
+
email_contact.name # => 'David'
|
282
|
+
email_contact.email # => 'david@example.com'
|
283
|
+
email_contact.valid? # => true
|
284
|
+
email_contact.persisted? # => false
|
285
|
+
```
|
286
|
+
|
287
|
+
Any class that includes `ActiveModel::Model` can be used with `form_for`,
|
288
|
+
`render` and any other Action View helper methods, just like Active Record
|
289
|
+
objects.
|
290
|
+
|
291
|
+
### Serialization
|
292
|
+
|
293
|
+
`ActiveModel::Serialization` provides a basic serialization for your object.
|
294
|
+
You need to declare an attributes hash which contains the attributes you want to
|
295
|
+
serialize. Attributes must be strings, not symbols.
|
296
|
+
|
297
|
+
```ruby
|
298
|
+
class Person
|
299
|
+
include ActiveModel::Serialization
|
300
|
+
|
301
|
+
attr_accessor :name
|
302
|
+
|
303
|
+
def attributes
|
304
|
+
{'name' => nil}
|
305
|
+
end
|
306
|
+
end
|
307
|
+
```
|
308
|
+
|
309
|
+
Now you can access a serialized hash of your object using the `serializable_hash`.
|
310
|
+
|
311
|
+
```ruby
|
312
|
+
person = Person.new
|
313
|
+
person.serializable_hash # => {"name"=>nil}
|
314
|
+
person.name = "Bob"
|
315
|
+
person.serializable_hash # => {"name"=>"Bob"}
|
316
|
+
```
|
317
|
+
|
318
|
+
#### ActiveModel::Serializers
|
319
|
+
|
320
|
+
Rails provides two serializers `ActiveModel::Serializers::JSON` and
|
321
|
+
`ActiveModel::Serializers::Xml`. Both of these modules automatically include
|
322
|
+
the `ActiveModel::Serialization`.
|
323
|
+
|
324
|
+
##### ActiveModel::Serializers::JSON
|
325
|
+
|
326
|
+
To use the `ActiveModel::Serializers::JSON` you only need to change from
|
327
|
+
`ActiveModel::Serialization` to `ActiveModel::Serializers::JSON`.
|
328
|
+
|
329
|
+
```ruby
|
330
|
+
class Person
|
331
|
+
include ActiveModel::Serializers::JSON
|
332
|
+
|
333
|
+
attr_accessor :name
|
334
|
+
|
335
|
+
def attributes
|
336
|
+
{'name' => nil}
|
337
|
+
end
|
338
|
+
end
|
339
|
+
```
|
340
|
+
|
341
|
+
With the `as_json` you have a hash representing the model.
|
342
|
+
|
343
|
+
```ruby
|
344
|
+
person = Person.new
|
345
|
+
person.as_json # => {"name"=>nil}
|
346
|
+
person.name = "Bob"
|
347
|
+
person.as_json # => {"name"=>"Bob"}
|
348
|
+
```
|
349
|
+
|
350
|
+
From a JSON string you define the attributes of the model.
|
351
|
+
You need to have the `attributes=` method defined on your class:
|
352
|
+
|
353
|
+
```ruby
|
354
|
+
class Person
|
355
|
+
include ActiveModel::Serializers::JSON
|
356
|
+
|
357
|
+
attr_accessor :name
|
358
|
+
|
359
|
+
def attributes=(hash)
|
360
|
+
hash.each do |key, value|
|
361
|
+
send("#{key}=", value)
|
362
|
+
end
|
363
|
+
end
|
364
|
+
|
365
|
+
def attributes
|
366
|
+
{'name' => nil}
|
367
|
+
end
|
368
|
+
end
|
369
|
+
```
|
370
|
+
|
371
|
+
Now it is possible to create an instance of person and set the attributes using `from_json`.
|
372
|
+
|
373
|
+
```ruby
|
374
|
+
json = { name: 'Bob' }.to_json
|
375
|
+
person = Person.new
|
376
|
+
person.from_json(json) # => #<Person:0x00000100c773f0 @name="Bob">
|
377
|
+
person.name # => "Bob"
|
378
|
+
```
|
379
|
+
|
380
|
+
##### ActiveModel::Serializers::Xml
|
381
|
+
|
382
|
+
To use the `ActiveModel::Serializers::Xml` you only need to change from
|
383
|
+
`ActiveModel::Serialization` to `ActiveModel::Serializers::Xml`.
|
384
|
+
|
385
|
+
```ruby
|
386
|
+
class Person
|
387
|
+
include ActiveModel::Serializers::Xml
|
388
|
+
|
389
|
+
attr_accessor :name
|
390
|
+
|
391
|
+
def attributes
|
392
|
+
{'name' => nil}
|
393
|
+
end
|
394
|
+
end
|
395
|
+
```
|
396
|
+
|
397
|
+
With the `to_xml` you have a XML representing the model.
|
398
|
+
|
399
|
+
```ruby
|
400
|
+
person = Person.new
|
401
|
+
person.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n <name nil=\"true\"/>\n</person>\n"
|
402
|
+
person.name = "Bob"
|
403
|
+
person.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n <name>Bob</name>\n</person>\n"
|
404
|
+
```
|
405
|
+
|
406
|
+
From a XML string you define the attributes of the model.
|
407
|
+
You need to have the `attributes=` method defined on your class:
|
408
|
+
|
409
|
+
```ruby
|
410
|
+
class Person
|
411
|
+
include ActiveModel::Serializers::Xml
|
412
|
+
|
413
|
+
attr_accessor :name
|
414
|
+
|
415
|
+
def attributes=(hash)
|
416
|
+
hash.each do |key, value|
|
417
|
+
send("#{key}=", value)
|
418
|
+
end
|
419
|
+
end
|
420
|
+
|
421
|
+
def attributes
|
422
|
+
{'name' => nil}
|
423
|
+
end
|
424
|
+
end
|
425
|
+
```
|
426
|
+
|
427
|
+
Now it is possible to create an instance of person and set the attributes using `from_xml`.
|
428
|
+
|
429
|
+
```ruby
|
430
|
+
xml = { name: 'Bob' }.to_xml
|
431
|
+
person = Person.new
|
432
|
+
person.from_xml(xml) # => #<Person:0x00000100c773f0 @name="Bob">
|
433
|
+
person.name # => "Bob"
|
434
|
+
```
|
435
|
+
|
436
|
+
### Translation
|
437
|
+
|
438
|
+
`ActiveModel::Translation` provides integration between your object and the Rails
|
439
|
+
internationalization (i18n) framework.
|
440
|
+
|
441
|
+
```ruby
|
442
|
+
class Person
|
443
|
+
extend ActiveModel::Translation
|
444
|
+
end
|
445
|
+
```
|
446
|
+
|
447
|
+
With the `human_attribute_name` you can transform attribute names into a more
|
448
|
+
human format. The human format is defined in your locale file.
|
449
|
+
|
450
|
+
* config/locales/app.pt-BR.yml
|
451
|
+
|
452
|
+
```yml
|
453
|
+
pt-BR:
|
454
|
+
activemodel:
|
455
|
+
attributes:
|
456
|
+
person:
|
457
|
+
name: 'Nome'
|
458
|
+
```
|
459
|
+
|
460
|
+
```ruby
|
461
|
+
Person.human_attribute_name('name') # => "Nome"
|
462
|
+
```
|
463
|
+
|
464
|
+
### Lint Tests
|
465
|
+
|
466
|
+
`ActiveModel::Lint::Tests` allow you to test whether an object is compliant with
|
467
|
+
the Active Model API.
|
468
|
+
|
469
|
+
* app/models/person.rb
|
470
|
+
|
471
|
+
```ruby
|
472
|
+
class Person
|
473
|
+
include ActiveModel::Model
|
474
|
+
|
475
|
+
end
|
476
|
+
```
|
477
|
+
|
478
|
+
* test/models/person_test.rb
|
479
|
+
|
480
|
+
```ruby
|
481
|
+
require 'test_helper'
|
482
|
+
|
483
|
+
class PersonTest < ActiveSupport::TestCase
|
484
|
+
include ActiveModel::Lint::Tests
|
485
|
+
|
486
|
+
def setup
|
487
|
+
@model = Person.new
|
488
|
+
end
|
489
|
+
end
|
490
|
+
```
|
491
|
+
|
492
|
+
```bash
|
493
|
+
$ rake test
|
494
|
+
|
495
|
+
Run options: --seed 14596
|
496
|
+
|
497
|
+
# Running:
|
498
|
+
|
499
|
+
......
|
500
|
+
|
501
|
+
Finished in 0.024899s, 240.9735 runs/s, 1204.8677 assertions/s.
|
502
|
+
|
503
|
+
6 runs, 30 assertions, 0 failures, 0 errors, 0 skips
|
504
|
+
```
|
505
|
+
|
506
|
+
An object is not required to implement all APIs in order to work with
|
507
|
+
Action Pack. This module only intends to provide guidance in case you want all
|
508
|
+
features out of the box.
|
509
|
+
|
510
|
+
### SecurePassword
|
511
|
+
|
512
|
+
`ActiveModel::SecurePassword` provides a way to securely store any
|
513
|
+
password in an encrypted form. On including this module, a
|
514
|
+
`has_secure_password` class method is provided which defines
|
515
|
+
an accessor named `password` with certain validations on it.
|
516
|
+
|
517
|
+
#### Requirements
|
518
|
+
|
519
|
+
`ActiveModel::SecurePassword` depends on the [`bcrypt`](https://github.com/codahale/bcrypt-ruby 'BCrypt'),
|
520
|
+
so include this gem in your Gemfile to use `ActiveModel::SecurePassword` correctly.
|
521
|
+
In order to make this work, the model must have an accessor named `password_digest`.
|
522
|
+
The `has_secure_password` will add the following validations on the `password` accessor:
|
523
|
+
|
524
|
+
1. Password should be present.
|
525
|
+
2. Password should be equal to its confirmation.
|
526
|
+
3. This maximum length of a password is 72 (required by `bcrypt` on which ActiveModel::SecurePassword depends)
|
527
|
+
|
528
|
+
#### Examples
|
529
|
+
|
530
|
+
```ruby
|
531
|
+
class Person
|
532
|
+
include ActiveModel::SecurePassword
|
533
|
+
has_secure_password
|
534
|
+
attr_accessor :password_digest
|
535
|
+
end
|
536
|
+
|
537
|
+
person = Person.new
|
538
|
+
|
539
|
+
# When password is blank.
|
540
|
+
person.valid? # => false
|
541
|
+
|
542
|
+
# When the confirmation doesn't match the password.
|
543
|
+
person.password = 'aditya'
|
544
|
+
person.password_confirmation = 'nomatch'
|
545
|
+
person.valid? # => false
|
546
|
+
|
547
|
+
# When the length of password, exceeds 72.
|
548
|
+
person.password = person.password_confirmation = 'a' * 100
|
549
|
+
person.valid? # => false
|
550
|
+
|
551
|
+
# When all validations are passed.
|
552
|
+
person.password = person.password_confirmation = 'aditya'
|
553
|
+
person.valid? # => true
|
554
|
+
```
|