aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2008-11-15 16:48:14 +0100
committerDavid Heinemeier Hansson <david@loudthinking.com>2008-11-15 16:48:14 +0100
commit9eaa0a3449595d07fe2ada5c4c93ec226622147c (patch)
tree9bf645cc94377fcdc49846c1aadddf6b2dab0702 /activesupport
parent8d8195dc2443755786d3a8598b7c2da1f08a38e7 (diff)
downloadrails-9eaa0a3449595d07fe2ada5c4c93ec226622147c.tar.gz
rails-9eaa0a3449595d07fe2ada5c4c93ec226622147c.tar.bz2
rails-9eaa0a3449595d07fe2ada5c4c93ec226622147c.zip
Added lambda merging to OptionMerger (especially useful with named_scope and with_options) [#740 state:committed] (Paweł Kondzior)
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/option_merger.rb8
-rw-r--r--activesupport/test/option_merger_test.rb8
2 files changed, 15 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/option_merger.rb b/activesupport/lib/active_support/option_merger.rb
index b563b093ed..63662b75c7 100644
--- a/activesupport/lib/active_support/option_merger.rb
+++ b/activesupport/lib/active_support/option_merger.rb
@@ -10,7 +10,13 @@ module ActiveSupport
private
def method_missing(method, *arguments, &block)
- arguments << (arguments.last.respond_to?(:to_hash) ? @options.deep_merge(arguments.pop) : @options.dup)
+ if arguments.last.is_a?(Proc)
+ proc = arguments.pop
+ arguments << lambda { |*args| @options.deep_merge(proc.call(*args)) }
+ else
+ arguments << (arguments.last.respond_to?(:to_hash) ? @options.deep_merge(arguments.pop) : @options.dup)
+ end
+
@context.__send__(method, *arguments, &block)
end
end
diff --git a/activesupport/test/option_merger_test.rb b/activesupport/test/option_merger_test.rb
index 0d72314880..f26d61617d 100644
--- a/activesupport/test/option_merger_test.rb
+++ b/activesupport/test/option_merger_test.rb
@@ -64,6 +64,14 @@ class OptionMergerTest < Test::Unit::TestCase
end
end
end
+
+ def test_nested_method_with_options_using_lamdba
+ local_lamdba = lambda { { :lambda => true } }
+ with_options(@options) do |o|
+ assert_equal @options.merge(local_lamdba.call),
+ o.method_with_options(local_lamdba).call
+ end
+ end
# Needed when counting objects with the ObjectSpace
def test_option_merger_class_method