Skip to content

Commit 702ad71

Browse files
author
David Heinemeier Hansson
committed
Added Object#self which returns the object itself
1 parent 20405e5 commit 702ad71

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

activesupport/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Added Object#self which returns the object itself. Useful when dealing with a chaining scenario, like Active Record scopes:
2+
3+
Event.public_send(state.presence_in?([ :trashed, :drafted ]) ? :self).order(:created_at)
4+
5+
*DHH*
6+
17
* `Object#with_options` executes block in merging option context when
28
explicit receiver in not passed.
39

activesupport/lib/active_support/core_ext/object.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'active_support/core_ext/object/blank'
33
require 'active_support/core_ext/object/duplicable'
44
require 'active_support/core_ext/object/deep_dup'
5+
require 'active_support/core_ext/object/self'
56
require 'active_support/core_ext/object/try'
67
require 'active_support/core_ext/object/inclusion'
78

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Object
2+
# Returns the object itself. Useful when dealing with a chaining scenario, like Active Record scopes:
3+
#
4+
# Event.public_send(state.presence_in?([ :trashed, :drafted ]) ? :self).order(:created_at)
5+
#
6+
# @return Object
7+
def self
8+
self
9+
end
10+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'abstract_unit'
2+
require 'active_support/core_ext/object'
3+
4+
class Object::SelfTest < ActiveSupport::TestCase
5+
test 'self returns self' do
6+
object = 'fun'
7+
assert_equal object, object.self
8+
end
9+
end

0 commit comments

Comments
 (0)