diff options
author | Matt Tanous <mtanous22@gmail.com> | 2018-07-25 17:04:34 -0400 |
---|---|---|
committer | Matt Tanous <mtanous22@gmail.com> | 2019-05-23 12:11:39 -0400 |
commit | f002be148e1377709ed28b8e80c5db76ee2fa410 (patch) | |
tree | eb368b86d475a4cd3449de50bc8af3d40e4b48fa /activesupport/lib/active_support/core_ext/module | |
parent | eba859d867454cd441f29ff15d8dbf442e51919c (diff) | |
download | rails-f002be148e1377709ed28b8e80c5db76ee2fa410.tar.gz rails-f002be148e1377709ed28b8e80c5db76ee2fa410.tar.bz2 rails-f002be148e1377709ed28b8e80c5db76ee2fa410.zip |
Add :allow_nil option to delegate_missing_to; use in ActiveStorage
attachment
Diffstat (limited to 'activesupport/lib/active_support/core_ext/module')
-rw-r--r-- | activesupport/lib/active_support/core_ext/module/delegation.rb | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index 9fe7f8fe01..2f88010d27 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -274,8 +274,9 @@ class Module # variables, methods, constants, etc. # # The delegated method must be public on the target, otherwise it will - # raise +NoMethodError+. - def delegate_missing_to(target) + # raise +DelegationError+. If you wish to instead return +nil+, + # use the <tt>:allow_nil</tt> option. + def delegate_missing_to(target, allow_nil: nil) target = target.to_s target = "self.#{target}" if DELEGATION_RESERVED_METHOD_NAMES.include?(target) @@ -295,7 +296,11 @@ class Module super rescue NoMethodError if #{target}.nil? - raise DelegationError, "\#{method} delegated to #{target}, but #{target} is nil" + if #{allow_nil == true} + return nil + else + raise DelegationError, "\#{method} delegated to #{target}, but #{target} is nil" + end else raise end |