aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorVipul A M <vipulnsward@gmail.com>2017-06-11 17:40:28 +0530
committerGitHub <noreply@github.com>2017-06-11 17:40:28 +0530
commit1d2cc1324e04773b6bb6c2692f38ccb815bf32c8 (patch)
treea7a2dccdd404ca41b2d95d12a36fed58b62aa286 /guides
parentf81f840c02cff34c169e6fca348fab9a372c8372 (diff)
parent43512df990ff1bba30b4b3cfa427b314a0314623 (diff)
downloadrails-1d2cc1324e04773b6bb6c2692f38ccb815bf32c8.tar.gz
rails-1d2cc1324e04773b6bb6c2692f38ccb815bf32c8.tar.bz2
rails-1d2cc1324e04773b6bb6c2692f38ccb815bf32c8.zip
Merge pull request #29268 from gsamokovarov/delegate-missing-to-guides
Document Module#delegate_missing_to in the guides [ci skip]
Diffstat (limited to 'guides')
-rw-r--r--guides/source/active_support_core_extensions.md21
1 files changed, 21 insertions, 0 deletions
diff --git a/guides/source/active_support_core_extensions.md b/guides/source/active_support_core_extensions.md
index e34af8aa8d..aa81d1eb6f 100644
--- a/guides/source/active_support_core_extensions.md
+++ b/guides/source/active_support_core_extensions.md
@@ -755,6 +755,8 @@ NOTE: Defined in `active_support/core_ext/module/anonymous.rb`.
### Method Delegation
+#### `delegate`
+
The macro `delegate` offers an easy way to forward methods.
Let's imagine that users in some application have login information in the `User` model but name and other data in a separate `Profile` model:
@@ -837,6 +839,25 @@ In the previous example the macro generates `avatar_size` rather than `size`.
NOTE: Defined in `active_support/core_ext/module/delegation.rb`
+#### `delegate_missing_to`
+
+Imagine you would like to delegate everything missing from the `User` object,
+to the `Profile` one. The `delegate_missing_to` macro lets you implement this
+in a breeze:
+
+```ruby
+class User < ApplicationRecord
+ has_one :profile
+
+ delegate_missing_to :profile
+end
+```
+
+The target can be anything callable within the object, e.g. instance variables,
+methods, constants, etc. Only the public methods of the target are delegated.
+
+NOTE: Defined in `active_support/core_ext/module/delegation.rb`
+
### Redefining Methods
There are cases where you need to define a method with `define_method`, but don't know whether a method with that name already exists. If it does, a warning is issued if they are enabled. No big deal, but not clean either.