aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@basecamp.com>2014-07-29 13:38:24 -0700
committerDavid Heinemeier Hansson <david@basecamp.com>2014-07-29 13:38:24 -0700
commitc56997eb1b4005f59f63d9cb2203424c905cd4b8 (patch)
treeb210d4d3fd45153cab1bbda471e248a47d1e8474 /activesupport/lib
parent160cc6956c3c9da0eab66546d3c266d9f317bbc1 (diff)
parent0cb3cc4ff794b9c3e92afa97a6d3c8e3acbf16ac (diff)
downloadrails-c56997eb1b4005f59f63d9cb2203424c905cd4b8.tar.gz
rails-c56997eb1b4005f59f63d9cb2203424c905cd4b8.tar.bz2
rails-c56997eb1b4005f59f63d9cb2203424c905cd4b8.zip
Merge pull request #16339 from rwz/with_options_implicit
Add implicit receiver support to `Object#with_options`
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/core_ext/object/with_options.rb17
1 files changed, 15 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/with_options.rb b/activesupport/lib/active_support/core_ext/object/with_options.rb
index 42e388b065..42e87c4424 100644
--- a/activesupport/lib/active_support/core_ext/object/with_options.rb
+++ b/activesupport/lib/active_support/core_ext/object/with_options.rb
@@ -34,9 +34,22 @@ class Object
# body i18n.t :body, user_name: user.name
# end
#
+ # When you don't pass an explicit receiver, it executes the whole block
+ # in merging options context:
+ #
+ # class Account < ActiveRecord::Base
+ # with_options dependent: :destroy do
+ # has_many :customers
+ # has_many :products
+ # has_many :invoices
+ # has_many :expenses
+ # end
+ # end
+ #
# <tt>with_options</tt> can also be nested since the call is forwarded to its receiver.
# Each nesting level will merge inherited defaults in addition to their own.
- def with_options(options)
- yield ActiveSupport::OptionMerger.new(self, options)
+ def with_options(options, &block)
+ option_merger = ActiveSupport::OptionMerger.new(self, options)
+ block.arity.zero? ? option_merger.instance_eval(&block) : block.call(option_merger)
end
end