aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2014-08-29 15:19:32 -0700
committerDavid Heinemeier Hansson <david@loudthinking.com>2014-08-29 15:19:32 -0700
commit961945046848492ddc541700419cf553e7817c94 (patch)
treeb9119602d1b03137c8408afbc1f9fa9614e67ba1 /activesupport
parentcc0d8fbec96aeb1664fe98d45929a236b18aec81 (diff)
downloadrails-961945046848492ddc541700419cf553e7817c94.tar.gz
rails-961945046848492ddc541700419cf553e7817c94.tar.bz2
rails-961945046848492ddc541700419cf553e7817c94.zip
Use instance_eval on @tenderlove's suggestion :trollface:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md2
-rw-r--r--activesupport/lib/active_support/core_ext/object/blank.rb9
-rw-r--r--activesupport/test/core_ext/object/blank_test.rb4
3 files changed, 8 insertions, 7 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index b32c97bead..5894dab230 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,6 +1,6 @@
* Added yield to Object#presence, so you can do this:
- project.account.owner.presence { |p| p.name.first } || 'Nobody'
+ project.account.owner.presence { name.first } || 'Nobody'
instead of calling twice (which may incur double SQL calls):
diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb
index 893858fcd6..99fd22ff56 100644
--- a/activesupport/lib/active_support/core_ext/object/blank.rb
+++ b/activesupport/lib/active_support/core_ext/object/blank.rb
@@ -40,15 +40,16 @@ class Object
# region = params[:state].presence || params[:country].presence || 'US'
#
# You can also use this with a block that will be yielded if the object is present
- # and the result of that block will then be returned
+ # and the result of that block will then be returned. The block itself is run against
+ # the instance you're running #presence on (using instance_eval)
#
- # project.account.owner.presence { |p| p.name.first } || 'Nobody'
+ # project.account.owner.presence { name.first } || 'Nobody'
#
# @return [Object]
- def presence
+ def presence(&block)
if present?
if block_given?
- yield self
+ instance_eval(&block)
else
self
end
diff --git a/activesupport/test/core_ext/object/blank_test.rb b/activesupport/test/core_ext/object/blank_test.rb
index 7b3f10b4da..34d10c6981 100644
--- a/activesupport/test/core_ext/object/blank_test.rb
+++ b/activesupport/test/core_ext/object/blank_test.rb
@@ -35,7 +35,7 @@ class BlankTest < ActiveSupport::TestCase
end
def test_presence_with_a_block
- assert_equal "SALLY", "sally".presence(&:upcase) || "Nobody"
- assert_equal "Nobody", nil.presence(&:upcase) || "Nobody"
+ assert_equal "SALLY", "sally".presence { upcase } || "Nobody"
+ assert_equal "Nobody", nil.presence { upcase } || "Nobody"
end
end